I am practicing using directives with Graphql in Nodejs. When in the Playground I want to use the @include directive, the Graphql client tells me: "Unknow directive include". This is my code:
query getChar($id: ID!, $withGames: Boolean!) {
getCharacter(_id: $id) {
_id
name
race
games @include(if: $withGames) {
_id
title
}
image
}
}
In my Query Variables I have:
{
"id": "5f945a7b24c84f35ecfae633",
"withGames": false
}
but I get the following error:
{
"error": {
"errors": [
{
"message": "Unknown directive \"@include\".",
"locations": [
{
"line": 6,
"column": 11
}
],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED",
"exception": {
"stacktrace": [
"GraphQLError: Unknown directive \"@include\".",
" at Object.Directive (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\validation\\rules\\KnownDirectivesRule.js:56:29)",
" at Object.enter (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\language\\visitor.js:323:29)",
" at Object.enter (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\utilities\\TypeInfo.js:370:25)",
" at visit (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\language\\visitor.js:243:26)",
" at Object.validate (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\graphql\\validation\\validate.js:69:24)",
" at validate (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\apollo-server-core\\src\\requestPipeline.ts:510:14)",
" at Object.<anonymous> (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\apollo-server-core\\src\\requestPipeline.ts:296:32)",
" at Generator.next (<anonymous>)",
" at fulfilled (C:\\Users\\MSI\\projects\\Graphql\\cursobasico\\node_modules\\apollo-server-core\\dist\\requestPipeline.js:5:58)",
" at processTicksAndRejections (internal/process/task_queues.js:97:5)"
]
}
}
}
]
}
}
What am I doing wrong?
It was a version problem of the graphql-tools
. Checking the node_modules/graphql-tools/package.json
file I observed that I was using version 4. I found here that version 4 had problems with the directives and the use of version 5 was recommended. This is what I did:
npm i graphql-tools@5.0.0
I restarted the server and refresh the Playground page
and voila, I already have the @include
directive and all the others available.