Graphql-code-generator: Generate types for interfaces without any implementing types

Created on 5 May 2020  路  2Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.
I have a statically-known portion of my schema including an interface Node that I would like to generate types for: when my app starts, I fetch a remote jsonschema and translate that into multiple type definitions that implement said interface Node which I attach to the schema and start the server.

Currently, interfaces without implementations don't seem to generate anything.
It also seems that concrete types where all fields are of a type of an unimplemented interface are completely omitted.

Describe the solution you'd like
I would like an option to generate a type for an interface that consists of the fields of that interface instead of a union of all of the implementations.

Describe alternatives you've considered
I tried creating a dummy type that implements this interface but I don't want to pollute my sever with types like this.

I also am using a custom plugin and it seems that the schema that I get as an argument doesn't even have unimplemented interfaces listed anywhere in the GraphQLSchema so this problem may be higher up.

Additional context
Go to the demo and paste this schema:
https://graphql-code-generator.com/#live-demo

schema {
  query: Query
  mutation: Mutation
}

type Query {
  with_impl: IFoo
  zero_impl: IBar
}

type Mutation {
  zero_impl: IBar
}

interface IFoo {
  foo: String!
}

interface IBar {
  bar: String!
}

type Foo implements IFoo {
  foo: String!
}
bug core dependencies

Most helpful comment

Adding a previous comment https://github.com/dotansimha/graphql-code-generator/pull/3618#issuecomment-600992660 of mine for additional context.

All 2 comments

The mentioned schema from using the graphql module directly seems to have those interfaces present on the type map, but not the plugin. The following is using graphql v14.6 and @graphql-codegen/cli v1.13.2 (uses the same graphql version)


From parse(buildASTSchema("..."))

const { parse, buildASTSchema } = require("graphql")
const schema = buildASTSchema(parse(`
schema {
  query: Query
  mutation: Mutation
}

type Query {
  with_impl: IFoo
  zero_impl: IBar
}

type Mutation {
  zero_impl: IBar
}

interface IFoo {
  foo: String!
}

interface IBar {
  bar: String!
}

type Foo implements IFoo {
  foo: String!
}
`))

yields

GraphQLSchema {
  __validationErrors: undefined,
  extensions: undefined,
  astNode: {
    kind: 'SchemaDefinition',
    directives: [],
    operationTypes: [ [Object], [Object] ],
    loc: { start: 1, end: 47 }
  },
  extensionASTNodes: undefined,
  __allowedLegacyNames: [],
  _queryType: Query,
  _mutationType: Mutation,
  _subscriptionType: null,
  _directives: [ @skip, @include, @deprecated ],
  _typeMap: [Object: null prototype] {
    Query: Query,
    IFoo: IFoo,
    String: String,
    IBar: IBar,                              // <-- IBar here, yay
    Mutation: Mutation,
    __Schema: __Schema,
    __Type: __Type,
    __TypeKind: __TypeKind,
    Boolean: Boolean,
    __Field: __Field,
    __InputValue: __InputValue,
    __EnumValue: __EnumValue,
    __Directive: __Directive,
    __DirectiveLocation: __DirectiveLocation,
    Foo: Foo
  },
  _possibleTypeMap: [Object: null prototype] {},
  _implementations: [Object: null prototype] { IFoo: [ Foo ] }
}

But when I run the same schema through the plugin tooling, the schema argument I get is


From module.exports = { plugin(schema, documents, config) }

GraphQLSchema {
    __validationErrors: undefined,
    extensions: {},
    astNode: {
      kind: 'SchemaDefinition',
      directives: [],
      operationTypes: [Array],
      loc: [Object]
    },
    extensionASTNodes: [],
    __allowedLegacyNames: [],
    _queryType: Query,
    _mutationType: undefined,          // <- entire mutation type is gone
    _subscriptionType: undefined,
    _directives: [ @skip, @include, @deprecated ],
    _typeMap: [Object: null prototype] {
      Query: Query,
      IFoo: IFoo,
      String: String,
      __Schema: __Schema,
      __Type: __Type,
      __TypeKind: __TypeKind,
      Boolean: Boolean,
      __Field: __Field,
      __InputValue: __InputValue,
      __EnumValue: __EnumValue,
      __Directive: __Directive,
      __DirectiveLocation: __DirectiveLocation,
      Foo: Foo
    },
    _possibleTypeMap: [Object: null prototype] {},
    _implementations: [Object: null prototype] { IFoo: [Array] }
  },

It seems that the used loadSchema from @graphql-toolkit/core has { filterKinds: OPERATION_KINDS } in the config... could this be narrowing the scope of the AST?

Adding a previous comment https://github.com/dotansimha/graphql-code-generator/pull/3618#issuecomment-600992660 of mine for additional context.

Was this page helpful?
0 / 5 - 0 ratings