Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apolloClient.mutate variables not working #2762

Closed
jarnojellesma opened this issue Dec 20, 2017 · 10 comments
Closed

apolloClient.mutate variables not working #2762

jarnojellesma opened this issue Dec 20, 2017 · 10 comments

Comments

@jarnojellesma
Copy link

jarnojellesma commented Dec 20, 2017

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
@jdmunro
Copy link
Contributor

jdmunro commented 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) });

@NicholasGWK
Copy link

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

@hwillson
Copy link
Member

As mentioned in #2762 (comment), the reproduction seems to work properly when tested. If this continues to be a problem, please post back with an updated reproduction that shows the issue happening. Thanks!

@masiamj
Copy link

masiamj commented Jun 25, 2019

@jarnojellesma I'm running into the same issue. Did you ever resolve this?

@zaid-zaheer-shaikh
Copy link

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) });

If i wanted to add a custom type in variables, like instead of string, i wanted to add a custom object type, how will i add. $text:String, instead, i want to put something like $text:Message, how do i define that on the client

@shkrt
Copy link

shkrt commented Jan 26, 2020

This is because variables need to be handled on the server side. Nothing to do with client, same as apollographql/react-apollo#164 @masiamj @marcotc

@osamaabid03
Copy link

Thankx @vigoss6666

@nicreichert
Copy link

nicreichert commented Aug 2, 2022

I'm having this issue both on mutate and query.

the code I use is:

loadUsers = (variables?: GetUsersVariables['input']) =>
   client
      .query<GetUsers>({
        variables,
        query: getUsersQuery,
        fetchPolicy: 'network-only',
      })
      .then(response => this.setState({ loading: false, users: response.data.users }))
      .catch(err => console.error(err))

and

  createUser = (variables: CreateUserVariables['input']) =>
    client
      .mutate<CreateUser>({
        variables,
        mutation: createUserMutation,
      })
      .then(response => this.setState({ userLogin: response.data.createUser }))

I log the variables parameter and the method is properly receiving the variables, however, all the requests I make have empty object on variables, like so:
Screenshot 2022-08-02 at 11 11 10

Could it be my client setup?

Here's how I'm setting it up:

import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'

const link = new HttpLink({ uri: 'http://localhost:4000/graphql' })

export const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
  defaultOptions: {
    query: {
      fetchPolicy: 'network-only',
      notifyOnNetworkStatusChange: true,
    },
  },
})

@victors1681
Copy link

late response, but that happens when the variables in the client variables: { .... } do not match with the variables with the query/mutation ... also are case sensitive.

I'm having this issue both on mutate and query.

the code I use is:

loadUsers = (variables?: GetUsersVariables['input']) =>
   client
      .query<GetUsers>({
        variables,
        query: getUsersQuery,
        fetchPolicy: 'network-only',
      })
      .then(response => this.setState({ loading: false, users: response.data.users }))
      .catch(err => console.error(err))

and

  createUser = (variables: CreateUserVariables['input']) =>
    client
      .mutate<CreateUser>({
        variables,
        mutation: createUserMutation,
      })
      .then(response => this.setState({ userLogin: response.data.createUser }))

I log the variables parameter and the method is properly receiving the variables, however, all the requests I make have empty object on variables, like so: Screenshot 2022-08-02 at 11 11 10

Could it be my client setup?

Here's how I'm setting it up:

import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client'

const link = new HttpLink({ uri: 'http://localhost:4000/graphql' })

export const client = new ApolloClient({
  link,
  cache: new InMemoryCache(),
  defaultOptions: {
    query: {
      fetchPolicy: 'network-only',
      notifyOnNetworkStatusChange: true,
    },
  },
})

@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2023

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants