GraphQL.js Subscription response error when nested queries/resolvers












0















I have the following subscription query that works fine:



subscription{
streamShipmentOrder{
id
}
}


But the following sends an "Cannot read property 'ShipmentOrder' of undefined" error



subscription{
streamShipmentOrder{
id
logs{
comment
}
}
}


Here is my ShipmentOrder type definition, I'm using sequelize to talk to the database



const ShipmentOrderType = new GraphQLObjectType({
name: 'ShipmentOrder',
fields: () => ({
id: { type: GraphQLID },
logs: {
type: new GraphQLList(LogType),
resolve: async (parent, args, { models }, info) => {
return await models.ShipmentOrder.findOne({
where: { id: parent.id },
include: [{ model: models.Log }],
}).then((data) => data.logs);
}
},
}


And here is the Subscription definition



const ShipmentOrderSubscription = new GraphQLObjectType({
name: 'Subscription',
fields: () => ({
streamShipmentOrder: {
type: ShipmentOrderType,
resolve: (payload) => payload.shipmentOrderData,
subscribe: () =>
socket.asyncIterator([
SHIPMENT_ORDER_CREATED,
SHIPMENT_ORDER_UPDATED,
SHIPMENT_ORDER_DELETED
]),
},
}),
});


And the mutation that fires the subscription, (also the mutation works fine when asking for nested results)



const ShipmentOrderMutation = new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
createShipmentOrder: {
type: ShipmentOrderType,
args: {...},
resolve: async (parent, args, { models }, info) => {

// create shipmentOrder
return await models.ShipmentOrder.create({...})
.then(async (createdShipmentOrder) => {

// create log and relate to created shipmentOrder
await models.Log.create({...});

// get created shipmentOrder with its logs
return await models.ShipmentOrder.findOne({
where: { id: createdShipmentOrder.id },
include: [{ model: models.Log }],
}).then((foundShipmentOrder) => {

// updates streams
socket.publish(SHIPMENT_ORDER_CREATED, {
shipmentOrderData: foundShipmentOrder,
});
return foundShipmentOrder;

});
}
}
}
})
});


What could I be missing?










share|improve this question























  • Presumably, models is undefined inside the resolver for the logs field. How are you importing models inside that file? How is that object exported? Are you using the "associate" pattern outlined here (github.com/sequelize/express-example) to prevent cyclic dependencies?

    – Daniel Rearden
    Nov 25 '18 at 13:36











  • Yes @DanielRearden, associations work on every other query and mutation, also every subscription when asking only for the main type works too, it crashes only when asking for other "sub-properties" or relations on the graphql response object in a subscription, like in this problems case.

    – Otoniel Guajardo
    Nov 25 '18 at 18:03











  • @DanielRearden you where right! Log resolver context contains the model object when calling query and mutation but empty when calling subscription!! I'm still figuring out why...

    – Otoniel Guajardo
    Nov 25 '18 at 23:28
















0















I have the following subscription query that works fine:



subscription{
streamShipmentOrder{
id
}
}


But the following sends an "Cannot read property 'ShipmentOrder' of undefined" error



subscription{
streamShipmentOrder{
id
logs{
comment
}
}
}


Here is my ShipmentOrder type definition, I'm using sequelize to talk to the database



const ShipmentOrderType = new GraphQLObjectType({
name: 'ShipmentOrder',
fields: () => ({
id: { type: GraphQLID },
logs: {
type: new GraphQLList(LogType),
resolve: async (parent, args, { models }, info) => {
return await models.ShipmentOrder.findOne({
where: { id: parent.id },
include: [{ model: models.Log }],
}).then((data) => data.logs);
}
},
}


And here is the Subscription definition



const ShipmentOrderSubscription = new GraphQLObjectType({
name: 'Subscription',
fields: () => ({
streamShipmentOrder: {
type: ShipmentOrderType,
resolve: (payload) => payload.shipmentOrderData,
subscribe: () =>
socket.asyncIterator([
SHIPMENT_ORDER_CREATED,
SHIPMENT_ORDER_UPDATED,
SHIPMENT_ORDER_DELETED
]),
},
}),
});


And the mutation that fires the subscription, (also the mutation works fine when asking for nested results)



const ShipmentOrderMutation = new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
createShipmentOrder: {
type: ShipmentOrderType,
args: {...},
resolve: async (parent, args, { models }, info) => {

// create shipmentOrder
return await models.ShipmentOrder.create({...})
.then(async (createdShipmentOrder) => {

// create log and relate to created shipmentOrder
await models.Log.create({...});

// get created shipmentOrder with its logs
return await models.ShipmentOrder.findOne({
where: { id: createdShipmentOrder.id },
include: [{ model: models.Log }],
}).then((foundShipmentOrder) => {

// updates streams
socket.publish(SHIPMENT_ORDER_CREATED, {
shipmentOrderData: foundShipmentOrder,
});
return foundShipmentOrder;

});
}
}
}
})
});


What could I be missing?










share|improve this question























  • Presumably, models is undefined inside the resolver for the logs field. How are you importing models inside that file? How is that object exported? Are you using the "associate" pattern outlined here (github.com/sequelize/express-example) to prevent cyclic dependencies?

    – Daniel Rearden
    Nov 25 '18 at 13:36











  • Yes @DanielRearden, associations work on every other query and mutation, also every subscription when asking only for the main type works too, it crashes only when asking for other "sub-properties" or relations on the graphql response object in a subscription, like in this problems case.

    – Otoniel Guajardo
    Nov 25 '18 at 18:03











  • @DanielRearden you where right! Log resolver context contains the model object when calling query and mutation but empty when calling subscription!! I'm still figuring out why...

    – Otoniel Guajardo
    Nov 25 '18 at 23:28














0












0








0








I have the following subscription query that works fine:



subscription{
streamShipmentOrder{
id
}
}


But the following sends an "Cannot read property 'ShipmentOrder' of undefined" error



subscription{
streamShipmentOrder{
id
logs{
comment
}
}
}


Here is my ShipmentOrder type definition, I'm using sequelize to talk to the database



const ShipmentOrderType = new GraphQLObjectType({
name: 'ShipmentOrder',
fields: () => ({
id: { type: GraphQLID },
logs: {
type: new GraphQLList(LogType),
resolve: async (parent, args, { models }, info) => {
return await models.ShipmentOrder.findOne({
where: { id: parent.id },
include: [{ model: models.Log }],
}).then((data) => data.logs);
}
},
}


And here is the Subscription definition



const ShipmentOrderSubscription = new GraphQLObjectType({
name: 'Subscription',
fields: () => ({
streamShipmentOrder: {
type: ShipmentOrderType,
resolve: (payload) => payload.shipmentOrderData,
subscribe: () =>
socket.asyncIterator([
SHIPMENT_ORDER_CREATED,
SHIPMENT_ORDER_UPDATED,
SHIPMENT_ORDER_DELETED
]),
},
}),
});


And the mutation that fires the subscription, (also the mutation works fine when asking for nested results)



const ShipmentOrderMutation = new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
createShipmentOrder: {
type: ShipmentOrderType,
args: {...},
resolve: async (parent, args, { models }, info) => {

// create shipmentOrder
return await models.ShipmentOrder.create({...})
.then(async (createdShipmentOrder) => {

// create log and relate to created shipmentOrder
await models.Log.create({...});

// get created shipmentOrder with its logs
return await models.ShipmentOrder.findOne({
where: { id: createdShipmentOrder.id },
include: [{ model: models.Log }],
}).then((foundShipmentOrder) => {

// updates streams
socket.publish(SHIPMENT_ORDER_CREATED, {
shipmentOrderData: foundShipmentOrder,
});
return foundShipmentOrder;

});
}
}
}
})
});


What could I be missing?










share|improve this question














I have the following subscription query that works fine:



subscription{
streamShipmentOrder{
id
}
}


But the following sends an "Cannot read property 'ShipmentOrder' of undefined" error



subscription{
streamShipmentOrder{
id
logs{
comment
}
}
}


Here is my ShipmentOrder type definition, I'm using sequelize to talk to the database



const ShipmentOrderType = new GraphQLObjectType({
name: 'ShipmentOrder',
fields: () => ({
id: { type: GraphQLID },
logs: {
type: new GraphQLList(LogType),
resolve: async (parent, args, { models }, info) => {
return await models.ShipmentOrder.findOne({
where: { id: parent.id },
include: [{ model: models.Log }],
}).then((data) => data.logs);
}
},
}


And here is the Subscription definition



const ShipmentOrderSubscription = new GraphQLObjectType({
name: 'Subscription',
fields: () => ({
streamShipmentOrder: {
type: ShipmentOrderType,
resolve: (payload) => payload.shipmentOrderData,
subscribe: () =>
socket.asyncIterator([
SHIPMENT_ORDER_CREATED,
SHIPMENT_ORDER_UPDATED,
SHIPMENT_ORDER_DELETED
]),
},
}),
});


And the mutation that fires the subscription, (also the mutation works fine when asking for nested results)



const ShipmentOrderMutation = new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
createShipmentOrder: {
type: ShipmentOrderType,
args: {...},
resolve: async (parent, args, { models }, info) => {

// create shipmentOrder
return await models.ShipmentOrder.create({...})
.then(async (createdShipmentOrder) => {

// create log and relate to created shipmentOrder
await models.Log.create({...});

// get created shipmentOrder with its logs
return await models.ShipmentOrder.findOne({
where: { id: createdShipmentOrder.id },
include: [{ model: models.Log }],
}).then((foundShipmentOrder) => {

// updates streams
socket.publish(SHIPMENT_ORDER_CREATED, {
shipmentOrderData: foundShipmentOrder,
});
return foundShipmentOrder;

});
}
}
}
})
});


What could I be missing?







graphql-js apollo-server graphql-subscriptions






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 6:04









Otoniel GuajardoOtoniel Guajardo

1




1













  • Presumably, models is undefined inside the resolver for the logs field. How are you importing models inside that file? How is that object exported? Are you using the "associate" pattern outlined here (github.com/sequelize/express-example) to prevent cyclic dependencies?

    – Daniel Rearden
    Nov 25 '18 at 13:36











  • Yes @DanielRearden, associations work on every other query and mutation, also every subscription when asking only for the main type works too, it crashes only when asking for other "sub-properties" or relations on the graphql response object in a subscription, like in this problems case.

    – Otoniel Guajardo
    Nov 25 '18 at 18:03











  • @DanielRearden you where right! Log resolver context contains the model object when calling query and mutation but empty when calling subscription!! I'm still figuring out why...

    – Otoniel Guajardo
    Nov 25 '18 at 23:28



















  • Presumably, models is undefined inside the resolver for the logs field. How are you importing models inside that file? How is that object exported? Are you using the "associate" pattern outlined here (github.com/sequelize/express-example) to prevent cyclic dependencies?

    – Daniel Rearden
    Nov 25 '18 at 13:36











  • Yes @DanielRearden, associations work on every other query and mutation, also every subscription when asking only for the main type works too, it crashes only when asking for other "sub-properties" or relations on the graphql response object in a subscription, like in this problems case.

    – Otoniel Guajardo
    Nov 25 '18 at 18:03











  • @DanielRearden you where right! Log resolver context contains the model object when calling query and mutation but empty when calling subscription!! I'm still figuring out why...

    – Otoniel Guajardo
    Nov 25 '18 at 23:28

















Presumably, models is undefined inside the resolver for the logs field. How are you importing models inside that file? How is that object exported? Are you using the "associate" pattern outlined here (github.com/sequelize/express-example) to prevent cyclic dependencies?

– Daniel Rearden
Nov 25 '18 at 13:36





Presumably, models is undefined inside the resolver for the logs field. How are you importing models inside that file? How is that object exported? Are you using the "associate" pattern outlined here (github.com/sequelize/express-example) to prevent cyclic dependencies?

– Daniel Rearden
Nov 25 '18 at 13:36













Yes @DanielRearden, associations work on every other query and mutation, also every subscription when asking only for the main type works too, it crashes only when asking for other "sub-properties" or relations on the graphql response object in a subscription, like in this problems case.

– Otoniel Guajardo
Nov 25 '18 at 18:03





Yes @DanielRearden, associations work on every other query and mutation, also every subscription when asking only for the main type works too, it crashes only when asking for other "sub-properties" or relations on the graphql response object in a subscription, like in this problems case.

– Otoniel Guajardo
Nov 25 '18 at 18:03













@DanielRearden you where right! Log resolver context contains the model object when calling query and mutation but empty when calling subscription!! I'm still figuring out why...

– Otoniel Guajardo
Nov 25 '18 at 23:28





@DanielRearden you where right! Log resolver context contains the model object when calling query and mutation but empty when calling subscription!! I'm still figuring out why...

– Otoniel Guajardo
Nov 25 '18 at 23:28












1 Answer
1






active

oldest

votes


















0














As @DanielRearden pointed me out, context was empty when calling the subscription, and that messed up my relations, this because context is handled differently on query/mutations and subscriptions.



So what fixed it was adding my models object to the onConnect property of the subscriptions option in the ApolloServer, passing the same objects I was already passing to my context option



const server = new ApolloServer({
schema,
subscriptions: {
onConnect: () => ({models}),
},
context: () => ({models})
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465071%2fgraphql-js-subscription-response-error-when-nested-queries-resolvers%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    As @DanielRearden pointed me out, context was empty when calling the subscription, and that messed up my relations, this because context is handled differently on query/mutations and subscriptions.



    So what fixed it was adding my models object to the onConnect property of the subscriptions option in the ApolloServer, passing the same objects I was already passing to my context option



    const server = new ApolloServer({
    schema,
    subscriptions: {
    onConnect: () => ({models}),
    },
    context: () => ({models})
    }





    share|improve this answer




























      0














      As @DanielRearden pointed me out, context was empty when calling the subscription, and that messed up my relations, this because context is handled differently on query/mutations and subscriptions.



      So what fixed it was adding my models object to the onConnect property of the subscriptions option in the ApolloServer, passing the same objects I was already passing to my context option



      const server = new ApolloServer({
      schema,
      subscriptions: {
      onConnect: () => ({models}),
      },
      context: () => ({models})
      }





      share|improve this answer


























        0












        0








        0







        As @DanielRearden pointed me out, context was empty when calling the subscription, and that messed up my relations, this because context is handled differently on query/mutations and subscriptions.



        So what fixed it was adding my models object to the onConnect property of the subscriptions option in the ApolloServer, passing the same objects I was already passing to my context option



        const server = new ApolloServer({
        schema,
        subscriptions: {
        onConnect: () => ({models}),
        },
        context: () => ({models})
        }





        share|improve this answer













        As @DanielRearden pointed me out, context was empty when calling the subscription, and that messed up my relations, this because context is handled differently on query/mutations and subscriptions.



        So what fixed it was adding my models object to the onConnect property of the subscriptions option in the ApolloServer, passing the same objects I was already passing to my context option



        const server = new ApolloServer({
        schema,
        subscriptions: {
        onConnect: () => ({models}),
        },
        context: () => ({models})
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 23:57









        Otoniel GuajardoOtoniel Guajardo

        1




        1
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465071%2fgraphql-js-subscription-response-error-when-nested-queries-resolvers%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen