-
Notifications
You must be signed in to change notification settings - Fork 468
/
engine.ts
187 lines (173 loc) · 4.46 KB
/
engine.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// EngineSchemaProvider (engine schema reg => schema)
import { NotificationHandler } from "vscode-languageserver";
import gql from "graphql-tag";
import { GraphQLSchema, buildClientSchema } from "graphql";
import { ApolloEngineClient, ClientIdentity } from "../../engine";
import { ClientConfig, parseServiceSpecifier } from "../../config";
import {
GraphQLSchemaProvider,
SchemaChangeUnsubscribeHandler,
SchemaResolveConfig
} from "./base";
import { GetSchemaByTag } from "../../graphqlTypes";
export class EngineSchemaProvider implements GraphQLSchemaProvider {
private schema?: GraphQLSchema;
private client?: ApolloEngineClient;
constructor(
private config: ClientConfig,
private clientIdentity?: ClientIdentity
) {}
async resolveSchema(override: SchemaResolveConfig) {
if (this.schema && (!override || !override.force)) return this.schema;
const { engine, client } = this.config;
if (typeof client.service !== "string") {
throw new Error(
`Service name not found for client, found ${client.service}`
);
}
// create engine client
if (!this.client) {
if (!engine.apiKey) {
throw new Error("ENGINE_API_KEY not found");
}
this.client = new ApolloEngineClient(
engine.apiKey,
engine.endpoint,
this.clientIdentity
);
}
const [id, tag = "current"] = parseServiceSpecifier(client.service);
const { data, errors } = await this.client.execute<GetSchemaByTag>({
query: SCHEMA_QUERY,
variables: {
id,
tag: override && override.tag ? override.tag : tag
}
});
if (errors) {
// XXX better error handling of GraphQL errors
throw new Error(errors.map(({ message }: Error) => message).join("\n"));
}
if (!(data && data.service && data.service.__typename === "Service")) {
throw new Error(
`Unable to get schema from Apollo Engine for service ${id}`
);
}
// @ts-ignore
// XXX Types of `data.service.schema` won't match closely enough with `IntrospectionQuery`
this.schema = buildClientSchema(data.service.schema);
return this.schema;
}
onSchemaChange(
_handler: NotificationHandler<GraphQLSchema>
): SchemaChangeUnsubscribeHandler {
throw new Error("Polling of Engine not implemented yet");
return () => {};
}
}
export const SCHEMA_QUERY = gql`
query GetSchemaByTag($tag: String!) {
service: me {
... on Service {
__typename
schema(tag: $tag) {
hash
__schema: introspection {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types(filter: { includeBuiltInTypes: true }) {
...IntrospectionFullType
}
directives {
name
description
locations
args {
...IntrospectionInputValue
}
}
}
}
}
}
}
fragment IntrospectionFullType on IntrospectionType {
kind
name
description
fields {
name
description
args {
...IntrospectionInputValue
}
type {
...IntrospectionTypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...IntrospectionInputValue
}
interfaces {
...IntrospectionTypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
depreactionReason
}
possibleTypes {
...IntrospectionTypeRef
}
}
fragment IntrospectionInputValue on IntrospectionInputValue {
name
description
type {
...IntrospectionTypeRef
}
defaultValue
}
fragment IntrospectionTypeRef on IntrospectionType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
`;