graphile / graphile.github.io

PostGraphile (PostGraphQL) and Graphile-Build website - contributions very welcome!
https://www.graphile.org/
25 stars 128 forks source link

Hooks filtering out interface child types from `makeExtendSchema` #368

Closed mgagliardo91 closed 10 months ago

mgagliardo91 commented 1 year ago

Summary

I am trying to add pure-GraphQL types using the makeExtendSchema plugin that includes an interface type and a few types that implement it. I can correctly specify these types and the __resolveType handler; however, if I only link the interface into the Graph (i.e. as a return result from a query), the child types are filtered from the final schema.

Steps to reproduce

Using the schema below:

interface MyInterface {
  id: String!
}

type MyType implements MyInterface {
  id: String!
}

extend type Query {
  myQuery: MyInterface
}

By providing the typedefs above using the makeExtendSchema plugin, MyInterface will be included in the resulting schema, but MyType will not.

If I instead explicitly tie MyType into the graph:

extend type Query {
  myQuery: MyInterface
  myOtherQuery: MyType
}

then MyType is correctly added the the final schema.

Expected results

All interface types and their implementing types are added to the final schema, regardless of if they are explicitly tied to the graph.

Actual results

Only explicit implementing types are added to the graph.

Additional context

Possible Solution

I currently have a hack in place that adds an explicity query type ___hack: MyType and then I remove this during the finalize hook.

While trying to diagnose, I do find that MyType is available during the GraphQLObject hook, so I know that the makeExtendSchema is correctly providing them back, but some other hook is filtering it out by the time GraphQLObject:Interfaces is hit.

benjie commented 1 year ago

This seems to be an omission in makeExtendSchemaPlugin; the types should be added here:

https://github.com/graphile/graphile-engine/blob/29fa78a91df5d9865420fee429918cf3f7010c1e/packages/graphile-utils/src/makeExtendSchemaPlugin.ts#L490

Here's where an object definition is registered into newTypes, for later construction:

https://github.com/graphile/graphile-engine/blob/29fa78a91df5d9865420fee429918cf3f7010c1e/packages/graphile-utils/src/makeExtendSchemaPlugin.ts#L179-L183

(Similar for other type definitions)

Hopefully grabbing the build.getTypeByName(newObjectTypeDefinition.name) and adding it to the types array should solve the issue.

Interested in taking this on?

mgagliardo91 commented 1 year ago

@benjie should be fixed in https://github.com/graphile/graphile-engine/pull/830