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.
Related
I've been trying to fix this bug and I haven't been able to figure it out. I know this is similar to some of the other threads I've seen on here but I've verified it's not a typo and that the issue isn't with an incorrect data type. Would really appreciate the help and for someone to point out what i'm missing here. Thanks!
Edit: Here's the stack trace
{
"errors": [
{
"message": "Syntax Error: Expected Name, found Int \"-12\".",
"locations": [
{
"line": 8,
"column": 27
}
],
"extensions": {
"code": "GRAPHQL_PARSE_FAILED",
"exception": {
"stacktrace": [
"GraphQLError: Syntax Error: Expected Name, found Int \"-12\".",
" at syntaxError (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/error/syntaxError.js:15:10)",
" at Parser.expectToken (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:1413:40)",
" at Parser.parseName (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:98:22)",
" at Parser.parseObjectField (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:591:21)",
" at Parser.item (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:575:21)",
" at Parser.any (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:1485:26)",
" at Parser.parseObject (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:580:20)",
" at Parser.parseValueLiteral (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:461:21)",
" at Parser.item (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:553:20)",
" at Parser.any (/Users/vn/Documents/Blockchain/fabric/blockchain-fabric-api/node_modules/graphql/language/parser.js:1485:26)"
]
}
}
}
]
}
You have creationTime as a string without quotes, so you need quotes
change it to this:
creationTime: "2007-12-03R10:15:30Z",
Is there a way when using Lighthouse PHP to get the GraphQL validation error to appear side by side with the Laravel validation errors? Currently the graph QL validations cause a different type of error, before the Laravel error are run.
For example if I submit the following mutation:
mutation register(
password: 123456
) {
id
}
I will get back just an error back
{ "message": "Variable username not present. Expected type String",
"extensions": {
"category": "graphql"
}
}
Then when I submit:
mutation register(
username: somethingStupid
password: 123456
) {
id
}
Then I will get this error:
{ "message": "Validation failed for the field [register].",
"extensions": {
"validation": {
"username": [
"Username must be unique."
]
},
"password": [
"Password must contain letters and numbers"
]
},
"category": "validation"
},
}
For my front end I'd like them only to have to expect a single error bag, but this seems to require them to expect two different types of errors. Assuming my Laravel validations cover the schema definition, I would like for them never to see the GraphQl errors. Or am I missing the concept of GraphQL somehow?
I have been using Chaskiq for some work but ran into an error.
I built from source on Ubuntu 20.04.
I got the graphql part working and query requests work. However, whenever I make a mutation request I seem to get this response:
{
"errors": [
{
"message": "Data not found",
"data": {}
}
]
}
Example mutation request I sent to get the response above:
mutation updateAppUser($appKey: String!, $options: Json!, $id: Int!) {
updateAppUser(appKey: $appKey, options: $options, id: $id) {
appUser {
id
name
email
}
}
}
I have the variables Query Variables as below:
{
"appKey": <My_APP_KEY>,
"options": {
"name": <Custom_Name>
},
"id": <My_ID>
}
Please help me know the solution to the problem.
Data not found is returned when the server does not found any record.
basically ActiveRecord::RecordNotFound , so you are probably trying to find the wrong record. check the logs to see what's happening
In a project where I'm sourcing data from NetlifyCMS I need to extend the GraphQL schema of Gatsby because there are optional fields in my CMS, which would cause errors trying to query non-existing data from GraphQL.
The below code extends the GraphQL types to always include the optional fields
// gatsby-node.js
// ADD OPTIONAL TYPES
// note: I have also tried exports.sourceNodes
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
type SettingsJson implements Node {
tags: String
name: String
phone: String
email: String
additional_info: [SettingsJsonAdditional_info]
}
type SettingsJsonAdditional_info {
type: String
text: String
}
`
createTypes(typeDefs)
}
Sourcing and transforming the tags, name, phone and email work as expected. Queries return null if an optional field has not been filled out, and the query returns the correct value after being set. However, querying for additional_info always returns null even when containing data.
This is the json-file generated by NetlifyCMS:
{
"name": "Name Nameson",
"phone": "+41 1234 5678",
"email": "mail#example.com",
"additional-info": [
{
"type": "Booking",
"text": "Booker McBookerson <book#book.com>"
}
]
}
The following GraphQL query shows that the data is not being transformed properly when extending the GraphQL schema myself.
Query
query {
file(relativePath: {eq: "settings/contacts.json"}) {
internal {
content
}
childSettingsJson {
name
phone
email
additional_info {
type
text
}
}
}
}
Response
{
"data": {
"file": {
"internal": {
"content": "{\n \"name\": \"Name Nameson\",\n \"phone\": \"+41 1234 5678\",\n \"email\": \"mail#example.com\",\n \"additional-info\": [\n {\n \"type\": \"Booking\",\n \"text\": \"Booker McBookerson <book#book.com>\"\n },\n {\n \"type\": \"Booking2\",\n \"text\": \"Booker2 McBookerson <book#book.com>\"\n }\n ]\n}"
},
"childSettingsJson": {
"name": "Name Nameson",
"phone": "+41 1234 5678",
"email": "mail#example.com",
"additional_info": null
}
}
},
"extensions": {}
}
When the types are inferred by the transformer plugin itself I get the expected data when querying
// ...
"additional_info": [
{
"type": "Booking",
"text": "Booker McBookerson <book#book.com>"
}
]
// ...
This example uses json files with gatsby-transformer-json. I have tried with gatsby-transformer-yaml too with the same results.
Is it possible to add my array of SettingsJsonAdditional_info to the schema to get the "optional field" functionality I'm looking for?
One kind of hacky solution I have found is to make the transformer plugin infer the types by adding a dummy-file that will be sourced and transformed along with "real" files.
// dummy.json
{
"name": "dummy",
"phone": "dummy",
"email": "dummy",
"tags": "dummy",
"additional-info": [
{
"type": "dummy",
"text": "dummy"
}
]
}
This file can be hidden from NetlifyCMS (by simply not including a UI entry for it in the config.yml file of NetlifyCMS. It will guarantee that you can always query for the fields included in this file without getting GraphQL "field doesn't exist" errors.
I'm trying to test the GraphQL server I built, by sending GraphQL queries to the server using Postman.
It works when I'm using raw radio button, but when I'm trying to use GraphQL radio button, it returns "message": "Syntax Error: Expected Name, found String \"query\"".
I have tried to change the syntax: mainly add or delete curly braces but nothing happened.
The query I sent in raw mode (working):
{
person(id:"123456789") {
personal_info {
address
}
}
}
The query I sent in GraphQL mode:
QUERY:
query getPerson ($id: String){
person(id: $id){
personal_info {
address
}
}
}
GRAPHQL VARIABLES:
{
"id": "123456789"
}
I expect to get the data I asked for, but I get the error message:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"query\"",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
}
I had the same problem. During researching I have found the next answer on stackoverflow, thanks #gbenga_ps.
Resolved by adding the correct header to Postman request:
Body of request should be something like next:
{
courses {
title
}
}
If incorrect content-type set, error like next happened:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"query\"",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
}