Graphql ClientError: Variable `userId` is required

Viewed 93

For a certain GraphQL request (from another endpoint) I try to use graphql-request package. I know it's not a good practice and we should add that endpoint to our federated gateway in the feature. For the rest I use Apollo client to talk to my GraphQL server and fetch data in my React app.

In my app when the user isn't logged in the id variable returns null. When the user is logged in id returns a string.

I use useReactiveVar from Apollo Client to get the current user id. This all works fine.

Then from the other endpoint I need below query and try to use it with graphql-request:

  const endpoint = 'https://my-endpoint/graphql';

  const query = gql`
    query($userId: String!) {
      isEligible(userId: $userId)
    }
  `;

  const variables = {
    userId: id,
  };

  request(endpoint, query, variables).then((data2) => console.log(data2));

But now it throws a graphql error:

ClientError: Variable userId is required.: {"response":{"errors":[{"message":"Variable userId is required.

How to solve this?

0 Answers
Related