Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling GraphQL mutation from another mutation?

Is it possible run a mutation on another graphqlType from within a mutation? I'm not sure if this is a good idea or not. Ideally it would be nice to have create mutations for each one of graphql data types and then call into each of those functions when needed. Hopefully this keeps things more DRY.

For example I have 2 graphql types "projects" and "rooms". For every project a room needs to be created and the ID placed in the project for reference later.

I see 3 ways of doing this:

  • From front end, run 2 different graphql queries (for a new project and a new room) and a final one updating the project with the new room id.
  • On the backend/graphql server. Just handle all the db calls inside of one resolve function for simplicity, returning the project and the room that were created.
  • Running mutations inside a mutation. Call into the room create function from the project create function, and returning both data sets.

Still learning the ropes with graphql so any insight on whats good practice would be awesome.

like image 463
Justin Avatar asked Sep 13 '16 13:09

Justin


People also ask

Are GraphQL mutations Atomic?

While GraphQL allows the client to freely shape query and response, mutations (create, update or delete operations) are by design atomic.

How do you use update mutations in GraphQL?

Update mutations take filter as an input to select specific objects. You can specify set and remove operations on fields belonging to the filtered objects. It returns the state of the objects after updating. Note Executing an empty remove {} or an empty set{} doesn't have any effect on the update mutation.


1 Answers

I'm sure you figured out a solution to your problem long ago, but why not just make a mutation that captures the entire process, like createProjectAndRoom? I think it's a mistake to think of GraphQL resolvers as mapping very rigidly to single actions on single models.

like image 88
acjay Avatar answered Oct 17 '22 08:10

acjay