Skip to content

Commit a3b278f

Browse files
committed
set up new directive
1 parent 815896d commit a3b278f

File tree

6 files changed

+64
-3
lines changed

6 files changed

+64
-3
lines changed

src/type/directives.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,30 @@ export const GraphQLDeferDirective = new GraphQLDirective({
192192
},
193193
});
194194

195+
/**
196+
* Used to conditionally defer fragments.
197+
*/
198+
export const GraphQLStreamDirective = new GraphQLDirective({
199+
name: 'stream',
200+
description:
201+
'Directs the executor to stream plural fields when the `if` argument is true or undefined.',
202+
locations: [DirectiveLocation.FIELD],
203+
args: {
204+
if: {
205+
type: GraphQLBoolean,
206+
description: 'Stream when true or undefined.',
207+
},
208+
label: {
209+
type: GraphQLNonNull(GraphQLString),
210+
description: 'Unique name',
211+
},
212+
initial_count: {
213+
type: GraphQLNonNull(GraphQLInt),
214+
description: 'Number of items to return immediately',
215+
},
216+
},
217+
});
218+
195219
/**
196220
* Constant string used for default reason for a deprecation.
197221
*/
@@ -220,7 +244,6 @@ export const GraphQLDeprecatedDirective = new GraphQLDirective({
220244
export const specifiedDirectives = Object.freeze([
221245
GraphQLIncludeDirective,
222246
GraphQLSkipDirective,
223-
GraphQLDeferDirective,
224247
GraphQLDeprecatedDirective,
225248
]);
226249

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: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
import { __Schema } from './introspection';
2525
import {
2626
GraphQLDirective,
27+
GraphQLStreamDirective,
28+
GraphQLDeferDirective,
2729
isDirective,
2830
specifiedDirectives,
2931
} from './directives';
@@ -136,6 +138,7 @@ export class GraphQLSchema {
136138
__validationErrors: ?$ReadOnlyArray<GraphQLError>;
137139
// Referenced by execute()
138140
__experimentalDeferFragmentSpreads: boolean;
141+
__experimentalStream: boolean;
139142

140143
constructor(config: $ReadOnly<GraphQLSchemaConfig>): void {
141144
// If this schema was built from a source known to be valid, then it may be
@@ -163,14 +166,27 @@ 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;
171173
// Provide specified directives (e.g. @include and @skip) by default.
172174
this._directives = config.directives || specifiedDirectives;
173175

176+
if (config.experimentalDeferFragmentSpreads) {
177+
this.__experimentalDeferFragmentSpreads = true;
178+
this._directives = [].concat(this._directives, [GraphQLDeferDirective]);
179+
} else {
180+
this.__experimentalDeferFragmentSpreads = false;
181+
}
182+
183+
if (config.experimentalStream) {
184+
this.__experimentalStream = true;
185+
this._directives = [].concat(this._directives, [GraphQLStreamDirective]);
186+
} else {
187+
this.__experimentalStream = false;
188+
}
189+
174190
// Build type map now to detect any errors within this schema.
175191
const initialTypes: Array<?GraphQLNamedType> = [
176192
this._queryType,
@@ -320,6 +336,18 @@ export type GraphQLSchemaValidationOptions = {|
320336
* Default: false
321337
*/
322338
experimentalDeferFragmentSpreads?: boolean,
339+
340+
/**
341+
*
342+
* EXPERIMENTAL:
343+
*
344+
* If enabled, items from a plural fields with @stream directive
345+
* are not returned from the iniital query and each item is returned
346+
* in a patch after the initial result from the synchronous query.
347+
*
348+
* Default: false
349+
*/
350+
experimentalStream?: boolean,
323351
|};
324352

325353
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';
@@ -795,6 +796,7 @@ describe('findBreakingChanges', () => {
795796
GraphQLSkipDirective,
796797
GraphQLIncludeDirective,
797798
GraphQLDeferDirective,
799+
GraphQLStreamDirective,
798800
],
799801
});
800802

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)