1
- import { SchemaLoader , Source , SingleFileOptions } from '@graphql-tools/utils' ;
1
+ import { SchemaLoader , Source , SingleFileOptions , parseGraphQLSDL } from '@graphql-tools/utils' ;
2
2
import { fetch } from 'cross-fetch' ;
3
- import { buildClientSchema } from 'graphql' ;
3
+ import AggregateError from '@ardatan/aggregate-error' ;
4
+ import syncFetch from 'sync-fetch' ;
4
5
5
6
/**
6
7
* Additional options for loading from Apollo Engine
@@ -25,49 +26,63 @@ export class ApolloEngineLoader implements SchemaLoader<ApolloEngineOptions> {
25
26
return 'apollo-engine' ;
26
27
}
27
28
29
+ private getFetchArgs ( options : ApolloEngineOptions ) : [ string , RequestInit ] {
30
+ return [
31
+ options . engine . endpoint || DEFAULT_APOLLO_ENDPOINT ,
32
+ {
33
+ method : 'POST' ,
34
+ headers : {
35
+ 'x-api-key' : options . engine . apiKey ,
36
+ 'apollo-client-name' : 'Apollo Language Server' ,
37
+ 'apollo-client-reference-id' : '146d29c0-912c-46d3-b686-920e52586be6' ,
38
+ 'apollo-client-version' : '2.6.8' ,
39
+ 'Content-Type' : 'application/json' ,
40
+ Accept : 'application/json' ,
41
+ ...options . headers ,
42
+ } ,
43
+ body : JSON . stringify ( {
44
+ query : SCHEMA_QUERY ,
45
+ variables : {
46
+ id : options . graph ,
47
+ tag : options . variant ,
48
+ } ,
49
+ } ) ,
50
+ } ,
51
+ ] ;
52
+ }
53
+
28
54
async canLoad ( ptr : string ) {
29
- return typeof ptr === 'string' && ptr === 'apollo-engine' ;
55
+ return this . canLoadSync ( ptr ) ;
30
56
}
31
57
32
- canLoadSync ( ) {
33
- return false ;
58
+ canLoadSync ( ptr : string ) {
59
+ return typeof ptr === 'string' && ptr === 'apollo-engine' ;
34
60
}
35
61
36
- async load ( _ : 'apollo-engine' , options : ApolloEngineOptions ) : Promise < Source > {
37
- const response = await fetch ( options . engine . endpoint || DEFAULT_APOLLO_ENDPOINT , {
38
- method : 'POST' ,
39
- headers : {
40
- 'x-api-key' : options . engine . apiKey ,
41
- 'apollo-client-name' : 'Apollo Language Server' ,
42
- 'apollo-client-reference-id' : '146d29c0-912c-46d3-b686-920e52586be6' ,
43
- 'apollo-client-version' : '2.6.8' ,
44
- 'Content-Type' : 'application/json' ,
45
- Accept : 'application/json' ,
46
- ...options . headers ,
47
- } ,
48
- body : JSON . stringify ( {
49
- query : SCHEMA_QUERY ,
50
- variables : {
51
- id : options . graph ,
52
- tag : options . variant ,
53
- } ,
54
- } ) ,
55
- } ) ;
62
+ async load ( pointer : 'apollo-engine' , options : ApolloEngineOptions ) : Promise < Source > {
63
+ const fetchArgs = this . getFetchArgs ( options ) ;
64
+ const response = await fetch ( ...fetchArgs ) ;
56
65
57
66
const { data, errors } = await response . json ( ) ;
58
67
59
68
if ( errors ) {
60
- throw new Error ( errors . map ( ( { message } : Error ) => message ) . join ( '\n' ) ) ;
69
+ throw new AggregateError ( errors ) ;
61
70
}
62
71
63
- return {
64
- location : 'apollo-engine' ,
65
- schema : buildClientSchema ( data . service . schema ) ,
66
- } ;
72
+ return parseGraphQLSDL ( pointer , data . service . schema . document , options ) ;
67
73
}
68
74
69
- loadSync ( ) : never {
70
- throw new Error ( 'Loader ApolloEngine has no sync mode' ) ;
75
+ loadSync ( pointer : 'apollo-engine' , options : ApolloEngineOptions ) : Source {
76
+ const fetchArgs = this . getFetchArgs ( options ) ;
77
+ const response = syncFetch ( ...fetchArgs ) ;
78
+
79
+ const { data, errors } = response . json ( ) ;
80
+
81
+ if ( errors ) {
82
+ throw new AggregateError ( errors ) ;
83
+ }
84
+
85
+ return parseGraphQLSDL ( pointer , data . service . schema . document , options ) ;
71
86
}
72
87
}
73
88
@@ -80,104 +95,7 @@ export const SCHEMA_QUERY = /* GraphQL */ `
80
95
... on Service {
81
96
__typename
82
97
schema(tag: $tag) {
83
- hash
84
- __schema: introspection {
85
- queryType {
86
- name
87
- }
88
- mutationType {
89
- name
90
- }
91
- subscriptionType {
92
- name
93
- }
94
- types(filter: { includeBuiltInTypes: true }) {
95
- ...IntrospectionFullType
96
- }
97
- directives {
98
- name
99
- description
100
- locations
101
- args {
102
- ...IntrospectionInputValue
103
- }
104
- }
105
- }
106
- }
107
- }
108
- }
109
- }
110
-
111
- fragment IntrospectionFullType on IntrospectionType {
112
- kind
113
- name
114
- description
115
- fields {
116
- name
117
- description
118
- args {
119
- ...IntrospectionInputValue
120
- }
121
- type {
122
- ...IntrospectionTypeRef
123
- }
124
- isDeprecated
125
- deprecationReason
126
- }
127
- inputFields {
128
- ...IntrospectionInputValue
129
- }
130
- interfaces {
131
- ...IntrospectionTypeRef
132
- }
133
- enumValues(includeDeprecated: true) {
134
- name
135
- description
136
- isDeprecated
137
- deprecationReason
138
- }
139
- possibleTypes {
140
- ...IntrospectionTypeRef
141
- }
142
- }
143
-
144
- fragment IntrospectionInputValue on IntrospectionInputValue {
145
- name
146
- description
147
- type {
148
- ...IntrospectionTypeRef
149
- }
150
- defaultValue
151
- }
152
-
153
- fragment IntrospectionTypeRef on IntrospectionType {
154
- kind
155
- name
156
- ofType {
157
- kind
158
- name
159
- ofType {
160
- kind
161
- name
162
- ofType {
163
- kind
164
- name
165
- ofType {
166
- kind
167
- name
168
- ofType {
169
- kind
170
- name
171
- ofType {
172
- kind
173
- name
174
- ofType {
175
- kind
176
- name
177
- }
178
- }
179
- }
180
- }
98
+ document
181
99
}
182
100
}
183
101
}
0 commit comments