-2

I'm using react, apollo, graphql, hasura, postgres as my stack to interact with the database. I think my issue is something small, so I'll just focus on the part that's not working rather than posting the whole code. Thanks.

Error: GraphQL error: unexpected variables in variableValues: birthday
    at new ApolloError (bundle.esm.js:63)
    at Object.next (bundle.esm.js:1004)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at bundle.esm.js:866
    at Set.forEach (<anonymous>)
    at Object.next (bundle.esm.js:866)
    at notifySubscription (Observable.js:135)
    at onNotify (Observable.js:179)
    at SubscriptionObserver.next (Observable.js:235)
    at bundle.esm.js:76
variables{ id: 2324324, name: "Fred", birthday: "1991-01-11" }

If i remove birthday the query works.

enter image description here

Here is the function

const onUpdateUser = (options) => {
    updateUser({
      variables: Object.assign({ id: userId }, options),
      optimisticResponse: {
        __typename: "mutation_root",
        update_users: {
          __typename: "users_mutation_response",
          affected_rows: 1,
          returning: [
            {
              __typename: "users",
              id: userId,
              ...options,
            },
          ],
        },
      },
    });
  };

input {birthday: '1991-01-11'}

Frederick Mfinanga
  • 1,045
  • 1
  • 10
  • 27
  • Please provide more of the javascript/Apollo code causing this error - this is not a Hasura issue, as the "1991-01-11" is a valid date format according to Postgres (tested in Heroku instance w/same schema). – avimoondra Apr 24 '20 at 06:48
  • I added the function code. – Frederick Mfinanga Apr 26 '20 at 01:58
  • Thanks, please provide the gql mutation corresponding to updateUser. Seems like the error is reported by Hasura, on further inspection: https://github.com/hasura/graphql-engine/blob/master/server/src-lib/Hasura/GraphQL/Validate.hs#L81, but doesn't mean it's not preventable upstream. – avimoondra Apr 26 '20 at 07:46

1 Answers1

0

So without looking at your graphql query, I think you may be thinking of it a little bit off.

You can't dynamically add non-existent variables to a graphql query. The error is telling you that you are trying to add a variable that doesn't exist in your query

i.e. this with NOT work because you haven't defined birthday.

mutation updateUser(
    $userId: Int!
    $birthday (UNDEFINED)
  ) {
    rest of query...
  }

If you need to add a dynamic amount of variables, you could do something like this.

React Code

const onUpdateUser = (options) => {
    updateUser({
      variables: {
        userId,
        userVariables: options
      },
      optimisticResponse: {
        __typename: "mutation_root",
        update_users: {
          __typename: "users_mutation_response",
          affected_rows: 1,
          returning: [
            {
              __typename: "users",
              id: userId,
              ...options,
            },
          ],
        },
      },
    });
  };

GraphQL mutation

mutation updateUser(
    $userId: Int!
    $userVariables: user_set_input!
  ) {
    update_user(
      where: { id: { _eq: $userId} }
      _set: $userVariables
    ) {
      affected_rows
    }
  }

https://hasura.io/docs/1.0/graphql/manual/mutations/update.html

moto
  • 946
  • 10
  • 27