Dropwizard GraphQL Bundle


License

License

Categories

Categories

DropWizard Container Microservices
GroupId

GroupId

com.smoketurner.dropwizard
ArtifactId

ArtifactId

graphql-core
Last Version

Last Version

2.0.12-1
Release Date

Release Date

Type

Type

jar
Description

Description

Dropwizard GraphQL Bundle
Dropwizard GraphQL Bundle

Download graphql-core

How to add to project

<!-- https://jarcasting.com/artifacts/com.smoketurner.dropwizard/graphql-core/ -->
<dependency>
    <groupId>com.smoketurner.dropwizard</groupId>
    <artifactId>graphql-core</artifactId>
    <version>2.0.12-1</version>
</dependency>
// https://jarcasting.com/artifacts/com.smoketurner.dropwizard/graphql-core/
implementation 'com.smoketurner.dropwizard:graphql-core:2.0.12-1'
// https://jarcasting.com/artifacts/com.smoketurner.dropwizard/graphql-core/
implementation ("com.smoketurner.dropwizard:graphql-core:2.0.12-1")
'com.smoketurner.dropwizard:graphql-core:jar:2.0.12-1'
<dependency org="com.smoketurner.dropwizard" name="graphql-core" rev="2.0.12-1">
  <artifact name="graphql-core" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.smoketurner.dropwizard', module='graphql-core', version='2.0.12-1')
)
libraryDependencies += "com.smoketurner.dropwizard" % "graphql-core" % "2.0.12-1"
[com.smoketurner.dropwizard/graphql-core "2.0.12-1"]

Dependencies

compile (3)

Group / Artifact Type Version
io.dropwizard : dropwizard-core jar
io.dropwizard : dropwizard-assets jar
com.graphql-java-kickstart : graphql-java-servlet jar 9.2.0

test (6)

Group / Artifact Type Version
io.dropwizard : dropwizard-testing jar
org.junit.jupiter : junit-jupiter jar 5.6.2
org.assertj : assertj-core jar 3.16.1
org.mockito : mockito-core jar 3.5.2
org.openjdk.jmh : jmh-core jar 1.25
org.openjdk.jmh : jmh-generator-annprocess jar 1.25

Project Modules

There are no modules declared in this project.

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.

com.smoketurner.dropwizard

Smoke Turner, LLC

Versions

Version
2.0.12-1
2.0.10-1
2.0.7-1
2.0.0-1
1.3.17-1
1.3.15-1
1.3.12-4
1.3.9-1
1.3.8-1
1.3.7-3
1.3.7-2
1.3.7-1
1.3.5-6
1.3.5-5
1.3.5-4
1.3.5-3
1.3.5-2
1.3.5-1
1.3.4-1
1.3.2-1
1.3.0-1
1.2.3-2
1.2.3-1
1.2.2-1
1.1.0-1
1.0.6-1
1.0.5-1