webpack-graphql-server icon indicating copy to clipboard operation
webpack-graphql-server copied to clipboard

feature req: add tooling that generates types from schema

Open Birowsky opened this issue 8 years ago • 13 comments

Have you checked any of these projects?

  • Apollo GraphQL code generator: https://github.com/apollographql/apollo-codegen
  • GQL2TS: https://github.com/avantcredit/gql2ts
  • GraphQL To Typescript: https://github.com/3VLINC/graphql-to-typescript
  • graphql-typewriter: https://github.com/nknapp/graphql-typewriter

Is there one you are leaning towards?

Birowsky avatar Aug 06 '17 11:08 Birowsky

Hey @Birowsky

The generator ive liked the most is https://github.com/dotansimha/graphql-code-generator But i have a task of making webpack plugin that will capture all required .gql files and add one virtual model for it.. If you want to integrate it as build step in the meanwhile you are welcome :)

DxCx avatar Aug 06 '17 13:08 DxCx

Hey mister!

I couldn't figure out how to do .json -> .d.ts with apollo-codegen. They only document how to accomplish that for clients.

The types output file is empty: https://stackoverflow.com/q/45534529/592641

With graphql-typewriter however, all I do is: graphql-typewriter. No joke. The interfaces module is created right there next to the .graphqls file. I later discovered that the graphql version it produces types for, is old. So they don't match.

Birowsky avatar Aug 06 '17 14:08 Birowsky

  • GQL2TS types are scarse
  • graphql-typewriter is great, but two graphql versions behind, so types are wrong.
  • GraphQL To Typescript is one version behind and throws weird error

What step would you next take?

Birowsky avatar Aug 06 '17 20:08 Birowsky

hey @Birowsky have you seen graphql-code-generator i've suggested?

DxCx avatar Aug 06 '17 22:08 DxCx

Holy donkey! I hadn't noticed that you actually recommended a different package:} Trying it asap.

Birowsky avatar Aug 06 '17 22:08 Birowsky

Sad news. For this schema:

schema {  query: Query }
type Query {  onion(peal:String!):Float }

I got these types:

export interface Query {
  onion: number | null;
}
export interface User {
  id: string | null;
  email: string | null;
}

Birowsky avatar Aug 06 '17 23:08 Birowsky

Hey @Birowsky, Can you open a branch with that? I wanna take a look since i do have good experiance with that in the past

DxCx avatar Aug 07 '17 05:08 DxCx

We don't need a branch for that. This is the schema.graphqls file:

schema {
  query: Query
}
type Query {
  someQuery(inputArg:String):Float
}

these are the commands that I run on it:

apollo-codegen introspect-schema schema.graphqls --output schema.json
gql-gen --file schema.json --template typescript --out schema.d.ts

here's the resulting schema.d.ts:

export interface Query {
  someQuery: number | null; 
}
export interface SomeQueryQueryArgs {
  inputArg: string | null; 
}

on the other hand, here's what graphql-typewriter gives us:

/* tslint:disable */

export namespace schema {
    export type GraphqlField<Args, Result, Ctx> = Result | Promise<Result> |
        ((args: Args, context: Ctx) => Result | Promise<Result>)

    export interface Query<Ctx> {
        someQuery?: GraphqlField<{inputArg: string}, number | undefined, Ctx>
    }

    export const defaultResolvers = {

    }
}

the types are way more complete, but they are incorrect (probably because the tool is old)

Birowsky avatar Aug 07 '17 05:08 Birowsky

Gql file here is not a valid schema..

DxCx avatar Aug 07 '17 09:08 DxCx

sorry, i guess i was in a rush. edited.

Birowsky avatar Aug 07 '17 09:08 Birowsky

Could you open an issue with the exact errors in graphql-typewriter? We'd need to know what exactly is wrong.

Although the project has not had an update for a while, it is not abandoned...

nknapp avatar Aug 07 '17 15:08 nknapp

@nknapp just mentioned it here. Wrong arguments in the resolvers.

Birowsky avatar Aug 07 '17 16:08 Birowsky

@dotansimha can you please help @Birowsky integrating type generation into this example repo?

DxCx avatar Aug 08 '17 07:08 DxCx