Skip to content

extendSchema: Do not modify standard directives #3618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/utilities/__tests__/extendSchema-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
assertScalarType,
assertUnionType,
} from '../../type/definition';
import { assertDirective } from '../../type/directives';
import { assertDirective, specifiedDirectives } from '../../type/directives';
import {
GraphQLBoolean,
GraphQLFloat,
Expand Down Expand Up @@ -85,6 +85,34 @@ describe('extendSchema', () => {
});
});

it('Do not modify built-in types and directives', () => {
const schema = buildSchema(`
type Query {
str: String
int: Int
float: Float
id: ID
bool: Boolean
}
`);

const extensionSDL = dedent`
extend type Query {
foo: String
}
`;
const extendedSchema = extendSchema(schema, parse(extensionSDL));

// Built-ins are used
expect(extendedSchema.getType('Int')).to.equal(GraphQLInt);
expect(extendedSchema.getType('Float')).to.equal(GraphQLFloat);
expect(extendedSchema.getType('String')).to.equal(GraphQLString);
expect(extendedSchema.getType('Boolean')).to.equal(GraphQLBoolean);
expect(extendedSchema.getType('ID')).to.equal(GraphQLID);

expect(extendedSchema.getDirectives()).to.have.members(specifiedDirectives);
});

it('extends objects by adding new fields', () => {
const schema = buildSchema(`
type Query {
Expand Down
6 changes: 6 additions & 0 deletions src/utilities/extendSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import {
GraphQLDeprecatedDirective,
GraphQLDirective,
GraphQLSpecifiedByDirective,
isSpecifiedDirective,
} from '../type/directives';
import { introspectionTypes, isIntrospectionType } from '../type/introspection';
import { isSpecifiedScalarType, specifiedScalarTypes } from '../type/scalars';
Expand Down Expand Up @@ -236,6 +237,11 @@ export function extendSchemaImpl(
}

function replaceDirective(directive: GraphQLDirective): GraphQLDirective {
if (isSpecifiedDirective(directive)) {
// Builtin directives are not extended.
return directive;
}

const config = directive.toConfig();
return new GraphQLDirective({
...config,
Expand Down