All Projects → tibor-kocsis → vertx-graphql-utils

tibor-kocsis / vertx-graphql-utils

Licence: Apache-2.0 license
Vert.x GraphQL utils

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to vertx-graphql-utils

vertx-rabbitmq-client
Vert.x RabbitMQ Service
Stars: ✭ 71 (+222.73%)
Mutual labels:  vertx
vertx-kotlin-example
Vert.x + Kotlin example
Stars: ✭ 12 (-45.45%)
Mutual labels:  vertx
vertx-mail-client
No description or website provided.
Stars: ✭ 30 (+36.36%)
Mutual labels:  vertx
Websockets-Vertx-Flink-Kafka
A simple request response cycle using Websockets, Eclipse Vert-x server, Apache Kafka, Apache Flink.
Stars: ✭ 14 (-36.36%)
Mutual labels:  vertx
microtrader
A fictitious stock trading microtrader application
Stars: ✭ 58 (+163.64%)
Mutual labels:  vertx
grooveex
Extension module for vertx-groovy adding methods and syntaxic sugar
Stars: ✭ 14 (-36.36%)
Mutual labels:  vertx
spring-webflux-research
spring webflux research
Stars: ✭ 42 (+90.91%)
Mutual labels:  vertx
vxrifa
Utility library for Vert.X that allows using strong-typed interfaces in communication through EventBus
Stars: ✭ 15 (-31.82%)
Mutual labels:  vertx
reactiverse
The Reactiverse main entry point
Stars: ✭ 26 (+18.18%)
Mutual labels:  vertx
mod-tinkerpop-persistor
Vert.x 2.x Persistor Module for Tinkerpop-compatible Graph Databases
Stars: ✭ 17 (-22.73%)
Mutual labels:  vertx
vertx-graphql-example
Vert.x Server which exposes a GraphQL API
Stars: ✭ 29 (+31.82%)
Mutual labels:  vertx
rest.vertx
A JAX-RS like annotation processor for vert.x verticals and more
Stars: ✭ 138 (+527.27%)
Mutual labels:  vertx
cloudopt-next-example
Example of cloudopt-next
Stars: ✭ 22 (+0%)
Mutual labels:  vertx
openapi4j
OpenAPI 3 parser, JSON schema and request validator.
Stars: ✭ 92 (+318.18%)
Mutual labels:  vertx
vertx-contextual-logging
Contextual logging for Vertx applications
Stars: ✭ 19 (-13.64%)
Mutual labels:  vertx
elasticsearch-client
This client exposes the Elasticsearch Java High Level REST Client for Eclipse Vert.x applications.
Stars: ✭ 40 (+81.82%)
Mutual labels:  vertx
consul-cluster-manager
Consul - based cluster manager that can be plugged into Vert.x ecosystem.
Stars: ✭ 17 (-22.73%)
Mutual labels:  vertx
slush-vertx
No description or website provided.
Stars: ✭ 36 (+63.64%)
Mutual labels:  vertx
vertx-codegen
Vert.x code generator for asynchronous polyglot APIs
Stars: ✭ 95 (+331.82%)
Mutual labels:  vertx
vertx-vue-keycloak
This repo holds the source codes for the Medium Article "Vert.x + VueJS + OAuth2 in 5 steps"
Stars: ✭ 20 (-9.09%)
Mutual labels:  vertx

vertx-graphql-utils

This project contains some helper classes you may need to write a GraphQL vert.x http server. It uses graphql-java async execution environment combined with vert.x Future.

Getting started with gradle

Make sure jcenter is among your repos:

repositories {
    jcenter()
}

Dependency:

dependencies {
  compile 'com.github.tibor-kocsis:vertx-graphql-utils:2.0.6'
}

Project contents

  • Vert.x RouteHandler for executing GraphQL queries with a Vertx HttpServer (only POST supported for now, for request/response formats see the GraphQL docs)
Router router = Router.router(vertx); // vert.x router
GraphQLSchema schema = ...
router.post("/graphql").handler(BodyHandler.create()); // we need the body
router.post("/graphql").handler(GraphQLPostRouteHandler.create(schema));
  • AsyncDataFetcher interface with a Handler parameter you should use
AsyncDataFetcher<String> helloFieldFetcher = (env, handler) -> {
	vertx.<String> executeBlocking(fut -> {
		fut.complete("world");
	}, handler);
};

GraphQLObjectType query = newObject()
    .name("query")
    .field(newFieldDefinition()
            .name("hello")
            .type(GraphQLString)
            .dataFetcher(helloFieldFetcher))
    .build(); 
  • A vert.x Future based interface for executing GraphQL queries
GraphQLSchema schema = ...
AsyncGraphQLExec asyncGraphQL = AsyncGraphQLExec.create(schema);
Future<JsonObject> queryResult = asyncGraphQL.executeQuery("query { hello }", null, null, null); // query, operationName, context, variables
queryResult.setHandler(res -> {
	JsonObject json = res.result();
}); 
  • Some util functions to parse IDL schema from file or content
Future<GraphQLSchema> fromFile(String path, RuntimeWiring runtimeWiring);
GraphQLSchema fromString(String schemaString, RuntimeWiring runtimeWiring);
  • A vert.x web BodyCodec and a basic query builder to perform graphql queries
class Hero {
   // fields: id, name, age
} 

JsonObject query = GraphQLQueryBuilder
   	.newQuery("query ($id: ID!) { hero(id: $id) { id name age } }")
   	.var("id", 10)
   	.build();

webClient.post("/graphql").as(GraphQLBodyCodec.queryResult()).sendJson(query, res -> {
   GraphQLQueryResult body = res.result().body();
   Hero hero = body.getData("hero", Hero.class);
}

Minimal Vert.x HttpServer example

import com.github.tkocsis.vertx.graphql.codec.GraphQLBodyCodec;
import com.github.tkocsis.vertx.graphql.datafetcher.AsyncDataFetcher;
import com.github.tkocsis.vertx.graphql.model.GraphQLQueryResult;
import com.github.tkocsis.vertx.graphql.routehandler.GraphQLPostRouteHandler;
import com.github.tkocsis.vertx.graphql.utils.GraphQLQueryBuilder;

import graphql.Scalars;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import io.vertx.ext.web.handler.BodyHandler;

@Ignore
public class MinimalVertxExample {

	public static void main(String[] args) {
		Vertx vertx = Vertx.vertx();
		// create the router
		Router router = Router.router(vertx);
		
		// setup an async datafetcher
		AsyncDataFetcher<String> helloFieldFetcher = (env, handler) -> {
			vertx.<String> executeBlocking(fut -> {
				fut.complete("world");
			}, handler);
		};
		
		// setup graphql helloworld schema
		GraphQLObjectType query = GraphQLObjectType.newObject()
		        .name("query")
		        .field(GraphQLFieldDefinition.newFieldDefinition()
		                .name("hello")
		                .type(Scalars.GraphQLString)
		                .dataFetcher(helloFieldFetcher))
		        .build(); 
		
		GraphQLSchema schema = GraphQLSchema.newSchema()
				.query(query)
				.build();
		
		router.post("/graphql").handler(BodyHandler.create()); // we need the body
		// create the graphql endpoint
		router.post("/graphql").handler(GraphQLPostRouteHandler.create(schema));
		
		// start the http server and make a call
		vertx.createHttpServer().requestHandler(router::accept).listen(8080);
		
		// test with vert.x webclient
		WebClient webClient= WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));
		JsonObject gqlQuery = GraphQLQueryBuilder.newQuery("query { hello }").build();
		webClient.post("/graphql").as(GraphQLBodyCodec.queryResult()).sendJson(gqlQuery, res -> {
			GraphQLQueryResult queryResult = res.result().body();
			System.out.println(queryResult.getData("hello", String.class)); // prints world
		});
	}
}

Fetching data with GraphiQL Chrome extension:

GrapiQL Chrome extension

See the unit tests for more detailed examples and use cases.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].