Skip to content

Commit fdc3555

Browse files
extendSchema: Do not modify standard directives (#3618)
1 parent 540bb38 commit fdc3555

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/utilities/__tests__/extendSchema-test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
assertScalarType,
1818
assertUnionType,
1919
} from '../../type/definition';
20-
import { assertDirective } from '../../type/directives';
20+
import { assertDirective, specifiedDirectives } from '../../type/directives';
2121
import {
2222
GraphQLBoolean,
2323
GraphQLFloat,
@@ -85,6 +85,34 @@ describe('extendSchema', () => {
8585
});
8686
});
8787

88+
it('Do not modify built-in types and directives', () => {
89+
const schema = buildSchema(`
90+
type Query {
91+
str: String
92+
int: Int
93+
float: Float
94+
id: ID
95+
bool: Boolean
96+
}
97+
`);
98+
99+
const extensionSDL = dedent`
100+
extend type Query {
101+
foo: String
102+
}
103+
`;
104+
const extendedSchema = extendSchema(schema, parse(extensionSDL));
105+
106+
// Built-ins are used
107+
expect(extendedSchema.getType('Int')).to.equal(GraphQLInt);
108+
expect(extendedSchema.getType('Float')).to.equal(GraphQLFloat);
109+
expect(extendedSchema.getType('String')).to.equal(GraphQLString);
110+
expect(extendedSchema.getType('Boolean')).to.equal(GraphQLBoolean);
111+
expect(extendedSchema.getType('ID')).to.equal(GraphQLID);
112+
113+
expect(extendedSchema.getDirectives()).to.have.members(specifiedDirectives);
114+
});
115+
88116
it('extends objects by adding new fields', () => {
89117
const schema = buildSchema(`
90118
type Query {

src/utilities/extendSchema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ import {
6767
GraphQLDeprecatedDirective,
6868
GraphQLDirective,
6969
GraphQLSpecifiedByDirective,
70+
isSpecifiedDirective,
7071
} from '../type/directives';
7172
import { introspectionTypes, isIntrospectionType } from '../type/introspection';
7273
import { isSpecifiedScalarType, specifiedScalarTypes } from '../type/scalars';
@@ -236,6 +237,11 @@ export function extendSchemaImpl(
236237
}
237238

238239
function replaceDirective(directive: GraphQLDirective): GraphQLDirective {
240+
if (isSpecifiedDirective(directive)) {
241+
// Builtin directives are not extended.
242+
return directive;
243+
}
244+
239245
const config = directive.toConfig();
240246
return new GraphQLDirective({
241247
...config,

0 commit comments

Comments
 (0)