FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
graphql-java/src/main/java/graphql/GraphQL.java at master · feifeiq/graphql-java · GitHub
feifeiq
/
graphql-java
Public
forked from
graphql-java/graphql-java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
graphql-java
/
src
/
main
/
java
/
graphql
/
GraphQL.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
484 lines (423 loc) · 20.1 KB
Breadcrumbs
graphql-java
/
src
/
main
/
java
/
graphql
/
GraphQL.java
Copy path
File metadata and controls
484 lines (423 loc) · 20.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package
graphql
;
import
graphql
.
execution
.
Execution
;
import
graphql
.
execution
.
ExecutionId
;
import
graphql
.
execution
.
ExecutionIdProvider
;
import
graphql
.
execution
.
ExecutionStrategy
;
import
graphql
.
execution
.
SimpleExecutionStrategy
;
import
graphql
.
execution
.
instrumentation
.
Instrumentation
;
import
graphql
.
execution
.
instrumentation
.
InstrumentationContext
;
import
graphql
.
execution
.
instrumentation
.
NoOpInstrumentation
;
import
graphql
.
execution
.
instrumentation
.
parameters
.
InstrumentationExecutionParameters
;
import
graphql
.
execution
.
instrumentation
.
parameters
.
InstrumentationValidationParameters
;
import
graphql
.
execution
.
preparsed
.
NoOpPreparsedDocumentProvider
;
import
graphql
.
execution
.
preparsed
.
PreparsedDocumentEntry
;
import
graphql
.
execution
.
preparsed
.
PreparsedDocumentProvider
;
import
graphql
.
language
.
Document
;
import
graphql
.
language
.
SourceLocation
;
import
graphql
.
parser
.
Parser
;
import
graphql
.
schema
.
GraphQLSchema
;
import
graphql
.
validation
.
ValidationError
;
import
graphql
.
validation
.
Validator
;
import
org
.
antlr
.
v4
.
runtime
.
RecognitionException
;
import
org
.
antlr
.
v4
.
runtime
.
misc
.
ParseCancellationException
;
import
org
.
slf4j
.
Logger
;
import
org
.
slf4j
.
LoggerFactory
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
concurrent
.
CompletableFuture
;
import
java
.
util
.
function
.
UnaryOperator
;
import
static
graphql
.
Assert
.
assertNotNull
;
@
PublicApi
public
class
GraphQL
{
private
static
final
Logger
log
=
LoggerFactory
.
getLogger
(
GraphQL
.
class
);
private
static
final
ExecutionIdProvider
DEFAULT_EXECUTION_ID_PROVIDER
= (
query
,
operationName
,
context
) ->
ExecutionId
.
generate
();
private
final
GraphQLSchema
graphQLSchema
;
private
final
ExecutionStrategy
queryStrategy
;
private
final
ExecutionStrategy
mutationStrategy
;
private
final
ExecutionStrategy
subscriptionStrategy
;
private
final
ExecutionIdProvider
idProvider
;
private
final
Instrumentation
instrumentation
;
private
final
PreparsedDocumentProvider
preparsedDocumentProvider
;
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
@
Internal
public
GraphQL
(
GraphQLSchema
graphQLSchema
) {
//noinspection deprecation
this
(
graphQLSchema
,
null
,
null
);
}
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
* @param queryStrategy the query execution strategy to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
@
Internal
public
GraphQL
(
GraphQLSchema
graphQLSchema
,
ExecutionStrategy
queryStrategy
) {
//noinspection deprecation
this
(
graphQLSchema
,
queryStrategy
,
null
);
}
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
* @param queryStrategy the query execution strategy to use
* @param mutationStrategy the mutation execution strategy to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
@
Internal
public
GraphQL
(
GraphQLSchema
graphQLSchema
,
ExecutionStrategy
queryStrategy
,
ExecutionStrategy
mutationStrategy
) {
this
(
graphQLSchema
,
queryStrategy
,
mutationStrategy
,
null
,
DEFAULT_EXECUTION_ID_PROVIDER
,
NoOpInstrumentation
.
INSTANCE
,
NoOpPreparsedDocumentProvider
.
INSTANCE
);
}
/**
* A GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
* @param queryStrategy the query execution strategy to use
* @param mutationStrategy the mutation execution strategy to use
* @param subscriptionStrategy the subscription execution strategy to use
*
* @deprecated use the {@link #newGraphQL(GraphQLSchema)} builder instead. This will be removed in a future version.
*/
@
Internal
public
GraphQL
(
GraphQLSchema
graphQLSchema
,
ExecutionStrategy
queryStrategy
,
ExecutionStrategy
mutationStrategy
,
ExecutionStrategy
subscriptionStrategy
) {
this
(
graphQLSchema
,
queryStrategy
,
mutationStrategy
,
subscriptionStrategy
,
DEFAULT_EXECUTION_ID_PROVIDER
,
NoOpInstrumentation
.
INSTANCE
,
NoOpPreparsedDocumentProvider
.
INSTANCE
);
}
private
GraphQL
(
GraphQLSchema
graphQLSchema
,
ExecutionStrategy
queryStrategy
,
ExecutionStrategy
mutationStrategy
,
ExecutionStrategy
subscriptionStrategy
,
ExecutionIdProvider
idProvider
,
Instrumentation
instrumentation
,
PreparsedDocumentProvider
preparsedDocumentProvider
) {
this
.
graphQLSchema
=
assertNotNull
(
graphQLSchema
,
"queryStrategy must be non null"
);
this
.
queryStrategy
=
queryStrategy
!=
null
?
queryStrategy
:
new
SimpleExecutionStrategy
();
this
.
mutationStrategy
=
mutationStrategy
!=
null
?
mutationStrategy
:
new
SimpleExecutionStrategy
();
this
.
subscriptionStrategy
=
subscriptionStrategy
!=
null
?
subscriptionStrategy
:
new
SimpleExecutionStrategy
();
this
.
idProvider
=
assertNotNull
(
idProvider
,
"idProvider must be non null"
);
this
.
instrumentation
=
instrumentation
;
this
.
preparsedDocumentProvider
=
assertNotNull
(
preparsedDocumentProvider
,
"preparsedDocumentProvider must be non null"
);
}
/**
* Helps you build a GraphQL object ready to execute queries
*
* @param graphQLSchema the schema to use
*
* @return a builder of GraphQL objects
*/
public
static
Builder
newGraphQL
(
GraphQLSchema
graphQLSchema
) {
return
new
Builder
(
graphQLSchema
);
}
@
PublicApi
public
static
class
Builder
{
private
GraphQLSchema
graphQLSchema
;
private
ExecutionStrategy
queryExecutionStrategy
=
new
SimpleExecutionStrategy
();
private
ExecutionStrategy
mutationExecutionStrategy
=
new
SimpleExecutionStrategy
();
private
ExecutionStrategy
subscriptionExecutionStrategy
=
new
SimpleExecutionStrategy
();
private
ExecutionIdProvider
idProvider
=
DEFAULT_EXECUTION_ID_PROVIDER
;
private
Instrumentation
instrumentation
=
NoOpInstrumentation
.
INSTANCE
;
private
PreparsedDocumentProvider
preparsedDocumentProvider
=
NoOpPreparsedDocumentProvider
.
INSTANCE
;
public
Builder
(
GraphQLSchema
graphQLSchema
) {
this
.
graphQLSchema
=
graphQLSchema
;
}
public
Builder
schema
(
GraphQLSchema
graphQLSchema
) {
this
.
graphQLSchema
=
assertNotNull
(
graphQLSchema
,
"GraphQLSchema must be non null"
);
return
this
;
}
public
Builder
queryExecutionStrategy
(
ExecutionStrategy
executionStrategy
) {
this
.
queryExecutionStrategy
=
assertNotNull
(
executionStrategy
,
"Query ExecutionStrategy must be non null"
);
return
this
;
}
public
Builder
mutationExecutionStrategy
(
ExecutionStrategy
executionStrategy
) {
this
.
mutationExecutionStrategy
=
assertNotNull
(
executionStrategy
,
"Mutation ExecutionStrategy must be non null"
);
return
this
;
}
public
Builder
subscriptionExecutionStrategy
(
ExecutionStrategy
executionStrategy
) {
this
.
subscriptionExecutionStrategy
=
assertNotNull
(
executionStrategy
,
"Subscription ExecutionStrategy must be non null"
);
return
this
;
}
public
Builder
instrumentation
(
Instrumentation
instrumentation
) {
this
.
instrumentation
=
assertNotNull
(
instrumentation
,
"Instrumentation must be non null"
);
return
this
;
}
public
Builder
preparsedDocumentProvider
(
PreparsedDocumentProvider
preparsedDocumentProvider
) {
this
.
preparsedDocumentProvider
=
assertNotNull
(
preparsedDocumentProvider
,
"PreparsedDocumentProvider must be non null"
);
return
this
;
}
public
Builder
executionIdProvider
(
ExecutionIdProvider
executionIdProvider
) {
this
.
idProvider
=
assertNotNull
(
executionIdProvider
,
"ExecutionIdProvider must be non null"
);
return
this
;
}
public
GraphQL
build
() {
assertNotNull
(
graphQLSchema
,
"queryStrategy must be non null"
);
assertNotNull
(
queryExecutionStrategy
,
"queryStrategy must be non null"
);
assertNotNull
(
idProvider
,
"idProvider must be non null"
);
return
new
GraphQL
(
graphQLSchema
,
queryExecutionStrategy
,
mutationExecutionStrategy
,
subscriptionExecutionStrategy
,
idProvider
,
instrumentation
,
preparsedDocumentProvider
);
}
}
/**
* Executes the specified graphql query/mutation/subscription
*
* @param query the query/mutation/subscription
*
* @return an {@link ExecutionResult} which can include errors
*/
public
ExecutionResult
execute
(
String
query
) {
ExecutionInput
executionInput
=
ExecutionInput
.
newExecutionInput
()
.
query
(
query
)
.
build
();
return
execute
(
executionInput
);
}
/**
* Info: This sets context = root to be backwards compatible.
*
* @param query the query/mutation/subscription
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
*
* @return an {@link ExecutionResult} which can include errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@
Deprecated
public
ExecutionResult
execute
(
String
query
,
Object
context
) {
ExecutionInput
executionInput
=
ExecutionInput
.
newExecutionInput
()
.
query
(
query
)
.
context
(
context
)
.
root
(
context
)
// This we are doing do be backwards compatible
.
build
();
return
execute
(
executionInput
);
}
/**
* Info: This sets context = root to be backwards compatible.
*
* @param query the query/mutation/subscription
* @param operationName the name of the operation to execute
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
*
* @return an {@link ExecutionResult} which can include errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@
Deprecated
public
ExecutionResult
execute
(
String
query
,
String
operationName
,
Object
context
) {
ExecutionInput
executionInput
=
ExecutionInput
.
newExecutionInput
()
.
query
(
query
)
.
operationName
(
operationName
)
.
context
(
context
)
.
root
(
context
)
// This we are doing do be backwards compatible
.
build
();
return
execute
(
executionInput
);
}
/**
* Info: This sets context = root to be backwards compatible.
*
* @param query the query/mutation/subscription
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
* @param variables variable values uses as argument
*
* @return an {@link ExecutionResult} which can include errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@
Deprecated
public
ExecutionResult
execute
(
String
query
,
Object
context
,
Map
<
String
,
Object
>
variables
) {
ExecutionInput
executionInput
=
ExecutionInput
.
newExecutionInput
()
.
query
(
query
)
.
context
(
context
)
.
root
(
context
)
// This we are doing do be backwards compatible
.
variables
(
variables
)
.
build
();
return
execute
(
executionInput
);
}
/**
* Info: This sets context = root to be backwards compatible.
*
* @param query the query/mutation/subscription
* @param operationName name of the operation to execute
* @param context custom object provided to each {@link graphql.schema.DataFetcher}
* @param variables variable values uses as argument
*
* @return an {@link ExecutionResult} which can include errors
*
* @deprecated Use {@link #execute(ExecutionInput)}
*/
@
Deprecated
public
ExecutionResult
execute
(
String
query
,
String
operationName
,
Object
context
,
Map
<
String
,
Object
>
variables
) {
ExecutionInput
executionInput
=
ExecutionInput
.
newExecutionInput
()
.
query
(
query
)
.
operationName
(
operationName
)
.
context
(
context
)
.
root
(
context
)
// This we are doing do be backwards compatible
.
variables
(
variables
)
.
build
();
return
execute
(
executionInput
);
}
/**
* Executes the graphql query using the provided input object builder
*
* @param executionInputBuilder {@link ExecutionInput.Builder}
*
* @return an {@link ExecutionResult} which can include errors
*/
public
ExecutionResult
execute
(
ExecutionInput
.
Builder
executionInputBuilder
) {
return
execute
(
executionInputBuilder
.
build
());
}
/**
* Executes the graphql query using calling the builder function and giving it a new builder.
* <p>
* This allows a lambda style like :
*
* <pre>
* {@code
* ExecutionResult result = graphql.execute(input -> input.query("{hello}").root(startingObj).context(contextObj));
* }
* </pre>
*
* @param builderFunction a function that is given a {@link ExecutionInput.Builder}
*
* @return an {@link ExecutionResult} which can include errors
*/
public
ExecutionResult
execute
(
UnaryOperator
<
ExecutionInput
.
Builder
>
builderFunction
) {
return
execute
(
builderFunction
.
apply
(
ExecutionInput
.
newExecutionInput
()).
build
());
}
/**
* Executes the graphql query using the provided input object
*
* @param executionInput {@link ExecutionInput}
*
* @return an {@link ExecutionResult} which can include errors
*/
public
ExecutionResult
execute
(
ExecutionInput
executionInput
) {
return
executeAsync
(
executionInput
).
join
();
}
/**
* Executes the graphql query using the provided input object builder
*
* This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult}
* which is the result of executing the provided query.
*
* @param executionInputBuilder {@link ExecutionInput.Builder}
*
* @return a promise to an {@link ExecutionResult} which can include errors
*/
public
CompletableFuture
<
ExecutionResult
>
executeAsync
(
ExecutionInput
.
Builder
executionInputBuilder
) {
return
executeAsync
(
executionInputBuilder
.
build
());
}
/**
* Executes the graphql query using the provided input object builder
*
* This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult}
* which is the result of executing the provided query.
* <p>
* This allows a lambda style like :
*
* <pre>
* {@code
* ExecutionResult result = graphql.execute(input -> input.query("{hello}").root(startingObj).context(contextObj));
* }
* </pre>
*
* @param builderFunction a function that is given a {@link ExecutionInput.Builder}
*
* @return a promise to an {@link ExecutionResult} which can include errors
*/
public
CompletableFuture
<
ExecutionResult
>
executeAsync
(
UnaryOperator
<
ExecutionInput
.
Builder
>
builderFunction
) {
return
executeAsync
(
builderFunction
.
apply
(
ExecutionInput
.
newExecutionInput
()).
build
());
}
/**
* Executes the graphql query using the provided input object
*
* This will return a promise (aka {@link CompletableFuture}) to provide a {@link ExecutionResult}
* which is the result of executing the provided query.
*
* @param executionInput {@link ExecutionInput}
*
* @return a promise to an {@link ExecutionResult} which can include errors
*/
public
CompletableFuture
<
ExecutionResult
>
executeAsync
(
ExecutionInput
executionInput
) {
log
.
debug
(
"Executing request. operation name: {}. query: {}. variables {} "
,
executionInput
.
getOperationName
(),
executionInput
.
getQuery
(),
executionInput
.
getVariables
());
InstrumentationContext
<
ExecutionResult
>
executionInstrumentation
=
instrumentation
.
beginExecution
(
new
InstrumentationExecutionParameters
(
executionInput
));
final
CompletableFuture
<
ExecutionResult
>
executionResult
=
parseValidateAndExecute
(
executionInput
);
executionResult
.
thenAccept
(
executionInstrumentation
::
onEnd
);
return
executionResult
;
}
private
CompletableFuture
<
ExecutionResult
>
parseValidateAndExecute
(
ExecutionInput
executionInput
) {
PreparsedDocumentEntry
preparsedDoc
=
preparsedDocumentProvider
.
get
(
executionInput
.
getQuery
(),
query
->
parseAndValidate
(
executionInput
));
if
(
preparsedDoc
.
hasErrors
()) {
return
CompletableFuture
.
completedFuture
(
new
ExecutionResultImpl
(
preparsedDoc
.
getErrors
()));
}
return
execute
(
executionInput
,
preparsedDoc
.
getDocument
());
}
private
PreparsedDocumentEntry
parseAndValidate
(
ExecutionInput
executionInput
) {
ParseResult
parseResult
=
parse
(
executionInput
);
if
(
parseResult
.
isFailure
()) {
return
new
PreparsedDocumentEntry
(
toInvalidSyntaxError
(
parseResult
.
getException
()));
}
else
{
final
Document
document
=
parseResult
.
getDocument
();
final
List
<
ValidationError
>
errors
=
validate
(
executionInput
,
document
);
if
(!
errors
.
isEmpty
()) {
return
new
PreparsedDocumentEntry
(
errors
);
}
return
new
PreparsedDocumentEntry
(
document
);
}
}
private
ParseResult
parse
(
ExecutionInput
executionInput
) {
InstrumentationContext
<
Document
>
parseInstrumentation
=
instrumentation
.
beginParse
(
new
InstrumentationExecutionParameters
(
executionInput
));
Parser
parser
=
new
Parser
();
Document
document
;
try
{
document
=
parser
.
parseDocument
(
executionInput
.
getQuery
());
}
catch
(
ParseCancellationException
e
) {
parseInstrumentation
.
onEnd
(
e
);
return
ParseResult
.
ofError
((
RecognitionException
)
e
.
getCause
());
}
parseInstrumentation
.
onEnd
(
document
);
return
ParseResult
.
of
(
document
);
}
private
List
<
ValidationError
>
validate
(
ExecutionInput
executionInput
,
Document
document
) {
InstrumentationContext
<
List
<
ValidationError
>>
validationCtx
=
instrumentation
.
beginValidation
(
new
InstrumentationValidationParameters
(
executionInput
,
document
));
Validator
validator
=
new
Validator
();
List
<
ValidationError
>
validationErrors
=
validator
.
validateDocument
(
graphQLSchema
,
document
);
validationCtx
.
onEnd
(
validationErrors
);
return
validationErrors
;
}
private
CompletableFuture
<
ExecutionResult
>
execute
(
ExecutionInput
executionInput
,
Document
document
) {
String
query
=
executionInput
.
getQuery
();
String
operationName
=
executionInput
.
getOperationName
();
Object
context
=
executionInput
.
getContext
();
Execution
execution
=
new
Execution
(
queryStrategy
,
mutationStrategy
,
subscriptionStrategy
,
instrumentation
);
ExecutionId
executionId
=
idProvider
.
provide
(
query
,
operationName
,
context
);
return
execution
.
execute
(
document
,
graphQLSchema
,
executionId
,
executionInput
);
}
private
InvalidSyntaxError
toInvalidSyntaxError
(
RecognitionException
recognitionException
) {
SourceLocation
sourceLocation
=
null
;
if
(
recognitionException
!=
null
) {
sourceLocation
=
new
SourceLocation
(
recognitionException
.
getOffendingToken
().
getLine
(),
recognitionException
.
getOffendingToken
().
getCharPositionInLine
());
}
return
new
InvalidSyntaxError
(
sourceLocation
);
}
private
static
class
ParseResult
{
private
final
Document
document
;
private
final
RecognitionException
exception
;
private
ParseResult
(
Document
document
,
RecognitionException
exception
) {
this
.
document
=
document
;
this
.
exception
=
exception
;
}
private
boolean
isFailure
() {
return
document
==
null
;
}
private
Document
getDocument
() {
return
document
;
}
private
RecognitionException
getException
() {
return
exception
;
}
private
static
ParseResult
of
(
Document
document
) {
return
new
ParseResult
(
document
,
null
);
}
private
static
ParseResult
ofError
(
RecognitionException
e
) {
return
new
ParseResult
(
null
,
e
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL