Skip to content

Commit ac7333f

Browse files
robrichardlilianammmatos
authored andcommitted
set up new directive
1 parent 9e3e6dc commit ac7333f

File tree

6 files changed

+63
-3
lines changed

6 files changed

+63
-3
lines changed

src/type/directives.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ export const GraphQLDeferDirective = new GraphQLDirective({
195195
},
196196
});
197197

198+
/**
199+
* Used to conditionally defer fragments.
200+
*/
201+
export const GraphQLStreamDirective = new GraphQLDirective({
202+
name: 'stream',
203+
description:
204+
'Directs the executor to stream plural fields when the `if` argument is true or undefined.',
205+
locations: [DirectiveLocation.FIELD],
206+
args: {
207+
if: {
208+
type: GraphQLBoolean,
209+
description: 'Stream when true or undefined.',
210+
},
211+
label: {
212+
type: GraphQLNonNull(GraphQLString),
213+
description: 'Unique name',
214+
},
215+
initial_count: {
216+
type: GraphQLNonNull(GraphQLInt),
217+
description: 'Number of items to return immediately',
218+
},
219+
},
220+
});
221+
198222
/**
199223
* Constant string used for default reason for a deprecation.
200224
*/
@@ -223,7 +247,6 @@ export const GraphQLDeprecatedDirective = new GraphQLDirective({
223247
export const specifiedDirectives = Object.freeze([
224248
GraphQLIncludeDirective,
225249
GraphQLSkipDirective,
226-
GraphQLDeferDirective,
227250
GraphQLDeprecatedDirective,
228251
]);
229252

src/type/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export {
7979
GraphQLIncludeDirective,
8080
GraphQLSkipDirective,
8181
GraphQLDeferDirective,
82+
GraphQLStreamDirective,
8283
GraphQLDeprecatedDirective,
8384
// Constant Deprecation Reason
8485
DEFAULT_DEPRECATION_REASON,

src/type/schema.js

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import {
2525
import { __Schema } from './introspection';
2626
import {
2727
GraphQLDirective,
28+
GraphQLStreamDirective,
29+
GraphQLDeferDirective,
2830
isDirective,
2931
specifiedDirectives,
3032
} from './directives';
@@ -141,6 +143,7 @@ export class GraphQLSchema {
141143
__validationErrors: ?$ReadOnlyArray<GraphQLError>;
142144
// Referenced by execute()
143145
__experimentalDeferFragmentSpreads: boolean;
146+
__experimentalStream: boolean;
144147

145148
constructor(config: $ReadOnly<GraphQLSchemaConfig>): void {
146149
// If this schema was built from a source known to be valid, then it may be
@@ -163,8 +166,7 @@ export class GraphQLSchema {
163166
this.astNode = config.astNode;
164167
this.extensionASTNodes = config.extensionASTNodes;
165168

166-
this.__experimentalDeferFragmentSpreads =
167-
config.experimentalDeferFragmentSpreads || false;
169+
this.__experimentalStream = config.experimentalStream || false;
168170
this._queryType = config.query;
169171
this._mutationType = config.mutation;
170172
this._subscriptionType = config.subscription;
@@ -182,6 +184,19 @@ export class GraphQLSchema {
182184
collectReferencedTypes(type, allReferencedTypes);
183185
}
184186
}
187+
if (config.experimentalDeferFragmentSpreads) {
188+
this.__experimentalDeferFragmentSpreads = true;
189+
this._directives = [].concat(this._directives, [GraphQLDeferDirective]);
190+
} else {
191+
this.__experimentalDeferFragmentSpreads = false;
192+
}
193+
194+
if (config.experimentalStream) {
195+
this.__experimentalStream = true;
196+
this._directives = [].concat(this._directives, [GraphQLStreamDirective]);
197+
} else {
198+
this.__experimentalStream = false;
199+
}
185200

186201
if (this._queryType != null) {
187202
collectReferencedTypes(this._queryType, allReferencedTypes);
@@ -380,6 +395,18 @@ export type GraphQLSchemaValidationOptions = {|
380395
* Default: false
381396
*/
382397
experimentalDeferFragmentSpreads?: boolean,
398+
399+
/**
400+
*
401+
* EXPERIMENTAL:
402+
*
403+
* If enabled, items from a plural fields with @stream directive
404+
* are not returned from the iniital query and each item is returned
405+
* in a patch after the initial result from the synchronous query.
406+
*
407+
* Default: false
408+
*/
409+
experimentalStream?: boolean,
383410
|};
384411

385412
export type GraphQLSchemaConfig = {|

src/utilities/__tests__/findBreakingChanges-test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { GraphQLSchema } from '../../type/schema';
77
import {
88
GraphQLSkipDirective,
99
GraphQLDeferDirective,
10+
GraphQLStreamDirective,
1011
GraphQLIncludeDirective,
1112
GraphQLDeprecatedDirective,
1213
} from '../../type/directives';
@@ -804,6 +805,7 @@ describe('findBreakingChanges', () => {
804805
GraphQLSkipDirective,
805806
GraphQLIncludeDirective,
806807
GraphQLDeferDirective,
808+
GraphQLStreamDirective,
807809
],
808810
});
809811

src/utilities/buildASTSchema.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
GraphQLSkipDirective,
1818
GraphQLIncludeDirective,
1919
GraphQLDeferDirective,
20+
GraphQLStreamDirective,
2021
GraphQLDeprecatedDirective,
2122
} from '../type/directives';
2223

@@ -110,6 +111,10 @@ export function buildASTSchema(
110111
directives.push(GraphQLDeferDirective);
111112
}
112113

114+
if (!directives.some(directive => directive.name === 'stream')) {
115+
directives.push(GraphQLStreamDirective);
116+
}
117+
113118
if (!directives.some(directive => directive.name === 'deprecated')) {
114119
directives.push(GraphQLDeprecatedDirective);
115120
}

src/validation/__tests__/harness.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
GraphQLIncludeDirective,
1313
GraphQLSkipDirective,
1414
GraphQLDeferDirective,
15+
GraphQLStreamDirective,
1516
} from '../../type/directives';
1617
import {
1718
GraphQLInt,
@@ -364,6 +365,7 @@ export const testSchema = new GraphQLSchema({
364365
GraphQLIncludeDirective,
365366
GraphQLSkipDirective,
366367
GraphQLDeferDirective,
368+
GraphQLStreamDirective,
367369
new GraphQLDirective({
368370
name: 'onQuery',
369371
locations: ['QUERY'],

0 commit comments

Comments
 (0)