Skip to content

Commit

Permalink
Add @defer directive to specified directives
Browse files Browse the repository at this point in the history
  • Loading branch information
robrichard committed Feb 9, 2021
1 parent 665e256 commit efc6c73
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export {
specifiedDirectives,
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeferDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
// "Enum" of Type Kinds
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export {
specifiedDirectives,
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeferDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
// "Enum" of Type Kinds
Expand Down
25 changes: 25 additions & 0 deletions src/type/__tests__/introspection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,31 @@ describe('Introspection', () => {
},
],
},
{
name: 'defer',
isRepeatable: false,
locations: ['FRAGMENT_SPREAD', 'INLINE_FRAGMENT'],
args: [
{
defaultValue: null,
name: 'if',
type: {
kind: 'SCALAR',
name: 'Boolean',
ofType: null,
},
},
{
defaultValue: null,
name: 'label',
type: {
kind: 'SCALAR',
name: 'String',
ofType: null,
},
},
],
},
{
name: 'deprecated',
isRepeatable: false,
Expand Down
5 changes: 5 additions & 0 deletions src/type/directives.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export const GraphQLIncludeDirective: GraphQLDirective;
*/
export const GraphQLSkipDirective: GraphQLDirective;

/**
* Used to conditionally defer fragments.
*/
export const GraphQLDeferDirective: GraphQLDirective;

/**
* Used to provide a URL for specifying the behavior of custom scalar definitions.
*/
Expand Down
24 changes: 24 additions & 0 deletions src/type/directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,29 @@ export const GraphQLSkipDirective = new GraphQLDirective({
},
});

/**
* Used to conditionally defer fragments.
*/
export const GraphQLDeferDirective = new GraphQLDirective({
name: 'defer',
description:
'Directs the executor to defer this fragment when the `if` argument is true or undefined.',
locations: [
DirectiveLocation.FRAGMENT_SPREAD,
DirectiveLocation.INLINE_FRAGMENT,
],
args: {
if: {
type: GraphQLBoolean,
description: 'Deferred when true or undefined.',
},
label: {
type: GraphQLString,
description: 'Unique name',
},
},
});

/**
* Constant string used for default reason for a deprecation.
*/
Expand Down Expand Up @@ -218,6 +241,7 @@ export const GraphQLSpecifiedByDirective = new GraphQLDirective({
export const specifiedDirectives = Object.freeze([
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeferDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
]);
Expand Down
1 change: 1 addition & 0 deletions src/type/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ export {
specifiedDirectives,
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeferDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
// Constant Deprecation Reason
Expand Down
1 change: 1 addition & 0 deletions src/type/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export {
specifiedDirectives,
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeferDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
// Constant Deprecation Reason
Expand Down
15 changes: 10 additions & 5 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GraphQLIncludeDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
GraphQLDeferDirective,
} from '../../type/directives';
import {
GraphQLID,
Expand Down Expand Up @@ -221,12 +222,13 @@ describe('Schema Builder', () => {
expect(cycleSDL(sdl)).to.equal(sdl);
});

it('Maintains @include, @skip & @specifiedBy', () => {
it('Maintains specified directives', () => {
const schema = buildSchema('type Query');

expect(schema.getDirectives()).to.have.lengthOf(4);
expect(schema.getDirectives()).to.have.lengthOf(5);
expect(schema.getDirective('skip')).to.equal(GraphQLSkipDirective);
expect(schema.getDirective('include')).to.equal(GraphQLIncludeDirective);
expect(schema.getDirective('defer')).to.equal(GraphQLDeferDirective);
expect(schema.getDirective('deprecated')).to.equal(
GraphQLDeprecatedDirective,
);
Expand All @@ -241,9 +243,10 @@ describe('Schema Builder', () => {
directive @include on FIELD
directive @deprecated on FIELD_DEFINITION
directive @specifiedBy on FIELD_DEFINITION
directive @defer on FRAGMENT_SPREAD
`);

expect(schema.getDirectives()).to.have.lengthOf(4);
expect(schema.getDirectives()).to.have.lengthOf(5);
expect(schema.getDirective('skip')).to.not.equal(GraphQLSkipDirective);
expect(schema.getDirective('include')).to.not.equal(
GraphQLIncludeDirective,
Expand All @@ -254,16 +257,18 @@ describe('Schema Builder', () => {
expect(schema.getDirective('specifiedBy')).to.not.equal(
GraphQLSpecifiedByDirective,
);
expect(schema.getDirective('defer')).to.not.equal(GraphQLDeferDirective);
});

it('Adding directives maintains @include, @skip & @specifiedBy', () => {
it('Adding directives maintains specified directives', () => {
const schema = buildSchema(`
directive @foo(arg: Int) on FIELD
`);

expect(schema.getDirectives()).to.have.lengthOf(5);
expect(schema.getDirectives()).to.have.lengthOf(6);
expect(schema.getDirective('skip')).to.not.equal(undefined);
expect(schema.getDirective('include')).to.not.equal(undefined);
expect(schema.getDirective('defer')).to.not.equal(undefined);
expect(schema.getDirective('deprecated')).to.not.equal(undefined);
expect(schema.getDirective('specifiedBy')).to.not.equal(undefined);
});
Expand Down
2 changes: 2 additions & 0 deletions src/utilities/__tests__/findBreakingChanges-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { describe, it } from 'mocha';
import { GraphQLSchema } from '../../type/schema';
import {
GraphQLSkipDirective,
GraphQLDeferDirective,
GraphQLIncludeDirective,
GraphQLSpecifiedByDirective,
GraphQLDeprecatedDirective,
Expand Down Expand Up @@ -802,6 +803,7 @@ describe('findBreakingChanges', () => {
GraphQLSkipDirective,
GraphQLIncludeDirective,
GraphQLSpecifiedByDirective,
GraphQLDeferDirective,
],
});

Expand Down
11 changes: 11 additions & 0 deletions src/utilities/__tests__/printSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ describe('Type System Printer', () => {
if: Boolean!
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
"""
Directs the executor to defer this fragment when the \`if\` argument is true or undefined.
"""
directive @defer(
"""Deferred when true or undefined."""
if: Boolean
"""Unique name"""
label: String
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
"""Marks an element of a GraphQL schema as no longer supported."""
directive @deprecated(
"""
Expand Down

0 comments on commit efc6c73

Please sign in to comment.