Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding documentation for dropwizard-graphql #49

Closed
aneessh18 opened this issue Aug 2, 2020 · 2 comments
Closed

Adding documentation for dropwizard-graphql #49

aneessh18 opened this issue Aug 2, 2020 · 2 comments

Comments

@aneessh18
Copy link
Contributor

The present hello world documentation does provide for starting GraphQL in Dropwizard But if we need to write REST and GraphQL APIs in the same Dropwizard application then we may need to look into the source code and tweak according to it.

Couple of tweaks which would help to expose both GraphQL and REST API endpoints simultaneously.

  1. We have to change the root path in the GraphQL bundle which we add in the initialize method.
  2. We have to also change the environment variables in run function in the GraphQL bundle class so that the GraphQL playground could find the schema.json file.

We have to override these methods in the main class of the Dropwizard app.
Now the changed versions of these methods will be :

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

@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("/schema", "/schema.json");
  // for exposing the graphql schema at localhost:8080/graphql/schema 

I found these tweaks useful for exposing REST and GraphQL APIs simultaneously. Raising this issue so that it would be helpful for those who would be facing the same issue. Adding these tweaks in the docs would be really helpful for someone who would want to try out GraphQL in Dropwizard.

@jplock
Copy link
Member

jplock commented Aug 5, 2020

Mind submitting a PR?

@aneessh18
Copy link
Contributor Author

aneessh18 commented Aug 7, 2020

Mind submitting a PR?

Added the documentation and created a PR. Would love to hear feedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants