Skip to content

Commit 7140e6c

Browse files
committed
Avoid AssertException for persisted query
Closes gh-930
1 parent 0fa2c4f commit 7140e6c

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

spring-graphql-docs/modules/ROOT/pages/request-execution.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ In Spring GraphQL you can register a `PreparsedDocumentProvider` through
308308
GraphQlSource.Builder builder = ...
309309
310310
// Create provider
311-
PreparsedDocumentProvider provider = ...
311+
PreparsedDocumentProvider provider =
312+
new ApolloPersistedQuerySupport(new InMemoryPersistedQueryCache(Collections.emptyMap()));
312313
313314
builder.schemaResources(..)
314315
.configureRuntimeWiring(..)

spring-graphql/src/main/java/org/springframework/graphql/server/support/SerializableGraphQlRequest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.Map;
1919

20+
import graphql.execution.preparsed.persisted.PersistedQuerySupport;
21+
2022
import org.springframework.graphql.GraphQlRequest;
2123
import org.springframework.lang.Nullable;
2224
import org.springframework.web.server.ServerWebInputException;
@@ -85,6 +87,9 @@ public Map<String, Object> getExtensions() {
8587
@Override
8688
public String getDocument() {
8789
if (this.query == null) {
90+
if (this.extensions != null && this.extensions.get("persistedQuery") != null) {
91+
return PersistedQuerySupport.PERSISTED_QUERY_MARKER;
92+
}
8893
throw new ServerWebInputException("No 'query'");
8994
}
9095
return this.query;

spring-graphql/src/test/java/org/springframework/graphql/server/webmvc/GraphQlHttpHandlerTests.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import java.util.Locale;
2323
import java.util.UUID;
2424

25-
import jakarta.servlet.ServletException;
26-
2725
import com.jayway.jsonpath.DocumentContext;
2826
import com.jayway.jsonpath.JsonPath;
27+
import graphql.execution.preparsed.persisted.ApolloPersistedQuerySupport;
28+
import graphql.execution.preparsed.persisted.InMemoryPersistedQueryCache;
29+
import jakarta.servlet.ServletException;
2930
import org.junit.jupiter.api.Test;
3031

3132
import org.springframework.context.i18n.LocaleContextHolder;
@@ -109,6 +110,45 @@ void shouldSetExecutionId() throws Exception {
109110
assertThatNoException().isThrownBy(() -> UUID.fromString(id));
110111
}
111112

113+
@Test
114+
void persistedQuery() throws Exception {
115+
116+
ApolloPersistedQuerySupport documentProvider =
117+
new ApolloPersistedQuerySupport(new InMemoryPersistedQueryCache(Collections.emptyMap()));
118+
119+
GraphQlHttpHandler handler = GraphQlSetup.schemaContent("type Query { greeting: String }")
120+
.configureGraphQl(builder -> builder.preparsedDocumentProvider(documentProvider))
121+
.toHttpHandler();
122+
123+
String document = """
124+
{
125+
"query" : "{__typename}",
126+
"extensions": {
127+
"persistedQuery": {
128+
"version":1,
129+
"sha256Hash":"ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38"
130+
}
131+
}
132+
}""";
133+
134+
MockHttpServletResponse servletResponse = handleRequest(createServletRequest(document, "*/*"), handler);
135+
assertThat(servletResponse.getContentAsString()).isEqualTo("{\"data\":{\"__typename\":\"Query\"}}");
136+
137+
document = """
138+
{
139+
"extensions":{
140+
"persistedQuery":{
141+
"version":1,
142+
"sha256Hash":"ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38"
143+
}
144+
}
145+
}""";
146+
147+
servletResponse = handleRequest(createServletRequest(document, "*/*"), handler);
148+
assertThat(servletResponse.getContentAsString()).isEqualTo("{\"data\":{\"__typename\":\"Query\"}}");
149+
}
150+
151+
112152
private MockHttpServletRequest createServletRequest(String query, String accept) {
113153
MockHttpServletRequest servletRequest = new MockHttpServletRequest("POST", "/");
114154
servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);

spring-graphql/src/testFixtures/java/org/springframework/graphql/GraphQlSetup.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.function.Consumer;
2223

2324
import graphql.GraphQL;
2425
import graphql.execution.instrumentation.Instrumentation;
@@ -137,6 +138,11 @@ public GraphQlSetup typeVisitorToTransformSchema(GraphQLTypeVisitor... visitors)
137138
return this;
138139
}
139140

141+
public GraphQlSetup configureGraphQl(Consumer<GraphQL.Builder> configurer) {
142+
this.graphQlSourceBuilder.configureGraphQl(configurer);
143+
return this;
144+
}
145+
140146
public GraphQL toGraphQl() {
141147
return this.graphQlSourceBuilder.build().graphQl();
142148
}

0 commit comments

Comments
 (0)