diff --git a/.changeset/jolly-drinks-sip.md b/.changeset/jolly-drinks-sip.md new file mode 100644 index 00000000000..573a1541005 --- /dev/null +++ b/.changeset/jolly-drinks-sip.md @@ -0,0 +1,20 @@ +--- +'@graphql-tools/utils': patch +--- + +Use `specifiedDirectives.some` instead of `isSpecifiedDirective` to check if the directive is a +native directive + +In case of overriding a native directive like `@deprecated`, `isSpecifiedDirective` only checks the name like; + +```ts +isSpecifiedDirective(directive) { + return specifiedDirectives.some(specifiedDirective => directive.name === specifiedDirective.name); +} +``` + +But we need to check the actual reference equality to avoid rewiring native directives like below; + +```ts +specifiedDirectives.some(specifiedDirective => directive === specifiedDirective) +``` diff --git a/packages/schema/tests/reproductions.test.ts b/packages/schema/tests/reproductions.test.ts new file mode 100644 index 00000000000..60d0049feac --- /dev/null +++ b/packages/schema/tests/reproductions.test.ts @@ -0,0 +1,27 @@ +import { stripIgnoredCharacters } from 'graphql'; +import { printSchemaWithDirectives } from '@graphql-tools/utils'; +import { makeExecutableSchema } from '../src/makeExecutableSchema'; + +test('works', () => { + const typeDefs = stripIgnoredCharacters(/* GraphQL */ ` + schema { + query: Query + } + + directive @deprecated( + owner: String! + expiry: Date! + reason: String! + ) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION + + scalar Date + + type Query { + myField: String! @deprecated(owner: "Test Squad", expiry: "2025-12-31", reason: "test") + } + `); + const schema = makeExecutableSchema({ + typeDefs, + }); + expect(stripIgnoredCharacters(printSchemaWithDirectives(schema))).toBe(typeDefs); +}); diff --git a/packages/utils/src/mapSchema.ts b/packages/utils/src/mapSchema.ts index 8ed639c5afe..5f3f91b13e8 100644 --- a/packages/utils/src/mapSchema.ts +++ b/packages/utils/src/mapSchema.ts @@ -77,7 +77,6 @@ export function mapSchema(schema: GraphQLSchema, schemaMapper: SchemaMapper = {} const newDirectives = mapDirectives(originalDirectives, schema, schemaMapper); const { typeMap, directives } = rewireTypes(newTypeMap, newDirectives); - return new GraphQLSchema({ ...schema.toConfig(), query: getObjectTypeFromTypeMap( diff --git a/packages/utils/src/print-schema-with-directives.ts b/packages/utils/src/print-schema-with-directives.ts index 2ab8b2230aa..57adbed3f5d 100644 --- a/packages/utils/src/print-schema-with-directives.ts +++ b/packages/utils/src/print-schema-with-directives.ts @@ -29,7 +29,6 @@ import { isIntrospectionType, isObjectType, isScalarType, - isSpecifiedDirective, isSpecifiedScalarType, isUnionType, Kind, @@ -75,7 +74,7 @@ export function getDocumentNodeFromSchema( const directives = schema.getDirectives(); for (const directive of directives) { - if (isSpecifiedDirective(directive)) { + if (specifiedDirectives.some(specifiedDirective => specifiedDirective === directive)) { continue; } diff --git a/packages/utils/src/rewire.ts b/packages/utils/src/rewire.ts index da2c21dc198..782fe4bd382 100644 --- a/packages/utils/src/rewire.ts +++ b/packages/utils/src/rewire.ts @@ -21,9 +21,9 @@ import { isNonNullType, isObjectType, isScalarType, - isSpecifiedDirective, isSpecifiedScalarType, isUnionType, + specifiedDirectives, } from 'graphql'; import { getBuiltInForStub, isNamedStub } from './stub.js'; @@ -74,7 +74,7 @@ export function rewireTypes( }; function rewireDirective(directive: GraphQLDirective): GraphQLDirective { - if (isSpecifiedDirective(directive)) { + if (specifiedDirectives.some(specifiedDirective => specifiedDirective === directive)) { return directive; } const directiveConfig = directive.toConfig();