Graphql-spring-boot, combining GRAPHQL's spring Boot starters Class Library overview

Source: Internet
Author: User
Tags http post

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.