Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 482d813

Browse files
committedOct 9, 2023
Improvements on runtime and JIT logic
1 parent 181f0e4 commit 482d813

File tree

14 files changed

+233
-80
lines changed

14 files changed

+233
-80
lines changed
 

‎.changeset/grumpy-onions-fold.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/transform-federation': patch
3+
---
4+
5+
Remove other directives in scalars just like it is done for objects and other types

‎.changeset/hot-bugs-hammer.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/runtime': patch
3+
---
4+
5+
Simplify the logic and use GraphQL Tools executor

‎.changeset/many-turkeys-share.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/grpc': patch
3+
---
4+
5+
Add response streams as subscriptions

‎.changeset/seven-bags-exist.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/runtime': patch
3+
---
4+
5+
Do not cache entire request but only DocumentNode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
subscription SearchMoviesByCast {
2+
exampleSearchMoviesByCast(input: { castName: "Tom Cruise" }) {
3+
name
4+
year
5+
rating
6+
cast
7+
}
8+
}

‎examples/grpc-example/tests/__snapshots__/grpc.test.ts.snap

+46-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-0 1`] = `
3+
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-stream-0 1`] = `
44
{
55
"data": {
66
"exampleSearchMoviesByCast": [],
@@ -9,7 +9,7 @@ exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-
99
}
1010
`;
1111

12-
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-1 1`] = `
12+
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-stream-1 1`] = `
1313
{
1414
"hasNext": true,
1515
"incremental": [
@@ -35,7 +35,7 @@ exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-
3535
}
3636
`;
3737

38-
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-2 1`] = `
38+
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-stream-2 1`] = `
3939
{
4040
"hasNext": true,
4141
"incremental": [
@@ -61,15 +61,50 @@ exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-
6161
}
6262
`;
6363

64-
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-3 1`] = `
64+
exports[`gRPC Example should fetch movies by cast as a stream correctly: movies-by-cast-grpc-example-result-stream-3 1`] = `
6565
{
6666
"hasNext": false,
6767
}
6868
`;
6969

70+
exports[`gRPC Example should fetch movies by cast as a subscription correctly: movies-by-cast-grpc-example-result-subscription-0 1`] = `
71+
{
72+
"data": {
73+
"exampleSearchMoviesByCast": {
74+
"cast": [
75+
"Tom Cruise",
76+
"Simon Pegg",
77+
"Jeremy Renner",
78+
],
79+
"name": "Mission: Impossible Rogue Nation",
80+
"rating": 0.9700000286102295,
81+
"year": 2015,
82+
},
83+
},
84+
}
85+
`;
86+
87+
exports[`gRPC Example should fetch movies by cast as a subscription correctly: movies-by-cast-grpc-example-result-subscription-1 1`] = `
88+
{
89+
"data": {
90+
"exampleSearchMoviesByCast": {
91+
"cast": [
92+
"Tom Cruise",
93+
"Simon Pegg",
94+
"Henry Cavill",
95+
],
96+
"name": "Mission: Impossible - Fallout",
97+
"rating": 0.9300000071525574,
98+
"year": 2018,
99+
},
100+
},
101+
}
102+
`;
103+
70104
exports[`gRPC Example should generate correct schema: grpc-schema 1`] = `
71105
"schema {
72106
query: Query
107+
subscription: Subscription
73108
}
74109
75110
directive @grpcMethod(rootJsonName: String, objPath: String, methodName: String, responseStream: Boolean) on FIELD_DEFINITION
@@ -166,6 +201,13 @@ enum ConnectivityState {
166201
SHUTDOWN
167202
}
168203
204+
type Subscription {
205+
"search movies by the name of the cast"
206+
exampleSearchMoviesByCast(input: SearchByCastRequest_Input): Movie @grpcMethod(rootJsonName: "Root0", objPath: "Example", methodName: "SearchMoviesByCast", responseStream: true)
207+
"search movies by the name of the cast"
208+
anotherExampleSearchMoviesByCast(input: SearchByCastRequest_Input): Movie @grpcMethod(rootJsonName: "Root0", objPath: "AnotherExample", methodName: "SearchMoviesByCast", responseStream: true)
209+
}
210+
169211
scalar ObjMap"
170212
`;
171213

‎examples/grpc-example/tests/grpc.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ describe('gRPC Example', () => {
3636
const result = await mesh.execute(MoviesByCastStream, undefined);
3737
let i = 0;
3838
for await (const item of result as AsyncIterable<any>) {
39-
expect(item).toMatchSnapshot(`movies-by-cast-grpc-example-result-${i++}`);
39+
expect(item).toMatchSnapshot(`movies-by-cast-grpc-example-result-stream-${i++}`);
40+
}
41+
});
42+
it('should fetch movies by cast as a subscription correctly', async () => {
43+
const MoviesByCastSubscription = await readFile(
44+
join(__dirname, '../example-queries/MoviesByCast.subscription.graphql'),
45+
'utf8',
46+
);
47+
const result = await mesh.execute(MoviesByCastSubscription, undefined);
48+
let i = 0;
49+
for await (const item of result as AsyncIterable<any>) {
50+
expect(item).toMatchSnapshot(`movies-by-cast-grpc-example-result-subscription-${i++}`);
4051
}
4152
});
4253
afterAll(async () => {

‎packages/handlers/grpc/src/index.ts

+54-19
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,22 @@ export default class GrpcHandler implements MeshHandler {
419419
objPath,
420420
creds,
421421
});
422-
field.resolve = this.getFieldResolver({
423-
client,
424-
methodName,
425-
isResponseStream: responseStream,
426-
});
422+
if (rootType.name === 'Subscription') {
423+
field.subscribe = this.getFieldResolver({
424+
client,
425+
methodName,
426+
isResponseStream: responseStream,
427+
});
428+
field.resolve = function identityFn(root) {
429+
return root;
430+
};
431+
} else {
432+
field.resolve = this.getFieldResolver({
433+
client,
434+
methodName,
435+
isResponseStream: responseStream,
436+
});
437+
}
427438
break;
428439
}
429440
case 'grpcConnectivityState': {
@@ -598,26 +609,29 @@ export default class GrpcHandler implements MeshHandler {
598609
for (const methodName in nested.methods) {
599610
const method = nested.methods[methodName];
600611
const rootFieldName = [...pathWithName, methodName].join('_');
612+
const fieldConfigTypeFactory = () => {
613+
const baseResponseTypePath = method.responseType?.split('.');
614+
if (baseResponseTypePath) {
615+
const responseTypePath = this.walkToFindTypePath(
616+
rootJson,
617+
pathWithName,
618+
baseResponseTypePath,
619+
);
620+
return getTypeName(this.schemaComposer, responseTypePath, false);
621+
}
622+
return 'Void';
623+
};
601624
const fieldConfig: ObjectTypeComposerFieldConfigAsObjectDefinition<any, any> = {
602625
type: () => {
603-
const baseResponseTypePath = method.responseType?.split('.');
604-
if (baseResponseTypePath) {
605-
const responseTypePath = this.walkToFindTypePath(
606-
rootJson,
607-
pathWithName,
608-
baseResponseTypePath,
609-
);
610-
let typeName = getTypeName(this.schemaComposer, responseTypePath, false);
611-
if (method.responseStream) {
612-
typeName = `[${typeName}]`;
613-
}
614-
return typeName;
626+
const typeName = fieldConfigTypeFactory();
627+
if (method.responseStream) {
628+
return `[${typeName}]`;
615629
}
616-
return 'Void';
630+
return typeName;
617631
},
618632
description: method.comment,
619633
};
620-
fieldConfig.args = {
634+
const fieldConfigArgs = {
621635
input: () => {
622636
if (method.requestStream) {
623637
return 'File';
@@ -635,6 +649,7 @@ export default class GrpcHandler implements MeshHandler {
635649
return undefined;
636650
},
637651
};
652+
fieldConfig.args = fieldConfigArgs;
638653
const methodNameLowerCased = methodName.toLowerCase();
639654
const prefixQueryMethod = this.config.prefixQueryMethod || QUERY_METHOD_PREFIXES;
640655
const rootTypeComposer = prefixQueryMethod.some(prefix =>
@@ -659,6 +674,26 @@ export default class GrpcHandler implements MeshHandler {
659674
],
660675
},
661676
});
677+
if (method.responseStream) {
678+
this.schemaComposer.Subscription.addFields({
679+
[rootFieldName]: {
680+
args: fieldConfigArgs,
681+
description: method.comment,
682+
type: fieldConfigTypeFactory,
683+
directives: [
684+
{
685+
name: 'grpcMethod',
686+
args: {
687+
rootJsonName,
688+
objPath,
689+
methodName,
690+
responseStream: true,
691+
},
692+
},
693+
],
694+
},
695+
});
696+
}
662697
}
663698
const connectivityStateFieldName = pathWithName.join('_') + '_connectivityState';
664699
this.schemaComposer.addDirective(grpcConnectivityStateDirective);

‎packages/handlers/grpc/test/__snapshots__/handler.spec.ts.snap

+40
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ scalar ObjMap"
187187
exports[`gRPC Handler Interpreting Protos should load the Empty proto 1`] = `
188188
"schema {
189189
query: Query
190+
subscription: Subscription
190191
}
191192
192193
directive @enum(value: String) on ENUM_VALUE
@@ -286,6 +287,13 @@ enum ConnectivityState {
286287
SHUTDOWN
287288
}
288289
290+
type Subscription {
291+
"search movies by the name of the cast"
292+
io_xtech_Example_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.Example", methodName: "SearchMoviesByCast", responseStream: true)
293+
"search movies by the name of the cast"
294+
io_xtech_AnotherExample_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.AnotherExample", methodName: "SearchMoviesByCast", responseStream: true)
295+
}
296+
289297
scalar ObjMap"
290298
`;
291299

@@ -453,6 +461,7 @@ scalar ObjMap"
453461
exports[`gRPC Handler Interpreting Protos should load the Movie proto 1`] = `
454462
"schema {
455463
query: Query
464+
subscription: Subscription
456465
}
457466
458467
directive @enum(value: String) on ENUM_VALUE
@@ -549,6 +558,13 @@ enum ConnectivityState {
549558
SHUTDOWN
550559
}
551560
561+
type Subscription {
562+
"search movies by the name of the cast"
563+
io_xtech_Example_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.Example", methodName: "SearchMoviesByCast", responseStream: true)
564+
"search movies by the name of the cast"
565+
io_xtech_AnotherExample_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.AnotherExample", methodName: "SearchMoviesByCast", responseStream: true)
566+
}
567+
552568
scalar ObjMap"
553569
`;
554570

@@ -661,6 +677,7 @@ scalar ObjMap"
661677
exports[`gRPC Handler Interpreting Protos should load the Outside proto 1`] = `
662678
"schema {
663679
query: Query
680+
subscription: Subscription
664681
}
665682
666683
directive @grpcMethod(rootJsonName: String, objPath: String, methodName: String, responseStream: Boolean) on FIELD_DEFINITION
@@ -767,6 +784,13 @@ input io_xtech_SearchByCastRequest_Input {
767784
castName: String
768785
}
769786
787+
type Subscription {
788+
"search movies by the name of the cast"
789+
io_xtech_Example_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.Example", methodName: "SearchMoviesByCast", responseStream: true)
790+
"search movies by the name of the cast"
791+
io_xtech_AnotherExample_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.AnotherExample", methodName: "SearchMoviesByCast", responseStream: true)
792+
}
793+
770794
scalar ObjMap"
771795
`;
772796

@@ -859,6 +883,7 @@ scalar ObjMap"
859883
exports[`gRPC Handler Interpreting Protos should load the With Underscores proto 1`] = `
860884
"schema {
861885
query: Query
886+
subscription: Subscription
862887
}
863888
864889
directive @enum(value: String) on ENUM_VALUE
@@ -955,13 +980,21 @@ enum ConnectivityState {
955980
SHUTDOWN
956981
}
957982
983+
type Subscription {
984+
"search movies by the name of the cast"
985+
io_xtech_Example_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.Example", methodName: "SearchMoviesByCast", responseStream: true)
986+
"search movies by the name of the cast"
987+
io_xtech_AnotherExample_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.AnotherExample", methodName: "SearchMoviesByCast", responseStream: true)
988+
}
989+
958990
scalar ObjMap"
959991
`;
960992

961993
exports[`gRPC Handler Load proto with prefixQueryMethod should load the retrieve-movie.proto 1`] = `
962994
"schema {
963995
query: Query
964996
mutation: Mutation
997+
subscription: Subscription
965998
}
966999
9671000
directive @enum(value: String) on ENUM_VALUE
@@ -1069,5 +1102,12 @@ input io_xtech_SearchByCastRequest_Input {
10691102
castName: String
10701103
}
10711104
1105+
type Subscription {
1106+
"search movies by the name of the cast"
1107+
io_xtech_Example_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.Example", methodName: "SearchMoviesByCast", responseStream: true)
1108+
"search movies by the name of the cast"
1109+
io_xtech_AnotherExample_SearchMoviesByCast(input: io_xtech_SearchByCastRequest_Input): io_xtech_Movie @grpcMethod(rootJsonName: "Root0", objPath: "io.xtech.AnotherExample", methodName: "SearchMoviesByCast", responseStream: true)
1110+
}
1111+
10721112
scalar ObjMap"
10731113
`;

‎packages/runtime/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@graphql-mesh/string-interpolation": "^0.5.2",
4747
"@graphql-tools/batch-delegate": "^9.0.0",
4848
"@graphql-tools/delegate": "^10.0.0",
49+
"@graphql-tools/executor": "^1.2.0",
4950
"@graphql-tools/wrap": "^10.0.0",
5051
"@whatwg-node/fetch": "^0.9.0",
5152
"graphql-jit": "0.8.2"

0 commit comments

Comments
 (0)
Please sign in to comment.