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
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
After
@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.
facing the same issue as @alonstar introduced… Can someone clarify the way of using codegen, please?
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.@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
npm install -g apollo-codegen
apollo-codegen introspect-schema http://localhost:13083/graphql --output schema.json
apollo-codegen generate getCategory.graphql --schema schema.json --target flow --output graphql.flow.js
the output it’s like thisNow for the function fetchCategory the autocomplete or types validations works
@triedal It‘s the same error as the OP has. Make sure you do not include your schema file (user.graphql) into the apollo-codegen glob