This is a GraphQL Java implementation based on the specification and the JavaScript reference implementation.
Status: Parsing and executing are implemented. Validation is in place, but not complete. There will be a first beta-release soon.
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.
This is the famous "hello world" in graphql-java:
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
public class HelloWorld {
public static void main(String[] args) {
GraphQLObjectType queryType = newObject()
.field(newFieldDefinition()
.type(GraphQLString)
.name("hello")
.staticValue("world")
.build())
.build();
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.build();
Object result = new GraphQL(schema).execute("{hello}").getResult();
System.out.println(result);
// Prints: {hello=world}
}
}Will be available soon via Bintray repository.
Currently: Please clone and Build it.
The Scalars Class has the following built-in types:
GraphQLStringGraphQLBooleanGraphQLIntGraphQLFloat
Example:
GraphQLObjectType simpsonCharacter = newObject()
.name("SimpsonCharacter")
.description("A Simpson character")
.field(newFieldDefinition()
.name("name")
.description("The name of the character.")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("mainCharacter")
.description("One of the main Simpson characters?")
.type(GraphQLBoolean)
.build())
.build();Example:
GraphQLObjectType comicCharacter = newObject()
.name("ComicCharacter")
.description("A abstract comic character.")
.field(newFieldDefinition()
.name("name")
.description("The name of the character.")
.type(GraphQLString)
.build())
.build();Example:
GraphQLEnumType colorEnum = newEnum()
.name("Color")
.description("Supported colors.")
.value("RED")
.value("GREEN")
.value("BLUE")
.build();
GraphQLSchema.newSchema() returns a new Builder to define a new Schema. All other types are created with the same pattern:
newObject, newFieldDefinition etc.
A full schema example (stolen from the js reference implementation): StarWarsSchema
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.
Example: GraphQL Test
Complexer examples: StarWars query tests
All fields in a SelectionSet are executed in parallel per default (via a Thread-Pool).
The first level of a Mutation is executed serially.
Just clone the repo and type
./gradlew buildIn build/libs you will find the jar file.
Running the tests:
./gradlew testThe implementation is in Java 7, 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 probably in the future Slf4J.
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.
I would appreciate any feedback via Twitter @andimarek or Pull request/Issue.