Calling a GraphQL mutation from another mutation?

Is it possible to run a mutation in another type of graphqlType from a mutation? I'm not sure if this is a good idea or not. Ideally, it would be nice to create mutations for each of the graphql data types, and then, if necessary, access each of these functions. Hope this keeps things drier.

For example, I have 2 types of graphql "projects" and "rooms". For each project, you need to create a room, and the identifier placed in the project for reference later.

I see 3 ways to do this:

  • From the external interface , run 2 different graphql queries (for a new project and a new room), and the last one will update the project with a new room identifier.
  • On the backend / graphql server . Just make all the db calls inside one resolution function for simplicity by returning the project and the created room.
  • Perform mutations within a mutation. A call to the room creates a function from the project creation function and returns both data sets.

By continuing to learn the ropes with graphql, you can understand how good practice would be awesome.

+5
source share
2 answers

The most common way to do this is on the afterInsert hook (or similarly named). Thus, in this case, after creating the β€œroom”, create a β€œproject” during the hook after insertion.

0
source

I’m sure that you have long solved your problem, but why not just make a mutation that captures the whole process, for example createProjectAndRoom ? I think it is a mistake to think of GraphQL resolvers as it is very tough to compare one action with single models.

0
source

All Articles