Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relations not loading after mutation #82

Open
ryanvanderpol opened this issue Jun 28, 2019 · 3 comments
Open

Relations not loading after mutation #82

ryanvanderpol opened this issue Jun 28, 2019 · 3 comments

Comments

@ryanvanderpol
Copy link

I have a mutation defined as follows


const mutation = {
    description: 'Updates a Person',
    type: graphQlSchema.getType('Persons'),
    args: {
        id: {
            type: new GraphQLNonNull(GraphQLInt),
            description: 'Person ID',
        },
        first_name: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'First Name',
        },
        last_name: {
            type: new GraphQLNonNull(GraphQLString),
            description: 'Last Name',
        }
    },
    resolve: async (root, userData) => {
        const { id, first_name, last_name } = userData;
        return await Person.query().findById(id).patch({ first_name, last_name }).returning('*');
    },
};

Which I call from my app like

mutation($id: Int!, firstName: String!, lastName: String!) {
    updatePerson(id: $id, first_name: $firstName, last_name: $lastName) {
        id,
        first_name,
        last_name,
        pets {
                name,
                toys {
                        brand_name,
                        quantity
                }
        }
    }
}

The mutation part works fine, but when the data is returned from the server, it's missing the pets object and everything below it. Interestingly (maybe), pets actually comes back as null rather than being omitted completely, so it seems like maybe it tried to complete the relation, but nothing came back.

Any idea what could be going on here?

@DaKaZ
Copy link

DaKaZ commented Aug 26, 2019

@ryanvanderpol I think what is wrong is that you need to do two things. First, separate out your find and patch query, secondly add an EAGER to your patch to pull in the pets, and third use the patchAndFetch instead of just patch. Here is an untested example but it is similar to what is working for me:

resolve: async (root, userData) => {
    const { id, first_name, last_name } = userData;
    const updating = await Person.query().findById(id);
    return await updating.$query().eager('pets').patchAndFetch({ first_name, last_name });
}

@ryanvanderpol
Copy link
Author

Thanks, @DaKaZ. in my OP I noted that a pet also has toys. Would I need to add toys to the eager list? And if that's the case, what happens when I have way, way more complex objects that are nested way deeper than this? I need to explicitly add everything to the eager query?

I guess my question is, why can't I get a mutation to work like the built-in Objection.js types? Using the built-in .model types the query can dynamically load the return object and its nested types based on the GraphQL request -- I just want my mutations to return objects using the same behavior.

@tsimons
Copy link

tsimons commented Sep 12, 2019

This might not be ideal depending on your project, but you can refetch the query after performing the mutation. Apollo boost has this an option when making a query.

If performance is a concern, and you don't want to eager load all of the relations, you'll have to traverse the ast and find the relations being requested. You can see an example how to do that in the SchemaBuilder class: https://github.com/Vincit/objection-graphql/blob/master/lib/SchemaBuilder.js#L301

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants