Skip to content

Commit

Permalink
compat: filter extensions prior to passing to buildASTSchema.
Browse files Browse the repository at this point in the history
.@IvanGoncharov originally brought this to my attention when he pointed me to
yaacovCR#32 (comment)
and suggested stripping extension nodes prior to invoking `buildASTSchema`
as a cross-version (v14 <=> v15) approach for interim compatibility on the
v4 series of `graphql-tools`.

The most urgent and pertinent need here from my perspective is to allow
user-exploration of the new `graphql@15` release candidate within Apollo
Server which currently re-exports the entirety of `graphql-tools` (even
though it only relies on small portions of it).

Upon further investigation of the above-referenced issue, it appears that
@yaacovCR had already crafted the solution that @IvanGoncharov had suggested
to me, which I found in 2280eef within the
well-organized #1206
(which I am thankful for the continued updates on!).

My commit here merely grabs a sub-set of that commit that seemed most
pertinent; I certainly don't claim that this solution is nearly as
comprehensive as the original 2280eef.  My
hope is that by using the same code/implementation here, it will marginally
lessen future merge conflicts.

Since this is basically a re-working of @yaacovCR's commit, I've attributed
co-authorship of this commit accordingly.  (Thank you, again!)

Ref: #1272
Co-authored-by: yaacovCR <yaacovCR@gmail.com>
  • Loading branch information
abernix and yaacovCR committed Feb 17, 2020
1 parent c7f5088 commit 66d73a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/generate/buildSchemaFromTypeDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
concatenateTypeDefs,
SchemaError,
} from '.';
import filterExtensionDefinitions from './filterExtensionDefinitions';

function buildSchemaFromTypeDefinitions(
typeDefinitions: ITypeDefinitions,
Expand All @@ -38,10 +39,11 @@ function buildSchemaFromTypeDefinitions(
}

const backcompatOptions = { commentDescriptions: true };
const typesAst = filterExtensionDefinitions(astDocument);

// TODO fix types https://github.com/apollographql/graphql-tools/issues/542
let schema: GraphQLSchema = (buildASTSchema as any)(
astDocument,
typesAst,
backcompatOptions,
);

Expand Down
19 changes: 19 additions & 0 deletions src/generate/filterExtensionDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { DocumentNode, DefinitionNode, Kind } from 'graphql';

export default function filterExtensionDefinitions(ast: DocumentNode) {
const extensionDefs = ast.definitions.filter(
(def: DefinitionNode) =>
def.kind !== Kind.OBJECT_TYPE_EXTENSION &&
def.kind !== Kind.INTERFACE_TYPE_EXTENSION &&
def.kind !== Kind.INPUT_OBJECT_TYPE_EXTENSION &&
def.kind !== Kind.UNION_TYPE_EXTENSION &&
def.kind !== Kind.ENUM_TYPE_EXTENSION &&
def.kind !== Kind.SCALAR_TYPE_EXTENSION &&
def.kind !== Kind.SCHEMA_EXTENSION,
);

return {
...ast,
definitions: extensionDefs,
};
}

0 comments on commit 66d73a8

Please sign in to comment.