This article uses MAVEN to build a spring boot app from scratch to publish a Web server that supports GRAPHQL.
1. Execution of MVN archetype:generate
Templates can be selected (but existing templates tend to keep up with technical advances), or they can just generate a basic MAVEN project.
2. Edit the generated pom.xml file so that it supports the Spring boot feature first
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId> spring-boot-starter-parent</artifactid>
<version>2.0.1.RELEASE</version>
</parent >
<dependencies>
<dependency>
<groupid>org.springframework.boot</groupid >
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</ dependencies>
<build>
<plugins>
<plugin>
<groupId> Org.springframework.boot</groupid>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3. Create Springbootapplication App Entry class
@SpringBootApplication public
class Eris4japplication {public
static void Main (string[] args) {
Springapplication.run (Graphqljapplication.class, args);
}
}
4. Edit the Pom.xml file to support the GRAPHQL service feature
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId> graphql-spring-boot-starter</artifactid>
<version>4.0.0</version>
</dependency >
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId> graphiql-spring-boot-starter</artifactid>
<version>4.0.0</version>
</dependency >
Note that Graphql-spring-boot relies on Graphql-java. Although the latest version of Graphql-java is currently 8.0, the Graphql-spring-boot 4.0.0 project is not supported. If you combine Graphql-java 8.0 with Graphql-spring-boot 4.0.0, the following exception will be thrown when you start the GRAPHQL server:
Java.lang.ClassNotFoundException:graphql.execution.instrumentation.NoOpInstrumentation
The reason is that the Noopinstrumentation class exists only in Graphql-java 7.0 and previous versions, and no longer exists in Graphql-java 8.0.
In addition, Graphql-spring-boot relies on Graphql-java-servlet to provide a URI endpoints for Web Access, which defaults to http://localhost:8080/graphql/ . , we will introduce Graphql-java-servlet later.
Note that the reliance on graphiql-spring-boot-starter makes it possible for us to test access to http://localhost:8080/graphiql/
5. Define an executable Graphqlschema object and use @bean to label it as a spring bean
1) You can create the Graphqlschema object directly, 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);
2) You can also use Graphql-java-tools to dynamically create Graphqlschema objects with Graphqlresolver and graphqlscalar in them.
At this point, Graphql-java-tools will read all files under classpath with *.graphqls suffix and create the Graphqlschema object. Refer to the GRAPHQL specification for specific *.GRAPHQLS file definitions.
6. Start the spring boot application so that the GRAPHQL server is ready
There are two main ways to start the Spring boot application, one in the IDE, the Run application graphqlapplication, and the other on the command line, to perform the Java-jar Target/myprojectname.jar.
After the GRAPHQL service is started, its URI defaults to http://localhost:8080/graphql/, and of course it can be in spring The boot application.properties configuration file is configured as follows to change its default URI:
Graphql.servlet.mapping=/mygraphql
7. Open a browser to access the GRAPHQL service
1) Get request schema for GRAPHQL server
Http://localhost:8080/graphql/schema.json
2) Post request query specific data http://localhost:8080/graphql
{"Query": "{Hello}"}