Graphql-code-generator: [question] generate typeDefs

Created on 20 Apr 2020  路  3Comments  路  Source: dotansimha/graphql-code-generator

Hey guys, thanks for your amazing project.

I would like to know if there is any plugin for that generates typeDefs like:

export const typeDefs = gql`
  extend type Query {
    isLoggedIn: Boolean!
    cartItems: [ID!]!
  }

  extend type Launch {
    isInCart: Boolean!
  }

  extend type Mutation {
    addOrRemoveFromCart(id: ID!): [ID!]!
  }
`;

I am using Apollo Local state management, therefore I want to generate the typeDefs automatically.

Thanks!

ref document

Most helpful comment

I used a custom plugin like:

typescript-typedefs.js:

const { printSchema } = require('graphql');

module.exports = {
    plugin: (schema, documents, config) => {
        return [
            'import gql from "graphql-tag";',
            '',
            'export const typeDefs = gql`',
            printSchema(schema),
            '`;',
            '',
        ].join('\n');
    },
};

and then added the custom plugin in my codegen.yml:

overwrite: true
schema: "./graphqls/*.graphqls"
documents: null
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
      - "typescript-typedefs.js" 

Maybe useful to add something like this to graphql-code-generator so other people can use out of the box?

All 3 comments

I had basically the same question.
My codegen yml looks like:

overwrite: true
schema: "./graphql/schema.graphql"
..

It would be useful to generate the typeDefs automatically based on this.

I used a custom plugin like:

typescript-typedefs.js:

const { printSchema } = require('graphql');

module.exports = {
    plugin: (schema, documents, config) => {
        return [
            'import gql from "graphql-tag";',
            '',
            'export const typeDefs = gql`',
            printSchema(schema),
            '`;',
            '',
        ].join('\n');
    },
};

and then added the custom plugin in my codegen.yml:

overwrite: true
schema: "./graphqls/*.graphqls"
documents: null
generates:
  src/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
      - "typescript-typedefs.js" 

Maybe useful to add something like this to graphql-code-generator so other people can use out of the box?

Closing, as @marceloverdijk suggested a quick plugin implementation if someone needs that. Feel free to create a PR!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dotansimha picture dotansimha  路  3Comments

SimenB picture SimenB  路  3Comments

zenVentzi picture zenVentzi  路  3Comments

steebchen picture steebchen  路  3Comments

fvisticot picture fvisticot  路  3Comments