Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript codegen #16

Merged
merged 8 commits into from
Jun 20, 2018
Merged

typescript codegen #16

merged 8 commits into from
Jun 20, 2018

Conversation

stephen
Copy link
Contributor

@stephen stephen commented Jun 14, 2018

This PR adds typescript codegen support to the loader. The option can be enabled with a codegen block:

options: {
  schema: "./schema.json",
  codegen: {
    typescript: {
      passthroughCustomScalars: true,
      customScalarsPrefix: "gql_",
    },
  },
},

The loader will then:
a) Emit a QuerySpec object for the query as the default export, along with the input/output types:

export interface FilmsQueryVariables {
  k: string,
};

export interface FilmsQuery {
  film:  {
    // The title of this film.
    title: string | null,
  } | null,
};

export interface Spec<Result extends object, Input extends object> {
  query: string;
  result?: Result;
  variables?: Input;
}

const documentOutput = "query FilmsQuery { ... }";
const Spec: Spec<FilmsQuery, FilmsQueryVariables> = { query: documentOutput };

The types are generated using apollo-codegen.

b) Write a .graphql.d.ts file to the filesystem as a sibling to the original .graphql file. This is useful for letting the IDE provide a useful type for the query.

Users of the feature may want to continue to put a:

declare module "*.graphql";

somewhere, so that things don't freak out before a type generation pass occurs.

Todo

  • If variables don't exist, don't emit them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant