Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing complex arguments to GraphQL mutations

I've been using GraphQL in a Node server using graphql-js, and GraphQL has shown to be an extremely valuable abstraction, but I'm running into an issue.

I often find myself needing to pass large structured objects as arguments to GraphQL mutations, using GraphQLInputObjectType. This would be fine, but GraphQL doesn't support the use of JSON notation :(. So I end up just sending a string containing the JSON, for the server to deal with.

const objectStr = JSON.stringify(object).replace(new RegExp("\"", "g"), "'")

graphQLClient(`{
    user: updateUser(someDataObject: "${objectStr}") {...}
}`)

But now I'm not benefiting at all from GraphQL!

I have a feeling I'm doing something wrong here. What is the GraphQL way of sending, say, signup form data, to a mutation?

like image 991
functorial Avatar asked Apr 25 '17 06:04

functorial


People also ask

How do I create a mutation in GraphQL?

To call a mutation, you must use the keyword mutation before your GraphQL query. To pass an input type, provide the data written as if it's a JSON object. For example, with the server defined above, you can create a new message and return the id of the new message with this operation: mutation {. createMessage(input: {.

How do you pass arguments to a GraphQL endpoint?

Passing Arguments Just like a REST API, it's common to pass arguments to an endpoint in a GraphQL API. By defining the arguments in the schema language, typechecking happens automatically. Each argument must be named and have a type.

How do I ADD arguments to the GraphQL schema language?

Each argument must be named and have a type. For example, in the Basic Types documentation we had an endpoint called rollThreeDice: Instead of hardcoding “three”, we might want a more general function that rolls numDice dice, each of which have numSides sides. We can add arguments to the GraphQL schema language like this:

How to pass an input type to a mutation client?

To pass an input type, provide the data written as if it's a JSON object. For example, with the server defined above, you can create a new message and return the id of the new message with this operation: You can use variables to simplify mutation client logic just like you can with queries.


1 Answers

The best way of doing this is to use input objects.

Essentially your request would look like:

/* Query */
mutation Update($input: UpdateUserInput!) {
    updateUser(input: $input) {
        changedUser {
            id
            username
        }
    }
}

/* Variables (as JSON) */
{
    "input": {
        "username": "[email protected]",
        "password": "SuperSecretPassword"
    }
}

You would pass that into the content body of the payload in your POST request as this:

{
    "query": <GraphQL query from above as a string>,
    "variables": <JSON object from above>
}

If you want a deeper explanation, you can check out Scaphold's Docs for updating data to help you structure your API.

Hope this helps!

like image 113
vince Avatar answered Oct 22 '22 09:10

vince