Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

fix: 'Invalid Syntax : offending token '<EOF>'' when using Apollo persisted queries #666

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Object graphqlPOST(
@Nullable @RequestParam(value = "query", required = false) String query,
@Nullable @RequestParam(value = "operationName", required = false) String operationName,
@Nullable @RequestParam(value = "variables", required = false) String variablesJson,
@Nullable @RequestParam(value = "extensions", required = false) String extensionsJson,
@Nullable @RequestBody(required = false) String body,
ServerWebExchange serverWebExchange) {

Expand All @@ -58,6 +59,7 @@ public Object graphqlPOST(
request.getQuery(),
request.getOperationName(),
request.getVariables(),
request.getExtensions(),
serverWebExchange);
}

Expand All @@ -68,15 +70,20 @@ public Object graphqlPOST(

if (query != null) {
return executeRequest(
query, operationName, convertVariablesJson(variablesJson), serverWebExchange);
query,
operationName,
convertVariablesJson(variablesJson),
convertExtensionsJson(extensionsJson),
serverWebExchange);
}

// * If the "application/graphql" Content-Type header is present,
// treat the HTTP POST body contents as the GraphQL query string.

if ("application/graphql".equals(contentType.toString())
|| "application/graphql; charset=utf-8".equals(contentType.toString())) {
return executeRequest(body, null, Collections.emptyMap(), serverWebExchange);
return executeRequest(
body, null, Collections.emptyMap(), Collections.emptyMap(), serverWebExchange);
}

throw new ResponseStatusException(
Expand All @@ -88,10 +95,14 @@ public Object graphqlGET(
@Nullable @RequestParam("query") String query,
@Nullable @RequestParam(value = "operationName", required = false) String operationName,
@Nullable @RequestParam(value = "variables", required = false) String variablesJson,
@Nullable @RequestParam(value = "extensions", required = false) String extensionsJson,
ServerWebExchange serverWebExchange) {

return executeRequest(
query, operationName, convertVariablesJson(variablesJson), serverWebExchange);
query == null ? "" : query,
operationName,
convertVariablesJson(variablesJson),
convertExtensionsJson(extensionsJson),
serverWebExchange);
}

private Map<String, Object> convertVariablesJson(String jsonMap) {
Expand All @@ -100,10 +111,17 @@ private Map<String, Object> convertVariablesJson(String jsonMap) {
.orElseGet(Collections::emptyMap);
}

private Map<String, Object> convertExtensionsJson(String jsonMap) {
return Optional.ofNullable(jsonMap)
.map(objectMapper::deserializeExtensions)
.orElseGet(Collections::emptyMap);
}

protected abstract Object executeRequest(
String query,
String operationName,
Map<String, Object> variables,
Map<String, Object> extensions,
ServerWebExchange serverWebExchange);

protected Object handleBodyParsingException(Exception exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ protected Object executeRequest(
String query,
String operationName,
Map<String, Object> variables,
Map<String, Object> extensions,
ServerWebExchange serverWebExchange) {
GraphQLSingleInvocationInput invocationInput =
invocationInputFactory.create(
new GraphQLRequest(query, variables, operationName), serverWebExchange);
new GraphQLRequest(query, variables, extensions, operationName), serverWebExchange);
Mono<ExecutionResult> executionResult =
Mono.fromCompletionStage(graphQLInvoker.executeAsync(invocationInput));
return executionResult.map(objectMapper::createResultFromExecutionResult);
Expand Down