question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

apolloClient.mutate variables not working

See original GitHub issue

Intended outcome: Variable merged into the mutation

Actual outcome: Status: 400 error with variable, and the mutation works when hard-coding the value in the mutation:

 sendMsgNotWork() {
      apolloClient.mutate({
        variables: { text: "hello" },
        mutation: gql`
          mutation {
            addComment(text: $text) {
              id
              text
            }
          }
        `,
        
      })
      .then(result => { console.log(result) })
      .catch(error => { console.log(error) });
    },

    sendMsgWorks() {
      apolloClient.mutate({
        variables: { text: "hello" },
        mutation: gql`
          mutation {
            addComment(text: "Hello") {
              id
              text
            }
          }
        `,
        
      })
      .then(result => { console.log(result) })
      .catch(error => { console.log(error) });
    }

How to reproduce the issue: https://github.com/jarnojellesma/graphql-experiment

Version

  • apollo-client@2.0.4

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

51reactions
jdmunrocommented, Dec 26, 2017

I tried your example project, this seems to work fine:

apolloClient.mutate({
  variables: { text: "hello" },
  mutation: gql`
    mutation AddComment($text: String!){
      addComment(text: $text) {
        id
        text
      }
    }
  `,
  
})
.then(result => { console.log(result) })
.catch(error => { console.log(error) });
5reactions
NicholasGWKcommented, Mar 16, 2018

@jarnojellesma Any insight on the cause of this? Seems to be happening to me right now 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutations in Apollo Client - Apollo GraphQL Docs
Here, we use the variables option to provide the values of any GraphQL variables that our mutation requires (specifically, the type of the...
Read more >
Passing a variable to GraphQL mutation using ApolloClient ...
I'm trying to figure out how to run mutations using Apollo Client. Here's the mutation I'm trying to run: export const CREATE_POST =...
Read more >
GraphQL Mutations and Caching using Apollo Client
This mutation adds new fields, and the simple caching approach will not work to update the cache. const CREATE_SESSION = gql`
Read more >
Mutations – Angular - GraphQL Code Generator
The mutation name with arguments ( submitRepository ), which represents the ... variables: { repoFullName: 'apollographql/apollo-client' } ...
Read more >
Apollo client.mutate for GraphQL mutation update - Hasura
+ variables: {id: todo.id, isCompleted: !todo.is_completed},. + optimisticResponse: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found