This library provides helpers for integrating graphql-java with your JVM application. These simplify schema configuration and input mapping, and are in active use for GraphQL services at New Relic.
<dependency>
<groupId>com.newrelic.graphql</groupId>
<artifactId>core</artifactId>
<version>0.1.1</version>
</dependency>
compile("com.newrelic.graphql:core:0.1.1")
Version specific Javadocs can be found here.
SimpleGraphQLBuilder
provides cleaner wireup of an executable schema. It expects a Reader
with your GraphQL schema contents. Further configuration is done with a fluent interface.
Reader schemaReader = //... Read your GraphQL SDL from somewhere
GraphQL graphql = new SimpleGraphQLBuilder(schemaReader)
.fetcher("Query", "myField", new QueryMyFieldFetcher())
.scalar("MyScalar",
GraphQLScalarType
.newScalar()
.name("MyScalar")
.coercing(new MyScalarCoercing())
.build())
.exceptionHandler(new MyDataFetcherExceptionHandler())
.build()
Defauls from the builder:
- The
fetcher
method associates aDataFetcher
implementation with a GraphQL type and field definition - Unregistered scalars are defaulted to a String coercion for simplicity of starting up.
- The default type resolver expects result class names to align with GraphQL types. You only need to override this if your scheme is different.
- Defaults exception handler to
SimpleDataFetcherExceptionHandler
from thegraphql-java
library
If functionality isn't available on the the fluent interface, a configure
callback provides access the underlying graphql-java
objects.
GraphQL graphql = new SimpleGraphQLBuilder(schemaReader)
.configure((typeRegistry, runtimeWiringBuilder) -> {
// Use the builder to do your work
})
.build()
GraphQLInputMapper
assists in handling incoming input types. It relies on Jackson, and configures to work between the graphql-java
types and your custom classes.
GraphQLInputMapper mapper = new GraphQLInputMapper("com.newrelic.my.model");
public T get(DataFetchingEnvironment environment) throws Exception {
InputObject obj = mapper.convert(
environment.getArgument("inputObject"),
environment.getFieldDefinition().getArgument("inputObject").getType());
//...
}
At New Relic we've found lots of uses for custom scalars, especially around time. These predefined scalars are available and registered by default for use in your application. To use these, simply include the related scalar declaration as below in your GraphQL schema file, then use the related Java class in your queries or mutations.
scalar EpochMilliseconds
- maps to wrapper around JavaInstant
scalar EpochSeconds
- maps to wrapper around JavaInstant
scalar Milliseconds
- maps to wrapper around JavaDuration
scalar Seconds
- maps to wrapper around JavaDuration
scalar Minutes
- maps to wrapper around JavaDuration
scalar DateTime
- maps toZonedDateTime
from ISO 8601 compatible strings
public T get(DataFetchingEnvironment environment) throws Exception {
DateTime theDate = environment.getArgument("theDate");
}
To opt out of auto-registration of these predefined scalars, use the usePredefinedScalars
method on SimpleGraphQLBuilder
.
- Java 8 or greater
The project uses Gradle 6 and GitHub Actions for building.
To compile, run the tests and build the jars:
$ ./gradlew build
This project uses the google-java-format code style, and it is easily applied via an included gradle plugin:
$ ./gradlew googleJavaFormat verifyGoogleJavaFormat
Please be sure to run the formatter before committing any changes. There is a pre-commit-hook.sh
which can
be applied automatically before commits by moving it into .git/hooks/pre-commit
.
newrelic-graphql-java-core is licensed under the Apache 2.0 License.
newrelic-graphql-java-core also uses source code from third party libraries. Full details on which libraries are used and the terms under which they are licensed can be found in the third party notices document.
Full details are available in our CONTRIBUTING.md file. We'd love to get your contributions! Keep in mind when you submit your pull request, you'll need to sign the CLA via the click-through using CLA-Assistant. You only have to sign the CLA one time per project. To execute our corporate CLA, which is required if your contribution is on behalf of a company, or if you have any questions, please drop us an email at opensource@newrelic.com.