Skip to content

Commit da7774d

Browse files
lilianammmatosrobrichard
authored andcommitted
Add @defer directive to specified directives
# Conflicts: # src/index.d.ts # src/type/directives.d.ts # src/type/directives.ts # src/type/index.js
1 parent 93bd84a commit da7774d

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export {
5555
specifiedDirectives,
5656
GraphQLIncludeDirective,
5757
GraphQLSkipDirective,
58+
GraphQLDeferDirective,
5859
GraphQLDeprecatedDirective,
5960
GraphQLSpecifiedByDirective,
6061
/** "Enum" of Type Kinds */

src/type/__tests__/introspection-test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,31 @@ describe('Introspection', () => {
936936
},
937937
],
938938
},
939+
{
940+
name: 'defer',
941+
isRepeatable: false,
942+
locations: ['FRAGMENT_SPREAD', 'INLINE_FRAGMENT'],
943+
args: [
944+
{
945+
defaultValue: null,
946+
name: 'if',
947+
type: {
948+
kind: 'SCALAR',
949+
name: 'Boolean',
950+
ofType: null,
951+
},
952+
},
953+
{
954+
defaultValue: null,
955+
name: 'label',
956+
type: {
957+
kind: 'SCALAR',
958+
name: 'String',
959+
ofType: null,
960+
},
961+
},
962+
],
963+
},
939964
{
940965
name: 'deprecated',
941966
isRepeatable: false,

src/type/directives.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,29 @@ export const GraphQLSkipDirective: GraphQLDirective = new GraphQLDirective({
166166
},
167167
});
168168

169+
/**
170+
* Used to conditionally defer fragments.
171+
*/
172+
export const GraphQLDeferDirective = new GraphQLDirective({
173+
name: 'defer',
174+
description:
175+
'Directs the executor to defer this fragment when the `if` argument is true or undefined.',
176+
locations: [
177+
DirectiveLocation.FRAGMENT_SPREAD,
178+
DirectiveLocation.INLINE_FRAGMENT,
179+
],
180+
args: {
181+
if: {
182+
type: GraphQLBoolean,
183+
description: 'Deferred when true or undefined.',
184+
},
185+
label: {
186+
type: GraphQLString,
187+
description: 'Unique name',
188+
},
189+
},
190+
});
191+
169192
/**
170193
* Constant string used for default reason for a deprecation.
171194
*/
@@ -217,6 +240,7 @@ export const specifiedDirectives: ReadonlyArray<GraphQLDirective> =
217240
Object.freeze([
218241
GraphQLIncludeDirective,
219242
GraphQLSkipDirective,
243+
GraphQLDeferDirective,
220244
GraphQLDeprecatedDirective,
221245
GraphQLSpecifiedByDirective,
222246
]);

src/type/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export {
129129
specifiedDirectives,
130130
GraphQLIncludeDirective,
131131
GraphQLSkipDirective,
132+
GraphQLDeferDirective,
132133
GraphQLDeprecatedDirective,
133134
GraphQLSpecifiedByDirective,
134135
/** Constant Deprecation Reason */

src/utilities/__tests__/buildASTSchema-test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
GraphQLIncludeDirective,
2020
GraphQLDeprecatedDirective,
2121
GraphQLSpecifiedByDirective,
22+
GraphQLDeferDirective,
2223
} from '../../type/directives';
2324
import {
2425
GraphQLID,
@@ -219,12 +220,13 @@ describe('Schema Builder', () => {
219220
expect(cycleSDL(sdl)).to.equal(sdl);
220221
});
221222

222-
it('Maintains @include, @skip & @specifiedBy', () => {
223+
it('Maintains specified directives', () => {
223224
const schema = buildSchema('type Query');
224225

225-
expect(schema.getDirectives()).to.have.lengthOf(4);
226+
expect(schema.getDirectives()).to.have.lengthOf(5);
226227
expect(schema.getDirective('skip')).to.equal(GraphQLSkipDirective);
227228
expect(schema.getDirective('include')).to.equal(GraphQLIncludeDirective);
229+
expect(schema.getDirective('defer')).to.equal(GraphQLDeferDirective);
228230
expect(schema.getDirective('deprecated')).to.equal(
229231
GraphQLDeprecatedDirective,
230232
);
@@ -239,9 +241,10 @@ describe('Schema Builder', () => {
239241
directive @include on FIELD
240242
directive @deprecated on FIELD_DEFINITION
241243
directive @specifiedBy on FIELD_DEFINITION
244+
directive @defer on FRAGMENT_SPREAD
242245
`);
243246

244-
expect(schema.getDirectives()).to.have.lengthOf(4);
247+
expect(schema.getDirectives()).to.have.lengthOf(5);
245248
expect(schema.getDirective('skip')).to.not.equal(GraphQLSkipDirective);
246249
expect(schema.getDirective('include')).to.not.equal(
247250
GraphQLIncludeDirective,
@@ -252,16 +255,18 @@ describe('Schema Builder', () => {
252255
expect(schema.getDirective('specifiedBy')).to.not.equal(
253256
GraphQLSpecifiedByDirective,
254257
);
258+
expect(schema.getDirective('defer')).to.not.equal(GraphQLDeferDirective);
255259
});
256260

257-
it('Adding directives maintains @include, @skip & @specifiedBy', () => {
261+
it('Adding directives maintains specified directives', () => {
258262
const schema = buildSchema(`
259263
directive @foo(arg: Int) on FIELD
260264
`);
261265

262-
expect(schema.getDirectives()).to.have.lengthOf(5);
266+
expect(schema.getDirectives()).to.have.lengthOf(6);
263267
expect(schema.getDirective('skip')).to.not.equal(undefined);
264268
expect(schema.getDirective('include')).to.not.equal(undefined);
269+
expect(schema.getDirective('defer')).to.not.equal(undefined);
265270
expect(schema.getDirective('deprecated')).to.not.equal(undefined);
266271
expect(schema.getDirective('specifiedBy')).to.not.equal(undefined);
267272
});

src/utilities/__tests__/findBreakingChanges-test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { describe, it } from 'mocha';
44
import { GraphQLSchema } from '../../type/schema';
55
import {
66
GraphQLSkipDirective,
7+
GraphQLDeferDirective,
78
GraphQLIncludeDirective,
89
GraphQLSpecifiedByDirective,
910
GraphQLDeprecatedDirective,
@@ -802,6 +803,7 @@ describe('findBreakingChanges', () => {
802803
GraphQLSkipDirective,
803804
GraphQLIncludeDirective,
804805
GraphQLSpecifiedByDirective,
806+
GraphQLDeferDirective,
805807
],
806808
});
807809

src/utilities/__tests__/printSchema-test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,17 @@ describe('Type System Printer', () => {
628628
if: Boolean!
629629
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
630630
631+
"""
632+
Directs the executor to defer this fragment when the \`if\` argument is true or undefined.
633+
"""
634+
directive @defer(
635+
"""Deferred when true or undefined."""
636+
if: Boolean
637+
638+
"""Unique name"""
639+
label: String
640+
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
641+
631642
"""Marks an element of a GraphQL schema as no longer supported."""
632643
directive @deprecated(
633644
"""

0 commit comments

Comments
 (0)