On the homepage readme for the generate command, it says
The purpose of this command is to generate types for query and mutation operations made against the schema (it will not generate types for the schema itself).
Is there a way to generate types for the schema itself? (target output typescript). This can be very useful for server side development.
cc @dmurawsky
This is 100% key for me. We're switching to having all of our enums and types defined in GraphQL then generating them to TS from there. Makes a lot of sense to support this and have GraphQL as the source of truth.
I also would love this for server-side development.
I have some complex input types to queries and return types that it would be wonderful to have TypeScript schema.d.ts generated code for.
To clarify my wishlist item here, a common use case is to write resolvers that return objects the shape of the entire schema.
It would be great to auto-gen the types from the schema just like a query that included every field in the schema.
That would allow server-side resolve functions in typescript to have a "MyQueryResponse" type as it's return type and get all the goodness to ensure the object returned is the right shape for the resolve function to cause no runtime issues.
My current work-around is to write out queries for the entire schema and code-gen from that and copy that into a server-side types module...
@gaberudy
That would allow server-side resolve functions in typescript to have a "MyQueryResponse" type as it's return type and get all the goodness to ensure the object returned is the right shape for the resolve function to cause no runtime issues.
This is exactly what we are doing with a combination of apollo-codegen (to combine the .graphql files and produce the introspection json) and gql2ts (to generate a .d.ts file).
The maintenance of gql2ts has been a little shaky though (I've had builds break because it is locked to an out-of-date version of graphql), so it would be nice if apollo-codegen had this capability built in. Another alternative that I just found is graphql-code-generator. You might want to take a look at that as well.
@adamkl could you expand on what you mean by:
to combine the .graphql files and produce the introspection json
I've been trying to find a way to do write multiple .graphql files and merge them together so that we don't need the server running to generate the introspection schema.
What does your setup look like for your schema as well as combining the files together?
@clayne11
We keep all our .graphql files in a single folder and generate a namespaced .d.ts file from that using a simple node bin script. Its not pretty, but it works.
I share the exact reasons for needing this with others here - type-safe server-side resolvers derived from the actual schema definitions. Similar to @gaberudy, I'm currently hacking around this by defining a full fragment for each query, then merging client and server schema together (using this script basically as-is: https://gist.github.com/zhenwenc/09c9e78faefa4c35bdff6280a010ab81).
Take a look at this https://stradivario.github.io/gapi-cli/
Schema Wiki
I have made some interpretation on every restart the server introspect schema and create typings. I am trying to integrate also inside this CLI postgraphile really good project for creating and introspecting Schema definition based on PostgreSQL definition.
I'm missing this feature, too.
Is there a certain reason why this isn't part of apollo-codegen?
gql-gen does it very well:
gql-gen -f graphql.schema.json -t ts --out Server/graphql/types/generated.gen.ts
@iamdanthedev thx, you are right.
I was just wondering If it's a bad practice or there's a better way, since it's used so much less than apollo-codegen.
@martijnwalraven can you add it to the roadmap? Maybe you need help?
const typeDefs = `
type Blog {
name: string
}
`
const schema = makeExecutableSchema({
typeDefs,
resolvers,
})
But the generated TS file uses interface, which can't be added to typeDef. Anyone having this issue as well?
Just wanted to namedrop prismake/typegql. It doesn't implement the requested feature but can help to solve the problem from the other way around. So you write the typed classes to create the schema and then can use the types to do other stuff.
Another way to generate .d.ts files is via graphql-cli codegen with prisma-binding.
Example: https://github.com/prismagraphql/prisma-binding/issues/164#issuecomment-389808108
However, I've to say, this method is still far from ideal and we shouldn't need prisma-binding to generate the definition at the first place...
https://github.com/avantcredit/gql2ts/tree/master/packages/cli
Typescript
npm install -g gql2ts
gql2ts schema.graphql > types.s
Flow
npm i -g @gql2ts/language-flow
gql2ts -e @gql2ts/language-flow schema.graphql > types.js.flow
Also https://github.com/zth/graphql-generate-flow-schema-assets
Another project I cam across aiming to solve this problem: https://github.com/dangcuuson/graphql-schema-typescript
I ended up switching to TypeGraphql and have schema be generated from resolver implementations via decorators
https://github.com/19majkel94/type-graphql/
It works like a charm and actually a lot faster to work with than generating server side types from schemas.
I wrote a small package which allows you to generate typeDefs from decorated classes. It aims to do only 1 thing and to be used in combination with apollo-server. It can be found at https://www.npmjs.com/package/typescript-typedefs .
Most helpful comment
gql-gen does it very well:
gql-gen -f graphql.schema.json -t ts --out Server/graphql/types/generated.gen.ts