Apollo-tooling: error: Validation of GraphQL query document failed

Created on 28 Dec 2017  路  24Comments  路  Source: apollographql/apollo-tooling

  • npm install -g apollo-codegen
  • schema.graphql
  • apollo-codegen introspect-schema schema.graphql --output schema.json
  • apollo-codegen generate **/*.graphql --schema schema.json --target typescript --output operation-result-types.ts
.../ivan/github/demo/schema.graphql: The Comment definition is not executable.
.../ivan/github/demo/schema.graphql: The ContentFormatEnum definition is not executable.
.../ivan/github/demo/schema.graphql: The Email definition is not executable.
.../ivan/github/demo/schema.graphql: The ImageSizeEnum definition is not executable.
.../ivan/github/demo/schema.graphql: The ImageType definition is not executable.
.../ivan/github/demo/schema.graphql: The ImageTypeEnum definition is not executable.
.../ivan/github/demo/schema.graphql: The Node definition is not executable.
.../ivan/github/demo/schema.graphql: The Query definition is not executable.
.../ivan/github/demo/schema.graphql: The SearchResultType definition is not executable.
.../ivan/github/demo/schema.graphql: The Story definition is not executable.
.../ivan/github/demo/schema.graphql: The StoryAffordancesEnum definition is not executable.
.../ivan/github/demo/schema.graphql: The Url definition is not executable.
.../ivan/github/demo/schema.graphql: The User definition is not executable.
error: Validation of GraphQL query document failed

Most helpful comment

I think the confusion here is that some people (myself included) want typescript interfaces for all the types in the graphql schema, not just for the query and mutation operations used by the client. That way we can use generated types on the server-side to add types to our resolvers. Not sure if there is a tool out there that does this?

Edit: https://github.com/dotansimha/graphql-code-generator

All 24 comments

I'm seeing this issue as well.

You have to make sure that schema.graphql is not part of the files that are treated as query documents. With **/*.graphql, all .graphql files will be selected, so you should probably make that more specific and/or move the schema file.

Woops. I didn't pay enough attention to the OP's errors. I'm seeing the following:

```.../src/graphql/types/user.graphql: The UserType definition is not executable.
.../src/graphql/types/user.graphql: The Query definition is not executable.
.../src/graphql/types/user.graphql: The Mutation definition is not executable.
.../src/graphql/types/user.graphql: The User definition is not executable.

Here is the user.graphql file:

enum UserType {
ISA,
REA,
ADMIN
}

type Query {
currentUser: User
}

type Mutation {
login(email: String!, password: String!): User
}

type User {
_id: ID
email: String
type: UserType
jwt: String
}
```

Do you see anything I'm doing wrong?

Command ran: apollo-codegen generate src/**/*.graphql --schema src/data/schema.json --target typescript --output operation-result-types.ts

@triedal It鈥榮 the same error as the OP has. Make sure you do not include your schema file (user.graphql) into the apollo-codegen glob

I have the same issue, too.

  1. npm install -g apollo-codegen
  2. apollo-codegen introspect-schema http://localhost:13083/graphql --output schema.json
  3. get-graphql-schema http://localhost:13083/graphql > schema.graphql
  4. apollo-codegen generate schema.graphql --schema schema.json --target typescript --output operation-result-types.ts

I used https://github.com/graphcool/get-graphql-schema to download schema.graphql
I only have two files in directory : schema.graphql and schema.json

Did I do anything wrong?

位 apollo-codegen generate schema.graphql --schema schema.json --target typescript --output operation-result-types.ts
.../GraphQL Spec\schema.graphql: The schema definition is not executable.
.../GraphQL Spec\schema.graphql: The AllBeneficiaryResult definition is not executable.
.../GraphQL Spec\schema.graphql: The AllTradeOrderResult definition is not executable.
.../GraphQL Spec\schema.graphql: The AssessmentAnswerOption definition is not executable.

facing the same issue as @alonstar introduced... Can someone clarify the way of using codegen, please?

@alonstar @Charius: codegen generates types for your operations, so it takes query documents as input (either .graphql files or JavaScript/TypeScript with gql tags). The error you're seeing is because it is trying to interpret schema.graphql as a query document.

@martijnwalraven thanks for the info about gql tags, I'll try it out. And yes, I guess I'm blind or something https://github.com/apollographql/apollo-codegen#gql-template-support

I think that it'll be nice to have a simple example of generate command using --tag-name option, or maybe mentioning that there is an ability to generate from .ts .js files, isn't it?

@martijnwalraven : my graphql server is working , how do I check my schema.graphl is correct ? thanks for your help.

@martijnwalraven apollo-codegen generate schema.graphql --schema schema.json --target typescript --output operation-result-types.ts also does not work. Executable definitions this validation should be pass for with operation and fragment and fail for with schema definition if I'm not mistaken.

@listepo The input to apollo-codegen should be query documents, not the schema (that is specified using --schema).

Thanks, so just to be clear, we can't use apollo-codegen to extract typescript types from a backend schema.graphql. It is only use with query document, is that right ?

You use both a schema and query documents, but the generated types are query-specific (so they accurately type the results returned from the server).

Not sure if anybody else is here because of this but there are app frameworks like graphcool that let you have graphql files in your project that are either queries or extension to the schema. If you structure your project by functionality you will have a mixture of both in the same folder. This means the usual way of distinguishing between schema or query by path dosen't work

The way we worked around this was to simply be more specific via the file extension, using *.schema.graphql and *.query.graphql.
This allows us to have separate ts generation scripts for schema and query files. and have just 3 scripts doing all the work:

  • generate types from schema (not apollo-codegen as it can only handle queries)
  • generate types from queries (graphql)
  • generate types from queries (gql)

Updated, to make clear for type generation of schema another tool has to be used.

@alonstar
I have tried the same steps that you, but I have noticed that this tool is to generate a file of types from different querys of grahpql.

This works for me

  1. npm install -g apollo-codegen
  2. apollo-codegen introspect-schema http://localhost:13083/graphql --output schema.json
  3. wirte custom query file
    getCategory.graphql
query getCategory{
  category(id: 1) {
    name
  }
}
  1. apollo-codegen generate getCategory.graphql --schema schema.json --target flow --output graphql.flow.js
    the output it's like this
export type getCategoryQuery = {|
  category: ? {|
    name: ?string,
  |},
|};
  1. Use this types in your code
// @flow
import ApolloClient from 'apollo-boost'
import gql from 'graphql-tag'
import type { getCategoryQuery } from "./graphql.flow.js"
const client = new ApolloClient()

export const fetchCategory = (id: number) => (
  client.query({
    query: gql`{
      category(id: ${id}) {
        name
      }
    }
  `}): Promise<{
    data: getCategoryQuery;
  }>
)

Now for the function fetchCategory the autocomplete or types validations works

@MiguelSavignano you don't need your getCategory.graphql as apollo-codegen can extract your graphql query from your JS source.

Maybe it won't work in your case as you are using a ${id} expression instead of passing the variables to client.query. Try it like this:

  client.query({
    query: gql`
      query getCategory($id: Int!) {
        category(id: $id) {
          name
        }
      }`, 
    variables: { id }
  }): Promise<{
    data: getCategoryQuery;
  }>

Anyone found a resolution for this issue? I have a Post.graph:

type Post {
    id: Int!
    name: String!
    text: String
}

And I have an error when I run:

$ apollo-codegen generate Post.graphql --schema schema.json --target typescript --output operation-result-types.ts
.../www/project/Post.graphql: The Post definition is not executable.
error: Validation of GraphQL query document failed

@pleerock your Post.graph is supposed to contain a query against the schema in schema.json. e.g.

query MyPostQuery {
  post {
    id
    name
  }

I think the confusion here is that some people (myself included) want typescript interfaces for all the types in the graphql schema, not just for the query and mutation operations used by the client. That way we can use generated types on the server-side to add types to our resolvers. Not sure if there is a tool out there that does this?

Edit: https://github.com/dotansimha/graphql-code-generator

I found that i was using what apollo considers anonymous operation. Which i fixed by adding a name to the query.

Before

query {
  example {
    id
  }
}

After

query Example{
  example {
    id
  }
}

@ibigpapa This was a shocking experience, I've just wasted three hours of my life until I found your hint. Would it be possible to improve the error message "Validation of GraphQL query document failed" to give some hints about the problem.

Looks like codegen doesn't do what I thought it would...

In my case i needed to update the query .graphql file as well as the schema.json

Was this page helpful?
0 / 5 - 0 ratings