The API query requests that the GRAPHQL server can provide are often implemented via HTTP. Spring boot makes it easy to build a Web service, so it naturally introduces the spring boot enabled GRAPHQL server.
Graphql-spring-boot is an auxiliary class library for spring boot applications, Graphql-java and Graphql-java-servlet based on the GRAPHQL Java implementation to spring Boot application in a way that builds a GRAPHQL server.
The full name of the Graphql-spring-boot class library is graphql and GRAPHIQL spring Framework Boot starters. GRAPHIQL is a test server that is similar to the GRAPHQL feature and is not detailed here.
The latest version of the Graphql-spring-boot class library is released in 2018.1.12 in 4.0.0. Its use requirements:
Java 1.8 Spring Boot 2.x.x 1. Composition
The Graphql-spring-boot class library contains two separate sub-projects: Com.graphql-java.graphql-spring-boot-starter Com.graphql-java.graphiql-spring-boot-starter
2. Configuration in the Spring boot MAVEN project
The following dependencies are added to the Pom.xml file:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId> graphql-spring-boot-starter</artifactid>
<version>4.0.0</version>
</dependency >
3. Define the Graphqlschema object and use the @bean callout as the spring bean
Here's an example that returns the Graphqlschema object directly in a simplified manner as follows:
@Bean
Graphqlschema schema () {
String schema = "type Query {hello:string}";
Schemaparser schemaparser = new Schemaparser ();
Typedefinitionregistry typedefinitionregistry = schemaparser.parse (schema);
Runtimewiring runtimewiring = newruntimewiring ()
. Type ("Query", builder-and Builder.datafetcher ("Hello", new Staticdatafetcher ("Xiangbin"))
. Build ();
Schemagenerator schemagenerator = new Schemagenerator ();
Return Schemagenerator.makeexecutableschema (Typedefinitionregistry, runtimewiring);
URI settings for 4.GraphQL server
Because Graphql-spring-boot relies on Graphql-java-servlet, the default open Web Access endpoint is /graphql, This is defined in the Graphql-java-servlet.
If you want to use a different endpoint, you can configure the following in the Application.properties file through the Spring boot configuration file:
GRAPHQL.SERVLET.MAPPING=/GRAPHQL
graphql.servlet.enabled=true
graphql.servlet.corsenabled=true
Or, configure the following in the Application.yaml file:
GRAPHQL:
servlet:
mapping:/graphql
enabled:true
corsenabled:true
5. Start the Spring boot app
You can run directly in the IDE, or you can execute Java-jar on the command line My_project.jar
6. Browser access
Using an HTTP POST request, the requested URI is http://localhost:8080/graphql/, and the requested body is {"Query": "{Hello}"}.
This gives the result of the response {"data": {"Hello": "Xiangbin"}}.
Reference Links:
Https://github.com/graphql-java/graphql-spring-boot