Home

Awesome

<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

Table of Contents

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

GraphQL and GraphiQL Spring Framework Boot Starters

Build Status

GraphQL Starter

Download

GraphiQL Starter

Download

Intro

Repository contains:

Requires

Add repository:

repositories {
    // stable build
    jcenter()
    // development build
    maven { url  "http://dl.bintray.com/oembedler/maven" }
}

Dependency:

dependencies {
  compile 'com.embedler.moon.graphql.boot:graphql-spring-boot-starter:INSERT_LATEST_VERSION_HERE'
  
  // to embed GraphiQL tool
  compile 'com.embedler.moon.graphql.boot:graphiql-spring-boot-starter:INSERT_LATEST_VERSION_HERE'
}

How to use the latest build with Maven:

<repository>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <id>bintray-oembedler-maven</id>
    <name>bintray</name>
    <url>http://dl.bintray.com/oembedler/maven</url>
</repository>

Dependency:

<dependency>
    <groupId>com.embedler.moon.graphql.boot</groupId>
    <artifactId>graphql-spring-boot-starter</artifactId>
    <version>INSERT_LATEST_VERSION_HERE</version>
</dependency>

<!-- to embed GraphiQL tool -->
<dependency>
    <groupId>com.embedler.moon.graphql.boot</groupId>
    <artifactId>graphiql-spring-boot-starter</artifactId>
    <version>INSERT_LATEST_VERSION_HERE</version>
</dependency>

Enable GraphQL Server

Server becomes accessible at /graphql if graphql-spring-boot-starter added as a dependency to a boot application and @EnableGraphQLServer annotation is set at the main java configuration class. GraphQL schemas are automatically discovered extracting all classes from Spring context marked as @GraphQLSchema.

Request parameters:

GraphQL will first look for each parameter in the URL's query-string:

/graphql?query=query+getUser($id:ID){user(id:$id){name}}&variables={"id":"4"}

If not found in the query-string, it will look in the POST request body.

Server uses [commons-fileupload][] middleware to add support for multipart/form-data content, which may be useful for GraphQL mutations involving uploading files (see test application for more details). GraphQLContext is a map of objects (context) for the current query execution. In order to get access to uploaded file com.oembedler.moon.graphql.boot.GraphQLContext should be passed as input parameter to datafetcher or mutation (don't need to be annotated). Calling method GraphQLContext.getUploadedFile() returns instance of MultipartFile.

If the POST body has not yet been parsed, graphql-express will interpret it depending on the provided Content-Type header.

Available Spring Boot configuration parameters (either application.yml or application.properties):

Server can host multiple schemas (all are registered at the startup time).

To run query against particular schema - HTTP header graphql-schema parameter passed along with the query should contain graphql schema name of interest.

graphql:
      server:
               mapping: /graphql
               corsEnabled: true
               suppressSpringResponseCodes: true
               query-key: query
               variables-key: variables
               uploadMaxFileSize: 128KB
               uploadMaxRequestSize: 128KB
      schema:
               clientMutationIdName: clientMutationId
               injectClientMutationId: true
               allowEmptyClientMutationId: false
               mutationInputArgumentName: input
               outputObjectNamePrefix: Payload
               inputObjectNamePrefix: Input
               schemaMutationObjectName: Mutation

To facilitate access from Nodejs frontend to GraphQL backend by default system enables global CORS filter for /graphql/** context. The corsEnabled can be set to false to disable it.

By default system register GlobalDefaultExceptionHandler which suppresses Spring framework error responses and responds with standard GraphQL server error. Application configuration suppressSpringResponseCodes property can be set to false to disable that handler.

Enable GraphiQL Tool

Tool becomes accessible at the root / if graphiql-spring-boot-starter added as a dependency to a boot application.

Note that GraphQL server must be available at /graphql context to be discovered by GraphiQL.

Contributions

Contributions are welcome.

Tips:

License

graphql-spring-boot-starter and graphiql-spring-boot-starter are licensed under the MIT License. See LICENSE for details.

spring-graphql-common License

graphql-java License

graphiql License

graphql-js License