Type-graphql: Question: How to generate a schema.graphql file using type-graphql

Created on 14 May 2018  Â·  5Comments  Â·  Source: MichalLytek/type-graphql

I'm currently working on setting up a graphql server using AWS AppSync and lambda. Part of the requirement is to have a schema.graphql written to disk at deploy time. Is this possible with type-graphql? I know there isn't any CLI tool available for this library but could this be done with the GraphQLSchema object returned from buildSchema? Thanks

Community Enhancement Solved

Most helpful comment

I've used this approach (schema in SDL, generating TS interfaces, writing resolvers using graphql-tools) in my earlier GraphQL projects and keeping things in sync was a real pain, as I described in article:
https://medium.com/@19majkel94/graphql-typescript-typegraphql-ba0225cb4bed
That's why TypeGraphQL has been created, to have one source of truth - only classes in the code.

If you need the SDL schema file for deployment purposes, you can create a simple npm script for that and you can run this even as a git commit hook.

// createSchema.ts
export function createSchema() {
  return buildSchema({
    // ...
  }
}

// generate.ts
(async () => {
  const schema = await createSchema();
  const sdl = printSchema(schema);
  await fs.writeFile(__dirname + '/schema.graphql', sdl);
})();
{
  "scripts": {
    "generate-schema": "ts-node ./generate.ts"
  }
}

All 5 comments

graphql-js has a printSchema helper:
http://graphql.org/graphql-js/utilities/#printschema

@ajmath Are you in the gitter lobby ? I'ld like to discuss this topic with you because I'm doing the same

I ended up going a different route and generating basic typescript types
from a graphql schema using
https://github.com/dotansimha/graphql-code-generator

On Fri, May 18, 2018 at 8:54 AM Olivier MATROT notifications@github.com
wrote:

@ajmath https://github.com/ajmath Are you in the gutter lobby ? I'ld
like to discuss this topic with you because I'm doing the same

—
You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub
https://github.com/19majkel94/type-graphql/issues/81#issuecomment-390197851,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAQRt_oTZPQWnYxVOIhrYRNCCOuAzw2Kks5tzsSWgaJpZM4T-e5_
.

I've used this approach (schema in SDL, generating TS interfaces, writing resolvers using graphql-tools) in my earlier GraphQL projects and keeping things in sync was a real pain, as I described in article:
https://medium.com/@19majkel94/graphql-typescript-typegraphql-ba0225cb4bed
That's why TypeGraphQL has been created, to have one source of truth - only classes in the code.

If you need the SDL schema file for deployment purposes, you can create a simple npm script for that and you can run this even as a git commit hook.

// createSchema.ts
export function createSchema() {
  return buildSchema({
    // ...
  }
}

// generate.ts
(async () => {
  const schema = await createSchema();
  const sdl = printSchema(schema);
  await fs.writeFile(__dirname + '/schema.graphql', sdl);
})();
{
  "scripts": {
    "generate-schema": "ts-node ./generate.ts"
  }
}

FYI, done in #170 🎉

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MichalLytek picture MichalLytek  Â·  4Comments

MichalLytek picture MichalLytek  Â·  3Comments

memark picture memark  Â·  3Comments

maplesteve picture maplesteve  Â·  3Comments

laukaichung picture laukaichung  Â·  3Comments