When attempting to execute a GraphQL request using the npm package graphql-request
, I am exploring 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 encountering an error while trying to utilize the usedId
variable :
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"}