Skip to content

graphql-java/graphql-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

139 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-java

Friendly warning: This library is currently under development and not yet stable.

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.

Table of Contents

Overview

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.

Hello World

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}
    }
}

Getting started

Will be available soon via Bintray repository.

Currently: Please clone and Build it.

Manual

Schema definition

Built-in Types

The Scalars Class has the following built-in types:

  • GraphQLString
  • GraphQLBoolean
  • GraphQLInt
  • GraphQLFloat
Creating a new Object Type

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();
Creating a new Interface Type

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();
Creating a new Enum Type

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

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.

Example: GraphQL Test

Complexer examples: StarWars query tests

Execution strategies

All fields in a SelectionSet are executed in parallel per default (via a Thread-Pool).

The first level of a Mutation is executed serially.

Build it

Just clone the repo and type

./gradlew build

In build/libs you will find the jar file.

Running the tests:

./gradlew test

Details

The 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.

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.

Feedback

I would appreciate any feedback via Twitter @andimarek or Pull request/Issue.

About

GraphQL Java implementation

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages