Skip to content

Alternative construction #127

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

Merged
merged 4 commits into from
Nov 12, 2018
Merged

Alternative construction #127

merged 4 commits into from
Nov 12, 2018

Conversation

oliemansm
Copy link
Member

fixes #60

This PR provides simplifies the construction of the GraphQL Servlet and provides an alternative way as well through the proposed getConfiguration() method.

To create a new servlet you have two options. If you just want to use default settings and expose a schema use GraphQLHttpServlet.with(graphqlSchema).

If you want to control all the options use GraphQLHttpServlet.with(graphqlConfiguration). The configuration basically is the replacement for the Builder with the same functionality.

As per the suggestion in #60 you can also subclass GraphQLHttpServlet and override the getConfiguration() method as follows.

@WebServlet(name = "HelloServlet", urlPatterns = {"graphql"}, loadOnStartup = 1)
public class HelloServlet extends GraphQLHttpServlet {

  @Override
  protected GraphQLConfiguration getConfiguration() {
    return GraphQLConfiguration.with(createSchema()).build();
  }

  private GraphQLSchema createSchema() {
    String schema = "type Query{hello: String}";

    SchemaParser schemaParser = new SchemaParser();
    TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);

    RuntimeWiring runtimeWiring = newRuntimeWiring()
        .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
        .build();

    SchemaGenerator schemaGenerator = new SchemaGenerator();
    return schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
  }

}

For now the previous functionality has been deprecated. So no breaking changes yet.

@oliemansm oliemansm merged commit e19ab31 into master Nov 12, 2018
@oliemansm oliemansm deleted the alternative-construction branch May 11, 2023 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide an alternative way of initializing the SimpleGraphQLServlet
1 participant