Skip to content

Commit f2dc7e7

Browse files
lilianammmatosrobrichard
authored andcommitted
Add @defer directive to specified directives
1 parent f4f3f09 commit f2dc7e7

File tree

9 files changed

+89
-5
lines changed

9 files changed

+89
-5
lines changed

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
@@ -168,6 +168,29 @@ export const GraphQLSkipDirective = new GraphQLDirective({
168168
},
169169
});
170170

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

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,
@@ -251,12 +252,13 @@ describe('Schema Builder', () => {
251252
expect(cycleSDL(sdl, { commentDescriptions: true })).to.equal(sdl);
252253
});
253254

254-
it('Maintains @include, @skip & @specifiedBy', () => {
255+
it('Maintains specified directives', () => {
255256
const schema = buildSchema('type Query');
256257

257-
expect(schema.getDirectives()).to.have.lengthOf(4);
258+
expect(schema.getDirectives()).to.have.lengthOf(5);
258259
expect(schema.getDirective('skip')).to.equal(GraphQLSkipDirective);
259260
expect(schema.getDirective('include')).to.equal(GraphQLIncludeDirective);
261+
expect(schema.getDirective('defer')).to.equal(GraphQLDeferDirective);
260262
expect(schema.getDirective('deprecated')).to.equal(
261263
GraphQLDeprecatedDirective,
262264
);
@@ -271,9 +273,10 @@ describe('Schema Builder', () => {
271273
directive @include on FIELD
272274
directive @deprecated on FIELD_DEFINITION
273275
directive @specifiedBy on FIELD_DEFINITION
276+
directive @defer on FRAGMENT_SPREAD
274277
`);
275278

276-
expect(schema.getDirectives()).to.have.lengthOf(4);
279+
expect(schema.getDirectives()).to.have.lengthOf(5);
277280
expect(schema.getDirective('skip')).to.not.equal(GraphQLSkipDirective);
278281
expect(schema.getDirective('include')).to.not.equal(
279282
GraphQLIncludeDirective,
@@ -284,16 +287,18 @@ describe('Schema Builder', () => {
284287
expect(schema.getDirective('specifiedBy')).to.not.equal(
285288
GraphQLSpecifiedByDirective,
286289
);
290+
expect(schema.getDirective('defer')).to.not.equal(GraphQLDeferDirective);
287291
});
288292

289-
it('Adding directives maintains @include, @skip & @specifiedBy', () => {
293+
it('Adding directives maintains specified directives', () => {
290294
const schema = buildSchema(`
291295
directive @foo(arg: Int) on FIELD
292296
`);
293297

294-
expect(schema.getDirectives()).to.have.lengthOf(5);
298+
expect(schema.getDirectives()).to.have.lengthOf(6);
295299
expect(schema.getDirective('skip')).to.not.equal(undefined);
296300
expect(schema.getDirective('include')).to.not.equal(undefined);
301+
expect(schema.getDirective('defer')).to.not.equal(undefined);
297302
expect(schema.getDirective('deprecated')).to.not.equal(undefined);
298303
expect(schema.getDirective('specifiedBy')).to.not.equal(undefined);
299304
});

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: 20 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
"""
@@ -852,6 +863,15 @@ describe('Type System Printer', () => {
852863
if: Boolean!
853864
) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
854865
866+
# Directs the executor to defer this fragment when the \`if\` argument is true or undefined.
867+
directive @defer(
868+
# Deferred when true or undefined.
869+
if: Boolean
870+
871+
# Unique name
872+
label: String
873+
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
874+
855875
# Marks an element of a GraphQL schema as no longer supported.
856876
directive @deprecated(
857877
# Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).

0 commit comments

Comments
 (0)