GraphQLError: Syntax Error: Expected Name, found "$"

Viewed 150

I am new to graphQL and after trying to run a React app, I get the following error:

I'm getting the error: GraphQLError: Syntax Error: Expected Name, found "$".

Here are my graphql queries/mutations.

This works fine:

gqlMutation = {
        mutation: gql`
      mutation{
              createMaps(input: [{
                            client_id: "7b8c903a-3402"
                            text: "Some Text"
                   }])
              {
                  info{
                    nodesCreated
                    relationshipsCreated
                  }
                  Maps{
                    uid
                  }
              }
            }`
    }

 const res = await client.mutate(gqlMutation);

But when I try to pass variables then it throws an error

client_id="7b8c903a-3402";  
text= "Some Text";

 const preppedQuery =`
    mutation{ 
createMaps(input: [{
                            $client_id: String!,
                            $text: String!
                   }])
              {
                  info{
                    nodesCreated
                    relationshipsCreated
                  }
                  Maps{
                    uid
                  }
              }
            }`
    }
const gqlMutation = {     
      mutation: gql`${preppedQuery}`,
      variables: 
      {
        client_id,
        text
      }
    };
     const res = await client.mutate(gqlMutation);
1 Answers

This worked for me. I was declaring variables in the wrong place. Need to declare them by giving a name to Mutation and then declare them inside. Also, need to remove extra '{' after mutation and '}' at the end.

client_id="7b8c903a-3402";  
text= "Some Text";

const preppedQuery = gql`
        mutation CreateMaps(
          $client_id: String!
          $text: String!
        ) {
          createMaps(
            input: [{ client_id: $client_id,  text: $text }]
          ) {
            info {
              nodesCreated
              relationshipsCreated
            }
            maps {
              uid
            }
          }
        }
      `;
      const gqlMutation = {
        mutation: gql`
          ${preppedQuery}
        `,
        variables: {
          client_id,
          text,
        },
      };
      const res = await client.mutate(gqlMutation);
Related