Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Validate existing @defer directive #402

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions apollo-ios-codegen/Sources/GraphQLCompiler/JavaScript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import {
buildASTSchema,
printSchema,
extendSchema,
Kind,
GraphQLDeferDirective,
GraphQLDirective,
DirectiveDefinitionNode,
ASTNode,
} from "graphql";
import { defaultValidationRules, ValidationOptions } from "./validationRules";
import { compileToIR, CompilationResult } from "./compiler";
import { assertValidSchema, assertValidSDL } from "./utilities/graphql";
import {
addApolloCodegenSchemaExtensionToDocument,
} from "./utilities/apolloCodegenSchemaExtension";
import { definitionNode } from "./utilities";

// We need to export all the classes we want to map to native objects,
// so we have access to the constructor functions for type checks.
Expand Down Expand Up @@ -51,6 +57,7 @@ export function loadSchemaFromSources(sources: Source[]): GraphQLSchema {
}

var document = addApolloCodegenSchemaExtensionToDocument(concatAST(documents))
document = validateOrAddExperimentalDeferDirectiveToDocument(document)

if (!introspectionJSONResult) { assertValidSDL(document) }

Expand Down Expand Up @@ -105,3 +112,51 @@ export function compileDocument(
): CompilationResult {
return compileToIR(schema, document, legacySafelistingCompatibleOperations, validationOptions);
}

// While @defer is experimental the directive needs to be manually added to the list of available
// directives for the schema document. If the directive is already in the document it must be
// validated to ensure it matches the directive definition that Apollo iOS defer support was
// built to support.
//
// Once defer is part of the GraphQL spec and the directive is no longer considered experimental
// this check can be removed.
function validateOrAddExperimentalDeferDirectiveToDocument(document: DocumentNode): DocumentNode {
const definition = document.definitions.find(isDeferDirectiveDefinitionNode)

if (definition === undefined) {
return concatAST([document, experimentalDeferDirectiveDocumentNode()])
}

if (!match(definition, GraphQLDeferDirective)) {
throw new Error(
`Mismatched ${GraphQLDeferDirective.toString()} directive found.`,
);
}

return document
}

// Returns whether the current node is a defer directive definition node.
function isDeferDirectiveDefinitionNode(node: ASTNode): node is DirectiveDefinitionNode {
return (
node.kind === Kind.DIRECTIVE_DEFINITION &&
node.name.value === GraphQLDeferDirective.name
)
}

// Checks whether the supplied directive definition node matches against important properties
// of the experimentally defined defer directive that Apollo iOS expects.
function match(definition: DirectiveDefinitionNode, target: GraphQLDirective): Boolean {
return(
definition.repeatable === target.isRepeatable &&
definition.locations.map((node) => node.value).sort().toString() === target.locations.slice(0).sort().toString() &&
definition.arguments?.map((value) => value.name.value).sort().toString() === target.args.map((value) => value.name).sort().toString()
)
}

function experimentalDeferDirectiveDocumentNode(): DocumentNode {
return {
kind: Kind.DOCUMENT,
definitions: [definitionNode(GraphQLDeferDirective)]
}
}
Loading