Skip to content

Commit 709fc27

Browse files
committed
Add @stream directive to schema
1 parent b5dddb8 commit 709fc27

File tree

9 files changed

+44
-15
lines changed

9 files changed

+44
-15
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export {
6161
GraphQLIncludeDirective,
6262
GraphQLSkipDirective,
6363
GraphQLDeferDirective,
64+
GraphQLStreamDirective,
6465
GraphQLDeprecatedDirective,
6566
GraphQLSpecifiedByDirective,
6667
/** "Enum" of Type Kinds */

src/type/directives.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
GraphQLFieldConfigArgumentMap,
1414
} from './definition';
1515
import { assertName } from './assertName';
16-
import { GraphQLString, GraphQLBoolean } from './scalars';
16+
import { GraphQLString, GraphQLBoolean, GraphQLInt } from './scalars';
1717
import {
1818
defineArguments,
1919
argsToArgsConfig,
@@ -188,6 +188,31 @@ export const GraphQLDeferDirective = new GraphQLDirective({
188188
},
189189
});
190190

191+
/**
192+
* Used to conditionally stream list fields.
193+
*/
194+
export const GraphQLStreamDirective = new GraphQLDirective({
195+
name: 'stream',
196+
description:
197+
'Directs the executor to stream plural fields when the `if` argument is true or undefined.',
198+
locations: [DirectiveLocation.FIELD],
199+
args: {
200+
if: {
201+
type: GraphQLBoolean,
202+
description: 'Stream when true or undefined.',
203+
},
204+
label: {
205+
type: GraphQLString,
206+
description: 'Unique name',
207+
},
208+
initialCount: {
209+
defaultValue: 0,
210+
type: GraphQLInt,
211+
description: 'Number of items to return immediately',
212+
},
213+
},
214+
});
215+
191216
/**
192217
* Constant string used for default reason for a deprecation.
193218
*/

src/type/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export {
132132
GraphQLIncludeDirective,
133133
GraphQLSkipDirective,
134134
GraphQLDeferDirective,
135+
GraphQLStreamDirective,
135136
GraphQLDeprecatedDirective,
136137
GraphQLSpecifiedByDirective,
137138
/** Constant Deprecation Reason */

src/type/schema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type { GraphQLDirective } from './directives';
2525
import { __Schema } from './introspection';
2626
import {
2727
GraphQLDeferDirective,
28+
GraphQLStreamDirective,
2829
isDirective,
2930
specifiedDirectives,
3031
} from './directives';
@@ -187,6 +188,9 @@ export class GraphQLSchema {
187188
if (!this._directives.some((directive) => directive.name === 'defer')) {
188189
this._directives = [...this._directives, GraphQLDeferDirective];
189190
}
191+
if (!this._directives.some((directive) => directive.name === 'stream')) {
192+
this._directives = [...this._directives, GraphQLStreamDirective];
193+
}
190194
}
191195

192196
// To preserve order of user-provided types, we add first to add them to

src/utilities/__tests__/buildASTSchema-test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
GraphQLDeprecatedDirective,
2121
GraphQLSpecifiedByDirective,
2222
GraphQLDeferDirective,
23+
GraphQLStreamDirective,
2324
} from '../../type/directives';
2425
import {
2526
GraphQLID,
@@ -156,8 +157,7 @@ describe('Schema Builder', () => {
156157
it('include standard type only if it is used', () => {
157158
const schema = buildSchema('type Query');
158159

159-
// String and Boolean are always included through introspection types
160-
expect(schema.getType('Int')).to.equal(undefined);
160+
// String, Boolean, and Int are always included through introspection types
161161
expect(schema.getType('Float')).to.equal(undefined);
162162
expect(schema.getType('ID')).to.equal(undefined);
163163
});
@@ -242,13 +242,14 @@ describe('Schema Builder', () => {
242242
directive @deprecated on FIELD_DEFINITION
243243
directive @specifiedBy on FIELD_DEFINITION
244244
directive @defer on FRAGMENT_SPREAD
245+
directive @stream on FIELD
245246
`,
246247
{
247248
enableDeferStream: true,
248249
},
249250
);
250251

251-
expect(schema.getDirectives()).to.have.lengthOf(5);
252+
expect(schema.getDirectives()).to.have.lengthOf(6);
252253
expect(schema.getDirective('skip')).to.not.equal(GraphQLSkipDirective);
253254
expect(schema.getDirective('include')).to.not.equal(
254255
GraphQLIncludeDirective,
@@ -260,6 +261,7 @@ describe('Schema Builder', () => {
260261
GraphQLSpecifiedByDirective,
261262
);
262263
expect(schema.getDirective('defer')).to.not.equal(GraphQLDeferDirective);
264+
expect(schema.getDirective('stream')).to.not.equal(GraphQLStreamDirective);
263265
});
264266

265267
it('Adding directives maintains @include, @skip & @specifiedBy', () => {
@@ -274,13 +276,14 @@ describe('Schema Builder', () => {
274276
expect(schema.getDirective('specifiedBy')).to.not.equal(undefined);
275277
});
276278

277-
it('Adds @defer when enableDeferStream flag is passed to schema', () => {
279+
it('Adds @defer and stream when enableDeferStream flag is passed to schema', () => {
278280
const schema = buildSchema('type Query', {
279281
enableDeferStream: true,
280282
});
281283

282-
expect(schema.getDirectives()).to.have.lengthOf(5);
284+
expect(schema.getDirectives()).to.have.lengthOf(6);
283285
expect(schema.getDirective('defer')).to.equal(GraphQLDeferDirective);
286+
expect(schema.getDirective('stream')).to.equal(GraphQLStreamDirective);
284287
expect(schema.getDirective('skip')).to.equal(GraphQLSkipDirective);
285288
expect(schema.getDirective('include')).to.equal(GraphQLIncludeDirective);
286289
expect(schema.getDirective('deprecated')).to.equal(

src/utilities/__tests__/buildClientSchema-test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ describe('Type System: build schema from introspection', () => {
153153
const introspection = introspectionFromSchema(schema);
154154
const clientSchema = buildClientSchema(introspection);
155155

156-
expect(clientSchema.getType('Int')).to.equal(undefined);
157156
expect(clientSchema.getType('Float')).to.equal(undefined);
158157
expect(clientSchema.getType('ID')).to.equal(undefined);
159158
});

src/utilities/__tests__/extendSchema-test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ describe('extendSchema', () => {
131131
it('extends objects with standard type fields', () => {
132132
const schema = buildSchema('type Query');
133133

134-
// String and Boolean are always included through introspection types
135-
expect(schema.getType('Int')).to.equal(undefined);
134+
// String, Boolean, and Int are always included through introspection types
136135
expect(schema.getType('Float')).to.equal(undefined);
137136
expect(schema.getType('String')).to.equal(GraphQLString);
138137
expect(schema.getType('Boolean')).to.equal(GraphQLBoolean);
@@ -146,7 +145,6 @@ describe('extendSchema', () => {
146145
const extendedSchema = extendSchema(schema, extendAST);
147146

148147
expect(validateSchema(extendedSchema)).to.deep.equal([]);
149-
expect(extendedSchema.getType('Int')).to.equal(undefined);
150148
expect(extendedSchema.getType('Float')).to.equal(undefined);
151149
expect(extendedSchema.getType('String')).to.equal(GraphQLString);
152150
expect(extendedSchema.getType('Boolean')).to.equal(GraphQLBoolean);

src/utilities/__tests__/findBreakingChanges-test.ts

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

src/validation/__tests__/KnownTypeNamesRule-test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('Validate: Known type names', () => {
8181
it('references to standard scalars that are missing in schema', () => {
8282
const schema = buildSchema('type Query { foo: String }');
8383
const query = `
84-
query ($id: ID, $float: Float, $int: Int) {
84+
query ($id: ID, $float: Float) {
8585
__typename
8686
}
8787
`;
@@ -94,10 +94,6 @@ describe('Validate: Known type names', () => {
9494
message: 'Unknown type "Float".',
9595
locations: [{ line: 2, column: 31 }],
9696
},
97-
{
98-
message: 'Unknown type "Int".',
99-
locations: [{ line: 2, column: 44 }],
100-
},
10197
]);
10298
});
10399

0 commit comments

Comments
 (0)