Skip to content

Commit

Permalink
fix: directive is duplicated when schema already has one (apollograph…
Browse files Browse the repository at this point in the history
  • Loading branch information
calvincestari authored and gh-action-runner committed Jan 11, 2024
1 parent bb821c0 commit c7b03c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Sources/GraphQLCompiler/ApolloCodegenFrontendBundle.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { emptyValidationOptions } from "../__testUtils__/validationHelpers";

describe("given schema", () => {
const schemaSDL: string = `
directive @defer(label: String, if: Boolean! = true) on FRAGMENT_SPREAD | INLINE_FRAGMENT
type Query {
allAnimals: [Animal!]
}
Expand All @@ -31,6 +33,28 @@ describe("given schema", () => {

const schema: GraphQLSchema = loadSchemaFromSources([new Source(schemaSDL, "Test Schema", { line: 1, column: 1 })]);

// Directive Definition Tests

describe("does not add a duplicate directive", () => {
const documentString: string = `
query Test {
allAnimals {
species
}
}
`;

const document: DocumentNode = parseOperationDocument(
new Source(documentString, "Test Query", { line: 1, column: 1 })
);

it("should pass validation", () => {
const validationErrors: readonly GraphQLError[] = validateDocument(schema, document, emptyValidationOptions)

expect(validationErrors).toHaveLength(0)
});
})

// Disabling Tests

describe("query has inline fragment with @defer directive", () => {
Expand Down
22 changes: 0 additions & 22 deletions Sources/GraphQLCompiler/JavaScript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ import {
buildASTSchema,
printSchema,
extendSchema,
Kind,
GraphQLDeferDirective,
} 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 @@ -54,7 +51,6 @@ export function loadSchemaFromSources(sources: Source[]): GraphQLSchema {
}

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

if (!introspectionJSONResult) { assertValidSDL(document) }

Expand All @@ -67,24 +63,6 @@ export function loadSchemaFromSources(sources: Source[]): GraphQLSchema {
return schema
}

function addExperimentalDirectivesToDocument(document: DocumentNode): DocumentNode {
// While @defer is experimental the directive needs to be manually added to the
// list of available directives for the schema document.
return document.definitions.some(definition =>
definition.kind == Kind.DIRECTIVE_DEFINITION &&
definition.name.value == GraphQLDeferDirective.name
) ?
document :
concatAST([document, experimentalDeferDirectiveDocumentNode()])
}

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

function loadSchemaFromIntrospectionResult(
introspectionResult: string
): GraphQLSchema {
Expand Down

0 comments on commit c7b03c2

Please sign in to comment.