Skip to content

Commit 2a67291

Browse files
lilianammmatosrobrichard
authored andcommitted
Add @defer directive to specified directives
1 parent a2707bb commit 2a67291

File tree

10 files changed

+81
-5
lines changed

10 files changed

+81
-5
lines changed

src/index.d.ts

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

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export {
5353
specifiedDirectives,
5454
GraphQLIncludeDirective,
5555
GraphQLSkipDirective,
56+
GraphQLDeferDirective,
5657
GraphQLDeprecatedDirective,
5758
GraphQLSpecifiedByDirective,
5859
// "Enum" of Type Kinds

src/type/__tests__/introspection-test.js

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.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ export const GraphQLIncludeDirective: GraphQLDirective;
7373
*/
7474
export const GraphQLSkipDirective: GraphQLDirective;
7575

76+
/**
77+
* Used to conditionally defer fragments.
78+
*/
79+
export const GraphQLDeferDirective: GraphQLDirective;
80+
7681
/**
7782
* Used to provide a URL for specifying the behavior of custom scalar definitions.
7883
*/

src/type/directives.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,29 @@ export const GraphQLSkipDirective = new GraphQLDirective({
163163
},
164164
});
165165

166+
/**
167+
* Used to conditionally defer fragments.
168+
*/
169+
export const GraphQLDeferDirective = new GraphQLDirective({
170+
name: 'defer',
171+
description:
172+
'Directs the executor to defer this fragment when the `if` argument is true or undefined.',
173+
locations: [
174+
DirectiveLocation.FRAGMENT_SPREAD,
175+
DirectiveLocation.INLINE_FRAGMENT,
176+
],
177+
args: {
178+
if: {
179+
type: GraphQLBoolean,
180+
description: 'Deferred when true or undefined.',
181+
},
182+
label: {
183+
type: GraphQLString,
184+
description: 'Unique name',
185+
},
186+
},
187+
});
188+
166189
/**
167190
* Constant string used for default reason for a deprecation.
168191
*/
@@ -211,6 +234,7 @@ export const GraphQLSpecifiedByDirective = new GraphQLDirective({
211234
export const specifiedDirectives = Object.freeze([
212235
GraphQLIncludeDirective,
213236
GraphQLSkipDirective,
237+
GraphQLDeferDirective,
214238
GraphQLDeprecatedDirective,
215239
GraphQLSpecifiedByDirective,
216240
]);

src/type/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export {
125125
specifiedDirectives,
126126
GraphQLIncludeDirective,
127127
GraphQLSkipDirective,
128+
GraphQLDeferDirective,
128129
GraphQLDeprecatedDirective,
129130
GraphQLSpecifiedByDirective,
130131
// Constant Deprecation Reason

src/type/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export {
7676
specifiedDirectives,
7777
GraphQLIncludeDirective,
7878
GraphQLSkipDirective,
79+
GraphQLDeferDirective,
7980
GraphQLDeprecatedDirective,
8081
GraphQLSpecifiedByDirective,
8182
// Constant Deprecation Reason

src/utilities/__tests__/buildASTSchema-test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
GraphQLIncludeDirective,
2121
GraphQLDeprecatedDirective,
2222
GraphQLSpecifiedByDirective,
23+
GraphQLDeferDirective,
2324
} from '../../type/directives';
2425
import {
2526
GraphQLID,
@@ -221,12 +222,13 @@ describe('Schema Builder', () => {
221222
expect(cycleSDL(sdl)).to.equal(sdl);
222223
});
223224

224-
it('Maintains @include, @skip & @specifiedBy', () => {
225+
it('Maintains specified directives', () => {
225226
const schema = buildSchema('type Query');
226227

227-
expect(schema.getDirectives()).to.have.lengthOf(4);
228+
expect(schema.getDirectives()).to.have.lengthOf(5);
228229
expect(schema.getDirective('skip')).to.equal(GraphQLSkipDirective);
229230
expect(schema.getDirective('include')).to.equal(GraphQLIncludeDirective);
231+
expect(schema.getDirective('defer')).to.equal(GraphQLDeferDirective);
230232
expect(schema.getDirective('deprecated')).to.equal(
231233
GraphQLDeprecatedDirective,
232234
);
@@ -241,9 +243,10 @@ describe('Schema Builder', () => {
241243
directive @include on FIELD
242244
directive @deprecated on FIELD_DEFINITION
243245
directive @specifiedBy on FIELD_DEFINITION
246+
directive @defer on FRAGMENT_SPREAD
244247
`);
245248

246-
expect(schema.getDirectives()).to.have.lengthOf(4);
249+
expect(schema.getDirectives()).to.have.lengthOf(5);
247250
expect(schema.getDirective('skip')).to.not.equal(GraphQLSkipDirective);
248251
expect(schema.getDirective('include')).to.not.equal(
249252
GraphQLIncludeDirective,
@@ -254,16 +257,18 @@ describe('Schema Builder', () => {
254257
expect(schema.getDirective('specifiedBy')).to.not.equal(
255258
GraphQLSpecifiedByDirective,
256259
);
260+
expect(schema.getDirective('defer')).to.not.equal(GraphQLDeferDirective);
257261
});
258262

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

264-
expect(schema.getDirectives()).to.have.lengthOf(5);
268+
expect(schema.getDirectives()).to.have.lengthOf(6);
265269
expect(schema.getDirective('skip')).to.not.equal(undefined);
266270
expect(schema.getDirective('include')).to.not.equal(undefined);
271+
expect(schema.getDirective('defer')).to.not.equal(undefined);
267272
expect(schema.getDirective('deprecated')).to.not.equal(undefined);
268273
expect(schema.getDirective('specifiedBy')).to.not.equal(undefined);
269274
});

src/utilities/__tests__/findBreakingChanges-test.js

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.js

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

0 commit comments

Comments
 (0)