Variable "$userId" of type "ID" used in position expecting type "ID!"

Viewed 3524

I am trying to make a graphql request using the npm package graphql-request. I am discovering the use of template literals.

   async getCandidate(userId: number) {
            const query = gql`
            query($userId: ID){
              candidate(id: $userId){
                id _id source phone
              }
            }
            `
            const variables = {userId: "/api/candidates/" + userId}
            return await request(GRAPHQL_URL, query, variables)
        }

I am trying to use the usedId variable but I am having the error :

Variable "$userId" of type "ID" used in position expecting type "ID!".: {"response":{"errors":[{"message":"Variable \"$userId\" of type \"ID\" used in position expecting type \"ID!\".","extensions":{"category":"graphql"},"locations":[{"line":2,"column":9},{"line":3,"column":19}]}],"status":200},"request":{"query":"\n\t\tquery($userId: ID){\n\t\t  candidate(id: $userId){\n\t\t    id _id source phone\n\t\t  }\n\t\t}\n\t\t","variables":{"userId":"/api/candidates/1"}
1 Answers

You just need to modify this query($userId: ID){ to this query($userId: ID!){

It is because the schema defines that value for userId needs to be non-nullable.

Related