You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hint: This README documents the latest release, but master contains the current development version. So please make sure
to checkout the appropriate tag when looking for the version documented here.
This is a Java Implementation of GraphQL. The library aims for real-life usage in production.
It takes care of parsing and executing a GraphQL query. It doesn't take care of actually fetching any data:
Data comes from implementing callbacks or providing static data.
Code of Conduct
Please note that this project is released with a Contributor Code of Conduct.
By contributing to this project (commenting or opening PR/Issues etc) you are agreeing to follow this conduct, so please
take the time to read it.
Mailing List
If you have a question or want to discuss anything else related to this project:
There is a mailing list (Google Group) for graphql-java: graphql-java group
The Scalars Class has the following built-in types:
GraphQLString
GraphQLBoolean
GraphQLInt
GraphQLFloat
Creating a new Object Type
Example:
GraphQLObjectTypesimpsonCharacter = newObject()
.name("SimpsonCharacter")
.description("A Simpson character")
.field(newFieldDefinition()
.name("name")
.description("The name of the character.")
.type(GraphQLString))
.field(newFieldDefinition()
.name("mainCharacter")
.description("One of the main Simpson characters?")
.type(GraphQLBoolean))
.build();
Creating a new Interface Type
Example:
GraphQLInterfaceTypecomicCharacter = newInterface()
.name("ComicCharacter")
.description("A abstract comic character.")
.field(newFieldDefinition()
.name("name")
.description("The name of the character.")
.type(GraphQLString))
.build();
Every field definition has a DataFetcher. When no one is configured, a
PropertyDataFetcher is used.
PropertyDataFetcher fetches data from Map and Java Beans. So when the field name matches the Map key or
the property name of the source Object, no DataFetcher is needed.
Example of configuring a custom DataFetcher:
DataFetchercalculateComplicatedValue = newDataFetcher() {
@OverrideObjectget(DataFetchingEnvironmentenvironment) {
// environment.getSource() is the value of the surrounding// object. In this case described by objectTypeObjectvalue = ... // Perhaps getting from a DB or whatever returnvalue;
}
GraphQLObjectTypeobjectType = newObject()
.name("ObjectType")
.field(newFieldDefinition()
.name("someComplicatedValue")
.type(GraphQLString)
.dataFetcher(calculateComplicatedValue))
.build();
Executing
To execute a Query/Mutation against a Schema instantiate a new GraphQL Object with the appropriate arguments and then call execute().
The result of a Query is a ExecutionResult Object with the result and/or a list of Errors.
All fields in a SelectionSet are executed serially per default.
GraphQL takes as second constructor argument an optional ExecutorService.
When provided fields will be executed parallel, except the first level of a mutation operation.
It's recommended to use a ExecutorService to speed up execution.
Alternatively, schemas with nested lists may benefit from using a BatchedExecutionStrategy and creating batched DataFetchers with get() methods annotated @Batched.
JDK8 Lambdas
This project is built using JDK6. But if you're using JDK8 and above then you can also use lambdas.
Relay sends queries to the GraphQL server as JSON containing a query field and a variables field. The query field is a JSON string,
and the variables field is a map of variable definitions. A relay-compatible server will need to parse this JSON and pass the query
string to this library as the query and the variables map as the third argument to execute as shown below. This is the implementation
from the todomvc-relay-java example.
Please be a as focused and clear as possible and don't mix concerns. This includes refactorings mixed with bug-fixes/features, see Open Source Contribution Etiquette
It would be good to add a automatic test. All tests are written in Spock.
Development Build
The latest development build is available on Bintray.
Please look at Latest Build for the
latest version value.
Javadocs
See the project page for the javadocs associated with each release.
The implementation is in Java 6, but the tests are in Groovy and Spock.
The query parsing is done with ANTLR. The grammar is here.
The only runtime dependencies are ANTLR and Slf4J.
Acknowledgment
This implementation is based on the js reference implementation.
For example the StarWarSchema and the tests (among a lot of other things) are simply adapted to the Java world.
Related projects
graphql-rxjava: An execution strategy that makes it easier to use rxjava's Observable