Play 1 graphql java server

Hi,

We have been moving to React in the Front End. Now we are replacing REST with graphQL. Unlike in spring boot, there is no starter, so it may be tricky to get it working right, so this is the code for play 1.x:

In dependencies add:

    - com.graphql-java -> graphql-java 13.0

in routes add

POST /graphql					GraphQLController.index
GET /graphql					GraphQLController.index
OPTIONS /graphql					GraphQLController.options

An helper class for parameters:

package controllers;

import java.util.HashMap;
import java.util.Map;

public class GraphParams {

	public Map<String, Object> variables = new HashMap<>();
	public String operationName;
	public String query;

}

The controller code is:

package controllers;

import java.io.IOException;
import java.io.InputStreamReader;

import com.google.gson.Gson;

import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphUtils;
import play.Logger;
import play.Play;
import play.mvc.Before;
import play.mvc.Controller;

public class GraphQLController extends Controller {

	@Before
	public void cors() {

		response.setHeader("Access-Control-Allow-Origin", ".domain.cl");
	}

	public static void index() throws IOException {

		GraphUtils fetchers = Play.runningInTestMode() ? new GraphUtils() : GraphUtils.getInstance();
		GraphQL grapQL = fetchers.getGraphql();
		Gson g = new Gson();
		GraphParams params = g.fromJson(new InputStreamReader(request.body), GraphParams.class);

		ExecutionInput executionInput = ExecutionInput.newExecutionInput().operationName(params.operationName)
				.query(params.query).variables(params.variables).build();
		ExecutionResult executionResult = grapQL.execute(executionInput);
		renderJSON(executionResult);

	}

	public static void options() {

		response.setHeader("Allow", "POST, GET");
	}

}

And our GraphUtils who resolve the queries, it uses a singleton in production:

public class GraphUtils {

	private GraphQL graphql;
	private static GraphUtils instance = new GraphUtils();

	DataFetcher dataFetcherDispositivo = new DataFetcher<DispositivoDTO>() {
		@Override
		public DispositivoDTO get(DataFetchingEnvironment environment) {
			Integer id = environment.getArgument("id");
			if (id == null)
				throw new RuntimeException("No viene el id para buscar proceso");

			Dispositivo dispositivo = Dispositivo.findById(id.longValue());
			if (dispositivo == null)
				throw new RuntimeException("No existe proceso " + id);

			return dispositivo.dto();
		}
	};

	public GraphUtils() {

		super();
		SchemaParser schemaParser = new SchemaParser();

		TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(new File("conf/schema.graphqls"));

		Builder wiring = RuntimeWiring.newRuntimeWiring();

		RuntimeWiring runtimeWiring = wiring.type("Query", builder -> {
			builder.dataFetcher("dispositivo", dataFetcherDispositivo);
			return builder;
		}).build();

		SchemaGenerator schemaGenerator = new SchemaGenerator();
		GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);

		graphql = GraphQL.newGraphQL(graphQLSchema).build();
	}

	public GraphQL getGraphql() {
		return graphql;
	}

	public static synchronized GraphUtils getInstance() {

		return instance;
	}

}

I hope it helps, feel free to make any questions or enhancements, better here for sharing.

Bye,
Hans