Skip to content

Commit b744ebd

Browse files
IvanGoncharovyaacovCR
authored andcommitted
extendSchema: Do not modify standard directives (graphql#3618)
1 parent c0a0acf commit b744ebd

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

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
@@ -68,6 +68,7 @@ import {
6868
GraphQLDirective,
6969
GraphQLOneOfDirective,
7070
GraphQLSpecifiedByDirective,
71+
isSpecifiedDirective,
7172
} from '../type/directives';
7273
import { introspectionTypes, isIntrospectionType } from '../type/introspection';
7374
import { isSpecifiedScalarType, specifiedScalarTypes } from '../type/scalars';
@@ -237,6 +238,11 @@ export function extendSchemaImpl(
237238
}
238239

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

0 commit comments

Comments
 (0)