How to decide which root value to use for GraphQL mutations

I am experimenting with GraphQL and need help to understand how to find out what root value needs to be provided when the mutation is done.

Case study: you want to update the username of the current user. The mutation is as follows:

mutation TestMutation { updateMyUsername(newName: "CoolName") { username } } 

The root object to provide here is the current user. But how do you know this when you get a mutation without disassembling it and not seeing its name? Based on the URL, it is not possible to solve only one endpoint. And parsing a string just to send it further, where it will be parsed, at best seems wasteful.

Perhaps a common practice is to provide additional parameters in the URL or in the body of the request to provide the application with more context?

+5
source share
1 answer

It's not often to include more data in a URL, but since each mutation is supported by a function, you do not need to parse its name, you can simply define it in the function.

Because mutations are always top-level, their root object will always be the top-level root object. If you need access to another object, you need to make sure that you provide enough information to receive it.

To track the current user, go to context , one of the initial arguments to run the GraphQL query and the permissions granted in each function. Context is very often used to track a registered user.

Now that you have a function that defines "changeMyUsername", you have a registered user, and you have provided arguments with a new username, you should have everything you need to implement the body of this function.

+5
source

Source: https://habr.com/ru/post/1247658/


All Articles