Skip to content

Commit

Permalink
Conditionally import PossibleTypeExtensions validation rule (#1935)
Browse files Browse the repository at this point in the history
As described in apollographql/apollo-server#4066,
older versions of `graphql` do not have this type.

Co-authored-by: Jesse Rosenberger <git@jro.cc>
  • Loading branch information
oguimbal and abernix committed Jun 8, 2020
1 parent ea1e0f4 commit 9e80e2e
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,36 @@ import { GraphQLSchemaValidationError } from "./GraphQLSchemaValidationError";
import { specifiedSDLRules } from "graphql/validation/specifiedRules";
import {
KnownTypeNamesRule,
UniqueDirectivesPerLocationRule
UniqueDirectivesPerLocationRule,
ValidationRule
} from "graphql/validation";
// Currently, this PossibleTypeExtensions rule is experimental and thus not
// exposed directly from the rules module above. This may change in the future!
import { PossibleTypeExtensions } from "graphql/validation/rules/PossibleTypeExtensions";
import { mapValues, isNotNullOrUndefined } from "apollo-env";

export interface GraphQLSchemaModule {
typeDefs: DocumentNode;
resolvers?: GraphQLResolverMap<any>;
}

const skippedSDLRules = [
PossibleTypeExtensions,
const skippedSDLRules: ValidationRule[] = [
KnownTypeNamesRule,
UniqueDirectivesPerLocationRule
];

// BREAKING VERSION: Remove this when graphql-js 15 is minimum version.
// Currently, this PossibleTypeExtensions rule is experimental and thus not
// exposed directly from the rules module above. This may change in the future!
// Additionally, it does not exist in prior graphql versions. Thus this try/catch.
try {
const PossibleTypeExtensions: typeof import("graphql/validation/rules/PossibleTypeExtensions").PossibleTypeExtensions = require("graphql/validation/rules/PossibleTypeExtensions")
.PossibleTypeExtensions;
if (PossibleTypeExtensions) {
skippedSDLRules.push(PossibleTypeExtensions);
}
} catch (e) {
// No need to fail in this case. Instead, if this validation rule is missing, we will assume its not used
// by the version of `graphql` that is available to us.
}

const sdlRules = specifiedSDLRules.filter(
rule => !skippedSDLRules.includes(rule)
);
Expand Down

0 comments on commit 9e80e2e

Please sign in to comment.