Apollo-codegen output is empty
I ran into a situation where apollo-codegen is not successfully generating typescript code.
For graphql ( generated/schema.graphql
) file :
type Author {
id: Int!
firstName: String
lastName: String
posts: [Post]
}
type Post {
id: Int!
title: String
author: Author
votes: Int
}
Then I run:
$apollo-codegen introspect-schema generated/schema.graphql --output generated/schema.json
this generates ./generated/schema.json
which appears to contain the relevant information (I see information about the Author and its properties, as well as the Post and its properties).
Then I ran
$apollo-codegen generate generated/schema.graphql --schema generated/schema.json --target typescript
and get (effectively) empty output.
// This file was automatically generated and should not be edited.
/* tslint:disable */
/* tslint:enable */
I tried to generate .swift files too, with similar empty output.
Apollo codegen version:
"apollo-codegen": "^0.11.2",
Does anyone see what I am doing wrong?
source to share
I work with apollo-codegen
. Glad to hear you give it a try!
You don't see any result because apollo-codegen generates types based on GraphQL operations (query, mutation) in your project - not based solely on your schema types.
In our experience, it is very rare that you submit a request for a full GraphQL type. Instead, we found that the types based on graphql operations are the most useful.
For example, given the types you provided, you could write a query:
query AuthorQuery {
authors {
firstName,
lastName
}
}
The type that will be generated (and which you probably want to use in code that uses the results of this query,
type AuthorQuery = {
authors: Array<{
firstName: string,
lastName: string
}>
}
Note how you would use a type AuthorQuery
in your React component (or similar), whereas you would not use a type Author
as it would include more fields than you actually requested.
If you, however, have a use case for a 1: 1 type from your graphql to typescript schema, follow the file asking about the project itself and I would be happy to discuss there :)
source to share