Coder Social home page Coder Social logo

graphql-java-servlet's Introduction

Build Status Maven Central Chat on Gitter

GraphQL Servlet

This module implements a GraphQL Java Servlet. It also supports Relay.js, Apollo and OSGi out of the box.

Downloading

You can download releases from maven central:

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.graphql-java:graphql-java-servlet:4.6.1'
}
<dependency>
    <groupId>com.graphql-java</groupId>
    <artifactId>graphql-java-servlet</artifactId>
    <version>4.6.1</version>
</dependency>

Usage

The servlet supports the following request formats:

  • GET request to ../schema.json: Get the result of an introspection query.
  • GET request with query parameters (query only, no mutation):
    • query
    • operationName (optional)
    • variables (optional)
  • POST body JSON object with fields:
    • query
    • operationName (optional)
    • variables (optional)
  • POST multipart part named "graphql" containing JSON object with fields:
    • query
    • operationName (optional)
    • variables (optional)
  • POST multipart parts named "query", "operationName" (optional), and "variables" (optional)

Standalone servlet

The simplest form of the servlet takes a graphql-java GraphQLSchema and an ExecutionStrategy:

GraphQLServlet servlet = new SimpleGraphQLServlet(schema, executionStrategy);

// or

GraphQLServlet servlet = new SimpleGraphQLServlet(schema, executionStrategy, operationListeners, servletListeners);

Servlet Listeners

You can also add servlet listeners to an existing servlet. These listeners provide hooks into query execution (before, success, failure, and finally) and servlet execution (before, success, error, and finally):

servlet.addListener(new GraphQLServletListener() {
    @Override
    GraphQLServletListener.RequestCallback onRequest(HttpServletRequest request, HttpServletResponse response) {

        return new GraphQLServletListener.RequestCallback() {
            @Override
            void onSuccess(HttpServletRequest request, HttpServletResponse response) {

            }

            @Override
            void onError(HttpServletRequest request, HttpServletResponse response, Throwable throwable) {

            }

            @Override
            void onFinally(HttpServletRequest request, HttpServletResponse response) {

            }
        }
    }

    @Override
    GraphQLServletListener.OperationCallback onOperation(GraphQLContext context, String operationName, String query, Map<String, Object> variables) {

        return new GraphQLServletListener.OperationCallback() {
            @Override
            void onSuccess(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {

            }

            @Override
            void onError(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data, List<GraphQLError> errors) {

            }

            @Override
            void onFinally(GraphQLContext context, String operationName, String query, Map<String, Object> variables, Object data) {

            }
        }
    }
})

Relay.js support

Relay.js support is provided by the EnhancedExecutionStrategy of graphql-java-annotations. You MUST pass this execution strategy to the servlet for Relay.js support.

This is the default execution strategy for the OsgiGraphQLServlet, and must be added as a dependency when using that servlet.

Apollo support

Query batching is supported, no configuration required.

Spring Framework support

To use the servlet with Spring Framework, either use the Spring Boot starter or simply define a ServletRegistrationBean in a web app:

@Bean
ServletRegistrationBean graphQLServletRegistrationBean(GraphQLSchema schema, ExecutionStrategy executionStrategy, List<GraphQLOperationListener> operationListeners) {
    return new ServletRegistrationBean(new SimpleGraphQLServlet(schema, executionStrategy, operationListeners), "/graphql");
}

OSGI support

The OsgiGraphQLServlet uses a "provider" model to supply the servlet with the required objects:

Examples

You can now find some example on how to use graphql-java-servlet.

OSGi Examples

Requirements

The OSGi examples use Maven as a build tool because it requires plugins that are not (yet) available for Gradle. Therefore you will need Maven 3.2+.

Building & running the OSGi examples

You can build the OSGi examples sub-projects by simply executing the following command from the examples/osgi directory:

mvn clean install

This will generate a complete Apache Karaf distribution in the following files:

 examples/osgi/apache-karaf-package/target/graphql-java-servlet-osgi-examples-apache-karaf-package-VERSION.tar.gz(.zip)

You can simply uncompress this file and launch the OSGi server using the command from the uncompressed directory:

bin/karaf

You should then be able to access the GraphQL endpoint at the following URL once the server is started:

http://localhost:8181/graphql/schema.json

If you see the JSON result of an introspection query, then all is ok. If not, check the data/log/karaf.log file for any errors.

We also provide a script file to do all of the building and running at once (only for Linux / MacOS ):

./buildAndRun.sh

Deploying inside Apache Karaf server

You can use the graphql-java-servlet as part of an Apache Karaf feature, as you can see in the example project here:

And here is a sample src/main/feature/feature.xml file to add some dependencies on other features:

Example GraphQL provider implementation

Here's an example of a GraphQL provider that implements three interfaces at the same time.

graphql-java-servlet's People

Contributors

apottere avatar denvned avatar fnovoa10 avatar jessthrysoee avatar jplock avatar karlhorky avatar metacosm avatar oliemansm avatar ronald-d-rogers avatar sergehuber avatar vojtechhabarta avatar yrashk avatar zdtaylor86 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.