0

I have a Graphql schema that looks like this:

type Mutation {
  createUploadUrl(
    input: CreateUploadUrl!
  ): MyResponse!
}

input CreateUploadUrl {
  deploymentId: ID!
}

I'm new to Graphql and am getting lost when trying to work out what my query should look like when I submit it via fetch:

fetch('https://example.com', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        query: `
            mutation($input: ) {
               createUploadUrl($input) {
                 __typename,
                 // my values I want to retrieve
                 // ...
               }
             }`,
        variables: {
            input: {deploymentId: "XXX"}
        }
    }),
})
    .then((res) => res.json())
    .then((result) => console.log(result));

The above gives me an error: `message: "Syntax Error: Expected Name, found "$"."

How am I supposed to specify a query or mutation that requires an object as a variable? `

1 Answer 1

1

You just need to define your parametrized function and wrap the effective mutation. In the example below the mutation MyMutation takes as param a $arg wich is a CreateUploadUrl!. Then pass the $arg to the input of the mutation, the invocation becomes:

mutation MyMutation($arg: CreateUploadUrl!) {
  createUploadUrl(input: $arg) {
  __typename,
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.