Skip to content

Commit d5c9dab

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 2153dda commit d5c9dab

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
@@ -948,6 +948,31 @@ describe('Introspection', () => {
948948
},
949949
],
950950
},
951+
{
952+
name: 'defer',
953+
isRepeatable: false,
954+
locations: ['FRAGMENT_SPREAD', 'INLINE_FRAGMENT'],
955+
args: [
956+
{
957+
defaultValue: null,
958+
name: 'if',
959+
type: {
960+
kind: 'SCALAR',
961+
name: 'Boolean',
962+
ofType: null,
963+
},
964+
},
965+
{
966+
defaultValue: null,
967+
name: 'label',
968+
type: {
969+
kind: 'SCALAR',
970+
name: 'String',
971+
ofType: null,
972+
},
973+
},
974+
],
975+
},
951976
{
952977
name: 'deprecated',
953978
isRepeatable: false,

src/type/directives.ts

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

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

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)