Skip to content

Commit b22640c

Browse files
committed
proof of concept
1 parent e90a6cd commit b22640c

File tree

3 files changed

+108
-20
lines changed

3 files changed

+108
-20
lines changed

packages/delegate/src/defaultMergedResolver.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defaultFieldResolver, GraphQLResolveInfo } from 'graphql';
22

3-
import { getResponseKeyFromInfo } from '@graphql-tools/utils';
3+
import { getResponseKeyFromInfo, ExecutionResult } from '@graphql-tools/utils';
44

55
import { resolveExternalValue } from './resolveExternalValue';
66
import { getSubschema } from './Subschema';
@@ -18,7 +18,7 @@ export function defaultMergedResolver(
1818
args: Record<string, any>,
1919
context: Record<string, any>,
2020
info: GraphQLResolveInfo
21-
) {
21+
): any {
2222
if (!parent) {
2323
return null;
2424
}
@@ -34,16 +34,26 @@ export function defaultMergedResolver(
3434
const data = parent[responseKey];
3535
const unpathedErrors = getUnpathedErrors(parent);
3636

37-
// To Do: extract this section to separate function.
38-
// If proxied result with defer or stream, result may be initially undefined
39-
// so must call out to a to-be-created Receiver abstraction
40-
// (as opposed to Dispatcher within graphql-js) that will notify of data arrival
41-
42-
if (data === undefined) {
43-
return 'test';
37+
// To Do:
38+
// add support for transforms
39+
// call out to Receiver abstraction that will publish all patches with channel based on path
40+
// edit code below to subscribe to appropriate channel based on path
41+
// so as to handle multiple defer patches and discriminate between them without need for labels
42+
43+
if (data === undefined && 'ASYNC_ITERABLE' in parent) {
44+
const asyncIterable = parent['ASYNC_ITERABLE'];
45+
return asyncIterableToResult(asyncIterable).then(patch => {
46+
return defaultMergedResolver(patch.data, args, context, info);
47+
});
4448
}
4549

4650
const subschema = getSubschema(parent, responseKey);
4751

4852
return resolveExternalValue(data, unpathedErrors, subschema, context, info);
4953
}
54+
55+
async function asyncIterableToResult(asyncIterable: AsyncIterable<ExecutionResult>): Promise<any> {
56+
const asyncIterator = asyncIterable[Symbol.asyncIterator]();
57+
const payload = await asyncIterator.next();
58+
return payload.value;
59+
}

packages/delegate/src/delegateToSchema.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
subscribe,
3-
execute,
43
validate,
54
GraphQLSchema,
65
isSchema,
@@ -13,9 +12,11 @@ import {
1312
GraphQLObjectType,
1413
} from 'graphql';
1514

15+
import { execute } from 'graphql/experimental';
16+
1617
import isPromise from 'is-promise';
1718

18-
import { mapAsyncIterator, ExecutionResult } from '@graphql-tools/utils';
19+
import { mapAsyncIterator, ExecutionResult, isAsyncIterable } from '@graphql-tools/utils';
1920

2021
import {
2122
IDelegateToSchemaOptions,
@@ -175,8 +176,16 @@ export function delegateRequest({
175176
info,
176177
});
177178

178-
if (isPromise(executionResult)) {
179-
return executionResult.then(originalResult => transformer.transformResult(originalResult));
179+
if (isAsyncIterable(executionResult)) {
180+
return asyncIterableToResult(executionResult).then(originalResult => {
181+
const transformedResult = transformer.transformResult(originalResult);
182+
transformedResult['ASYNC_ITERABLE'] = executionResult;
183+
return transformedResult;
184+
});
185+
} else if (isPromise(executionResult)) {
186+
return (executionResult as Promise<ExecutionResult>).then(originalResult =>
187+
transformer.transformResult(originalResult)
188+
);
180189
}
181190
return transformer.transformResult(executionResult);
182191
}
@@ -190,7 +199,7 @@ export function delegateRequest({
190199
context,
191200
info,
192201
}).then((subscriptionResult: AsyncIterableIterator<ExecutionResult> | ExecutionResult) => {
193-
if (Symbol.asyncIterator in subscriptionResult) {
202+
if (isAsyncIterable(subscriptionResult)) {
194203
// "subscribe" to the subscription result and map the result through the transforms
195204
return mapAsyncIterator<ExecutionResult, any>(
196205
subscriptionResult as AsyncIterableIterator<ExecutionResult>,
@@ -237,3 +246,9 @@ function createDefaultSubscriber(schema: GraphQLSchema, rootValue: Record<string
237246
rootValue: rootValue ?? info?.rootValue,
238247
}) as any;
239248
}
249+
250+
async function asyncIterableToResult(asyncIterable: AsyncIterable<ExecutionResult>): Promise<any> {
251+
const asyncIterator = asyncIterable[Symbol.asyncIterator]();
252+
const payload = await asyncIterator.next();
253+
return payload.value;
254+
}

packages/delegate/tests/deferStream.test.ts

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
import { graphql } from 'graphql/experimental';
22

33
import { makeExecutableSchema } from '@graphql-tools/schema';
4+
import { stitchSchemas } from '@graphql-tools/stitch';
5+
import { isAsyncIterable } from '@graphql-tools/utils';
46

57
describe('defer support', () => {
6-
test('should work', async () => {
8+
test('should work for root fields', async () => {
79
const schema = makeExecutableSchema({
810
typeDefs: `
911
type Query {
10-
test(input: String): String
12+
test: String
1113
}
1214
`,
1315
resolvers: {
1416
Query: {
15-
test: (_root, args) => args.input,
17+
test: () => 'test',
1618
}
1719
},
1820
});
1921

22+
const stitchedSchema = stitchSchemas({
23+
subschemas: [schema]
24+
});
25+
2026
const result = await graphql(
21-
schema,
27+
stitchedSchema,
2228
`
2329
query {
2430
... on Query @defer {
25-
test(input: "test")
31+
test
2632
}
2733
}
2834
`,
2935
);
3036

3137
const results = [];
32-
if (result[Symbol.asyncIterator]) {
38+
if (isAsyncIterable(result)) {
3339
for await (let patch of result) {
3440
results.push(patch);
3541
}
@@ -46,6 +52,63 @@ describe('defer support', () => {
4652
hasNext: false,
4753
path: [],
4854
});
55+
});
4956

57+
test('should work for nested fields', async () => {
58+
const schema = makeExecutableSchema({
59+
typeDefs: `
60+
type Object {
61+
test: String
62+
}
63+
type Query {
64+
object: Object
65+
}
66+
`,
67+
resolvers: {
68+
Object: {
69+
test: () => 'test',
70+
},
71+
Query: {
72+
object: () => ({}),
73+
}
74+
},
75+
});
76+
77+
const stitchedSchema = stitchSchemas({
78+
subschemas: [schema]
79+
});
80+
81+
const result = await graphql(
82+
stitchedSchema,
83+
`
84+
query {
85+
object {
86+
... on Object @defer {
87+
test
88+
}
89+
}
90+
}
91+
`,
92+
);
93+
94+
const results = [];
95+
if (isAsyncIterable(result)) {
96+
for await (let patch of result) {
97+
results.push(patch);
98+
}
99+
}
100+
101+
expect(results[0]).toEqual({
102+
data: { object: {} },
103+
hasNext: true,
104+
});
105+
expect(results[1]).toEqual({
106+
data: {
107+
test: 'test'
108+
},
109+
hasNext: false,
110+
path: ['object'],
111+
});
50112
});
51113
});
114+

0 commit comments

Comments
 (0)