Skip to content

GraphQL code generator plugin for generating an interface with all object types from your schema

License

Notifications You must be signed in to change notification settings

Fullscript/graphql-codegen-object-types

Repository files navigation

graphql-codegen-object-types

graphql-codegen-object-types is a plugin for @graphql-codgen used to generate a TypeScript interface of all object types within a GraphQL schema.

Prerequisites

If you don't already have @graphql-codegen setup, you can follow their getting started guide.

To use graphql-codegen-object-types you will also need:

Installation

Add graphql-codegen-object-types to your dependencies like so:

Yarn v1:

  • yarn add https://github.com/Fullscript/graphql-codegen-object-types.git#1.0.0

Yarn v2 (and onwards):

  • yarn add @fullscript/graphql-codegen-object-types@https://github.com/Fullscript/graphql-codegen-object-types.git#1.0.0

NPM:

  • npm install https://github.com/Fullscript/graphql-codegen-object-types.git#1.0.0

Configuration

In your codegen configuration file, add something similar to the following:

generates:
  # any configuration you already have
  path/to/generatedFile.ts:
    preset: import-types
    presetConfig:
      typesPath: "path/to/baseTypes"
    plugins:
      - add: 
        content:
          - "import type * as Types from 'path/to/baseTypes';"
      - "@fullscript/graphql-codegen-object-types"
    config:
      ignoredSuffixes:
        - "Connection"
        - "Error"
        - "Query"
        - "Mutation"
      useTypeImports: true

Options

Each of the following options are optional, default values are listed as part of description.

  • ignoredSuffixes: A list of type suffixes or names to ignore from your GraphQL schema. Any object type that matches an entry within this list will not be part of the generated interface.
  • useTypeImports: (default false) Whether or not to use type imports/exports.
  • interfaceName: (default GraphQLObjectTypes) The name of the generated TypeScript interface.

Example output

import type * as Types from 'path/to/baseTypes';

interface GraphQLObjectTypes {  
  User: Types.User;
  Country: Types.Country;
  // ...
}

export type { GraphQLObjectTypes };

About

GraphQL code generator plugin for generating an interface with all object types from your schema

Resources

License

Stars

Watchers

Forks

Packages

No packages published