Skip to content

Commit 5b0b1ed

Browse files
fix(apollo-graphql): Preserve directive usages on SchemaDefinition nodes (#2457)
1 parent c6ac2ae commit 5b0b1ed

File tree

4 files changed

+50
-39
lines changed

4 files changed

+50
-39
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- `apollo-graphql`
88
- Export useful operation sanitization transforms [PR #2057](https://github.com/apollographql/apollo-tooling/pull/2057)
9+
- Preserve directive usages on SchemaDefinition nodes in `buildSchemaFromSDL` [PR #2457](https://github.com/apollographql/apollo-tooling/pull/2457)
910

1011
## `apollo@2.33.7`
1112

package-lock.json

+14-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/apollo-graphql/src/schema/__tests__/buildSchemaFromSDL.test.ts

+23
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,29 @@ type MutationRoot {
351351
`"Unknown directive \\"@something\\"."`
352352
);
353353
});
354+
355+
it(`should preserve directive usages from the schema definition node`, () => {
356+
const schema = buildSchemaFromSDL(
357+
gql`
358+
directive @contact(
359+
name: String!
360+
url: String!
361+
description: String
362+
) on SCHEMA
363+
364+
schema
365+
@contact(
366+
name: "Contact"
367+
url: "http://example.com/contact"
368+
description: "Testing"
369+
) {
370+
query: Query
371+
}
372+
`
373+
);
374+
expect(schema.astNode!.directives).toHaveLength(1);
375+
expect(schema.astNode!.directives![0].name.value).toEqual("contact");
376+
});
354377
});
355378

356379
describe(`resolvers`, () => {

packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
isAbstractType,
1919
isScalarType,
2020
isEnumType,
21-
GraphQLEnumValueConfig
21+
GraphQLEnumValueConfig,
22+
DirectiveNode
2223
} from "graphql";
2324
import { validateSDL } from "graphql/validation/validate";
2425
import { isDocumentNode, isNode } from "../utilities/graphql";
@@ -108,6 +109,7 @@ export function buildSchemaFromSDL(
108109

109110
const schemaDefinitions: SchemaDefinitionNode[] = [];
110111
const schemaExtensions: SchemaExtensionNode[] = [];
112+
const schemaDirectives: DirectiveNode[] = [];
111113

112114
for (const definition of documentAST.definitions) {
113115
if (isTypeDefinitionNode(definition)) {
@@ -130,6 +132,9 @@ export function buildSchemaFromSDL(
130132
directiveDefinitions.push(definition);
131133
} else if (definition.kind === Kind.SCHEMA_DEFINITION) {
132134
schemaDefinitions.push(definition);
135+
schemaDirectives.push(
136+
...(definition.directives ? definition.directives : [])
137+
);
133138
} else if (definition.kind === Kind.SCHEMA_EXTENSION) {
134139
schemaExtensions.push(definition);
135140
}
@@ -211,7 +216,12 @@ export function buildSchemaFromSDL(
211216
typeName
212217
? (schema.getType(typeName) as GraphQLObjectType<any, any>)
213218
: undefined
214-
)
219+
),
220+
astNode: {
221+
kind: Kind.SCHEMA_DEFINITION,
222+
directives: schemaDirectives,
223+
operationTypes: [] // satisfies typescript, will be ignored
224+
}
215225
});
216226

217227
for (const module of modules) {

0 commit comments

Comments
 (0)