-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstitching-operation.ts
41 lines (35 loc) · 1.29 KB
/
stitching-operation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { graphql, GraphQLSchema, GraphQLResolveInfo } from "graphql";
import { delegateToSchema, Transform } from "graphql-tools";
export class Connector {
//Constructor get remote schema
constructor(private schema: GraphQLSchema) {
}
//Resource query is a remote endpoint operation
ResourceCreate(args: any, context: any, info: GraphQLResolveInfo): Promise<any> {
//use of delegation on remote
let fieldName = "ResourceCreate";
return delegateToSchema({
schema: this.schema,
operation: 'mutation',
fieldName,
args: { args },
context: {},
info,
});
}
//Local query
addResources(args: any, context: any, info: GraphQLResolveInfo, transforms?: Array<Transform>): Promise<any> {
//That need to rewruite a different query adapted to next microservice
const mutation = `mutation ResourceCreate($proto: Resource) {
ResourceCreate(resource: $proto) {
id
}
}`;
//Promise for resources resolution
return new Promise((resolve) => {
graphql(this.schema, mutation, null, context, args).then((resource: any) => {
resolve(resource.data.ResourceCreate);
});
});
}
}