Skip to content

Commit

Permalink
Avoid AssertException for persisted query
Browse files Browse the repository at this point in the history
Closes gh-930
  • Loading branch information
rstoyanchev committed Mar 22, 2024
1 parent 0fa2c4f commit 7140e6c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ In Spring GraphQL you can register a `PreparsedDocumentProvider` through
GraphQlSource.Builder builder = ...
// Create provider
PreparsedDocumentProvider provider = ...
PreparsedDocumentProvider provider =
new ApolloPersistedQuerySupport(new InMemoryPersistedQueryCache(Collections.emptyMap()));
builder.schemaResources(..)
.configureRuntimeWiring(..)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Map;

import graphql.execution.preparsed.persisted.PersistedQuerySupport;

import org.springframework.graphql.GraphQlRequest;
import org.springframework.lang.Nullable;
import org.springframework.web.server.ServerWebInputException;
Expand Down Expand Up @@ -85,6 +87,9 @@ public Map<String, Object> getExtensions() {
@Override
public String getDocument() {
if (this.query == null) {
if (this.extensions != null && this.extensions.get("persistedQuery") != null) {
return PersistedQuerySupport.PERSISTED_QUERY_MARKER;
}
throw new ServerWebInputException("No 'query'");
}
return this.query;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
import java.util.Locale;
import java.util.UUID;

import jakarta.servlet.ServletException;

import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import graphql.execution.preparsed.persisted.ApolloPersistedQuerySupport;
import graphql.execution.preparsed.persisted.InMemoryPersistedQueryCache;
import jakarta.servlet.ServletException;
import org.junit.jupiter.api.Test;

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

@Test
void persistedQuery() throws Exception {

ApolloPersistedQuerySupport documentProvider =
new ApolloPersistedQuerySupport(new InMemoryPersistedQueryCache(Collections.emptyMap()));

GraphQlHttpHandler handler = GraphQlSetup.schemaContent("type Query { greeting: String }")
.configureGraphQl(builder -> builder.preparsedDocumentProvider(documentProvider))
.toHttpHandler();

String document = """
{
"query" : "{__typename}",
"extensions": {
"persistedQuery": {
"version":1,
"sha256Hash":"ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38"
}
}
}""";

MockHttpServletResponse servletResponse = handleRequest(createServletRequest(document, "*/*"), handler);
assertThat(servletResponse.getContentAsString()).isEqualTo("{\"data\":{\"__typename\":\"Query\"}}");

document = """
{
"extensions":{
"persistedQuery":{
"version":1,
"sha256Hash":"ecf4edb46db40b5132295c0291d62fb65d6759a9eedfa4d5d612dd5ec54a6b38"
}
}
}""";

servletResponse = handleRequest(createServletRequest(document, "*/*"), handler);
assertThat(servletResponse.getContentAsString()).isEqualTo("{\"data\":{\"__typename\":\"Query\"}}");
}


private MockHttpServletRequest createServletRequest(String query, String accept) {
MockHttpServletRequest servletRequest = new MockHttpServletRequest("POST", "/");
servletRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import graphql.GraphQL;
import graphql.execution.instrumentation.Instrumentation;
Expand Down Expand Up @@ -137,6 +138,11 @@ public GraphQlSetup typeVisitorToTransformSchema(GraphQLTypeVisitor... visitors)
return this;
}

public GraphQlSetup configureGraphQl(Consumer<GraphQL.Builder> configurer) {
this.graphQlSourceBuilder.configureGraphQl(configurer);
return this;
}

public GraphQL toGraphQl() {
return this.graphQlSourceBuilder.build().graphQl();
}
Expand Down

0 comments on commit 7140e6c

Please sign in to comment.