forked from spring-io/spring-asciidoctor-backends
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support programmatic GraphQLSchema creation
Prior to this commit, the `GraphQLAutoConfiguration` would only consider the case where the GraphQL schema is read as an SDL from a configured location. GraphQL also supports the programmatic creation of the schema and this case needs to be supported by the auto-configuration. This commit updates the auto-configuration to disable the schema creation from the SDL if a `GraphQL.Builder` bean is already contributed by the application. Fixes spring-iogh-4
- Loading branch information
Showing
3 changed files
with
152 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...hql-web/src/main/java/org/springframework/boot/graphql/MissingGraphQLSchemaException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2002-2020 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.graphql; | ||
|
||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Exception thrown when no GraphQL schema is available. | ||
*/ | ||
public class MissingGraphQLSchemaException extends RuntimeException { | ||
|
||
private final String path; | ||
|
||
MissingGraphQLSchemaException(String path) { | ||
super(StringUtils.hasText(path) ? "Path to GraphQL schema not configured" : "Cannot find schema file at: " | ||
+ path + " (please add a schema file or check your GraphQL configuration)"); | ||
this.path = path; | ||
} | ||
|
||
public String getPath() { | ||
return this.path; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
...hql-web/src/test/java/org/springframework/boot/graphql/GraphQLAutoConfigurationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2002-2020 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.graphql; | ||
|
||
import graphql.GraphQL; | ||
import graphql.schema.GraphQLObjectType; | ||
import graphql.schema.GraphQLSchema; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import static graphql.Scalars.GraphQLString; | ||
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; | ||
import static graphql.schema.GraphQLObjectType.newObject; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link GraphQLAutoConfiguration} | ||
*/ | ||
class GraphQLAutoConfigurationTests { | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(GraphQLAutoConfiguration.class)); | ||
|
||
|
||
@Test | ||
void shouldFailWhenSchemaFileIsMissing() { | ||
contextRunner.run((context) -> { | ||
assertThat(context).hasFailed(); | ||
assertThat(context).getFailure().getRootCause().isInstanceOf(MissingGraphQLSchemaException.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldCreateBuilderWithSdl() { | ||
contextRunner | ||
.withPropertyValues("spring.graphql.schema:classpath:books/schema.graphqls") | ||
.run((context) -> { | ||
assertThat(context).hasSingleBean(GraphQL.Builder.class); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldUseProgrammaticallyDefinedBuilder() { | ||
contextRunner | ||
.withPropertyValues("spring.graphql.schema:classpath:books/schema.graphqls") | ||
.withUserConfiguration(CustomGraphQLBuilderConfiguration.class) | ||
.run((context) -> { | ||
assertThat(context).hasBean("customGraphQLBuilder"); | ||
assertThat(context).hasSingleBean(GraphQL.Builder.class); | ||
}); | ||
} | ||
|
||
@Configuration | ||
static class CustomGraphQLBuilderConfiguration { | ||
|
||
@Bean | ||
public GraphQL.Builder customGraphQLBuilder() { | ||
GraphQLObjectType queryType = newObject() | ||
.name("helloWorldQuery") | ||
.field(newFieldDefinition() | ||
.type(GraphQLString) | ||
.name("hello")) | ||
.build(); | ||
GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build(); | ||
return GraphQL.newGraphQL(schema); | ||
} | ||
} | ||
|
||
} |