4

I'm running into a situation in which apollo-codegen is not successfully generating typescript code.

For the graphql file (generated/schema.graphql):

type Author {
  id: Int!
  firstName: String
  lastName: String
  posts: [Post]
}

type Post {
  id: Int!
  title: String
  author: Author
  votes: Int
}

I then run :

$apollo-codegen introspect-schema generated/schema.graphql --output generated/schema.json

this generates a ./generated/schema.json that appears to contain the relevant information (I see information about Author and its properties, and Post and its properties).

I then run

$apollo-codegen generate generated/schema.graphql --schema generated/schema.json --target typescript and get an (effectively) empty output.

//  This file was automatically generated and should not be edited.
/* tslint:disable */
/* tslint:enable */

I've tried generating .swift files as well, with similar empty output.

Apollo codegen version is:

"apollo-codegen": "^0.11.2",

Anyone see if what I'm doing wrong?

1 Answer 1

3

I'm a collaborator on apollo-codegen. Happy to hear that you're giving it a try!

You're not seeing any output because apollo-codegen generates types based on the GraphQL operations (query, mutation) in your project -- not based solely on the types in your schema.

In our experience, it's very rare that you would send a query for a full GraphQL type. Instead, we have found types based on graphql operations to be the most useful.

For instance, given the types you've provided, you might write a query:

query AuthorQuery {
  authors {
    firstName,
    lastName
  }
}

The type that would get generated (and that you'd probably want to use in code that consumes the results of this query, is

type AuthorQuery = {
  authors: Array<{
    firstName: string,
    lastName: string
  }>
}

Notice how you would use the AuthorQuery type in your React component (or similar) whereas you wouldn't use an Author type since it would include more fields than you've actually requested.

If you do however have a use-case for a 1:1 type from your graphql schema to typescript, do file an issue on the project itself and I'd be happy to discuss there :)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but it looks like it gets filtered when I generate the schema. Could you take a look? github.com/benjaminabbitt/graphql-experiments
I had trouble understanding this too since the example is "--output scema.ts" but what is in fact outputted are the query types, not the schema types. Perhaps naming the output file "query-types.ts" in the example would make it clearer.
Do you have any doc on what is in that .graphql file ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.