Giter Club home page Giter Club logo

dropwizard-graphql's Introduction

Dropwizard GraphQL Bundle

Build Status Maven Central GitHub license Become a Patron

A bundle for providing GraphQL API endpoints in Dropwizard applications.

Dependency Info

<dependency>
    <groupId>com.smoketurner.dropwizard</groupId>
    <artifactId>graphql-core</artifactId>
    <version>2.0.7-1</version>
</dependency>

Usage

Add a GraphQLBundle to your Application class.

@Override
public void initialize(Bootstrap<MyConfiguration> bootstrap) {
    // ...
    final GraphQLBundle<HelloWorldConfiguration> bundle = new GraphQLBundle<HelloWorldConfiguration>() {
        @Override
        public GraphQLFactory getGraphQLFactory(HelloWorldConfiguration configuration) {

            final GraphQLFactory factory = configuration.getGraphQLFactory();
            // the RuntimeWiring must be configured prior to the run()
            // methods being called so the schema is connected properly.
            factory.setRuntimeWiring(buildWiring(configuration));
            return factory;
        }
    };
    bootstrap.addBundle(bundle);
}

Adding GraphQL along with REST API Endpoints in Dropwizard

To use GraphQL along with REST APIs in dropwizard you need to change the root path in the bundle which we add in the main class of dropwizard. Otherwise the bundle may conflict with root path of REST API's.

You need to add the root path by overiding the initialize method in GraphQL bundle.

@Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/assets", "/", "index.htm", "graphql-playground"));
  }

This is the default initialize method in GraphQL bundle. If you want to expose your GraphQL endpoint at localhost:8080/graphql then you have to change the path in the AssetBundle constructor.

Now the overriden method which we add while adding bundle is

@Override
  public void initialize(Bootstrap<?> bootstrap) {
    bootstrap.addBundle(new AssetsBundle("/assets", "/graphql", "index.htm", "graphql-playground"));
    //graphql is the endpoint which is concerned with graphql
  }

This avoids conflict between REST API and GraphQL endpoints.

When we start the dropwizard server the GraphQL playground looks for GraphQL schema.GraphQL dropwizard creates a schema.json file after processing our GraphQL schema. The GraphQL playground looks out for this schema. It looks out at /graphql from the GraphQL endpoint.If you wish to change where the GraphQL playground looks for this schema file then you may override the run method in GraphQL bundle class.

If we want our schema.json to be available at localhost:8080/graphql/query then the overridden method should like this.

@Override
  public void run(final C configuration, final Environment environment) throws Exception {
    final GraphQLFactory factory = getGraphQLFactory(configuration);

    final PreparsedDocumentProvider provider =
        new CachingPreparsedDocumentProvider(factory.getQueryCache(), environment.metrics());

    final GraphQLSchema schema = factory.build();

    final GraphQLQueryInvoker queryInvoker =
        GraphQLQueryInvoker.newBuilder()
            .withPreparsedDocumentProvider(provider)
            .withInstrumentation(factory.getInstrumentations())
            .build();

    final graphql.kickstart.servlet.GraphQLConfiguration config =
        graphql.kickstart.servlet.GraphQLConfiguration.with(schema).with(queryInvoker).build();

    final GraphQLHttpServlet servlet = GraphQLHttpServlet.with(config);

    environment.servlets().addServlet("graphql", servlet).addMapping("/query", "/schema.json");
  }

Example Application

This bundle includes a modified version of the HelloWorldApplication from Dropwizard's Getting Started documentation.

You can execute this application on your local machine then running:

./mvnw clean package
java -jar graphql-example/target/graphql-example-2.0.7-2-SNAPSHOT.jar server graphql-example/hello-world.yml

This will start the application on port 8080 with a GraphQL Playground interface for exploring the API.

Support

Please file bug reports and feature requests in GitHub issues.

License

Copyright (c) 2020 Smoke Turner, LLC

This library is licensed under the Apache License, Version 2.0.

See http://www.apache.org/licenses/LICENSE-2.0.html or the LICENSE file in this repository for the full license text.

dropwizard-graphql's People

Contributors

jplock avatar dependabot[bot] avatar dependabot-preview[bot] avatar dependabot-support avatar rcleps avatar sullis avatar phact avatar aneessh18 avatar

Watchers

James Cloos 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.