{"id":13565437,"url":"https://github.com/tibor-kocsis/vertx-graphql-utils","last_synced_at":"2025-04-03T22:31:27.860Z","repository":{"id":215184667,"uuid":"101883035","full_name":"tibor-kocsis/vertx-graphql-utils","owner":"tibor-kocsis","description":"Vert.x GraphQL utils","archived":false,"fork":false,"pushed_at":"2018-05-30T08:46:09.000Z","size":115,"stargazers_count":22,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-04T19:41:16.294Z","etag":null,"topics":["graphql","vertx"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tibor-kocsis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-08-30T13:11:53.000Z","updated_at":"2024-03-31T14:19:53.000Z","dependencies_parsed_at":"2024-01-06T20:44:09.706Z","dependency_job_id":null,"html_url":"https://github.com/tibor-kocsis/vertx-graphql-utils","commit_stats":null,"previous_names":["tibor-kocsis/vertx-graphql-utils"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tibor-kocsis%2Fvertx-graphql-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tibor-kocsis%2Fvertx-graphql-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tibor-kocsis%2Fvertx-graphql-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tibor-kocsis%2Fvertx-graphql-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tibor-kocsis","download_url":"https://codeload.github.com/tibor-kocsis/vertx-graphql-utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247090230,"owners_count":20881932,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","vertx"],"created_at":"2024-08-01T13:01:46.946Z","updated_at":"2025-04-03T22:31:25.786Z","avatar_url":"https://github.com/tibor-kocsis.png","language":"Java","funding_links":[],"categories":["Java","Exposing a Schema"],"sub_categories":["Code First"],"readme":"# vertx-graphql-utils\n\nThis 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.  \n\n### Getting started with gradle\n\nMake sure jcenter is among your repos:\n\n```\nrepositories {\n    jcenter()\n}\n\n```\n\nDependency:\n\n```\ndependencies {\n  compile 'com.github.tibor-kocsis:vertx-graphql-utils:2.0.6'\n}\n\n```\n\n### Project contents\n\n - Vert.x RouteHandler for executing GraphQL queries with a Vertx HttpServer (only POST supported for now, for request/response formats see the [GraphQL docs](http://graphql.org/learn/serving-over-http/))\n \n \n```java\nRouter router = Router.router(vertx); // vert.x router\nGraphQLSchema schema = ...\nrouter.post(\"/graphql\").handler(BodyHandler.create()); // we need the body\nrouter.post(\"/graphql\").handler(GraphQLPostRouteHandler.create(schema));\n```\n\n - AsyncDataFetcher interface with a Handler\u003cAsyncResult\u003e parameter you should use\n\n\n```java\nAsyncDataFetcher\u003cString\u003e helloFieldFetcher = (env, handler) -\u003e {\n\tvertx.\u003cString\u003e executeBlocking(fut -\u003e {\n\t\tfut.complete(\"world\");\n\t}, handler);\n};\n\nGraphQLObjectType query = newObject()\n    .name(\"query\")\n    .field(newFieldDefinition()\n            .name(\"hello\")\n            .type(GraphQLString)\n            .dataFetcher(helloFieldFetcher))\n    .build(); \n```\n \n - A vert.x Future based interface for executing GraphQL queries\n \n```java\nGraphQLSchema schema = ...\nAsyncGraphQLExec asyncGraphQL = AsyncGraphQLExec.create(schema);\nFuture\u003cJsonObject\u003e queryResult = asyncGraphQL.executeQuery(\"query { hello }\", null, null, null); // query, operationName, context, variables\nqueryResult.setHandler(res -\u003e {\n\tJsonObject json = res.result();\n}); \n```\n \n - Some util functions to parse IDL schema from file or content\n \n ```java\nFuture\u003cGraphQLSchema\u003e fromFile(String path, RuntimeWiring runtimeWiring);\nGraphQLSchema fromString(String schemaString, RuntimeWiring runtimeWiring);\n ```\n\n - A vert.x web BodyCodec and a basic query builder to perform graphql queries\n \n ```java\nclass Hero {\n\t// fields: id, name, age\n} \n \nJsonObject query = GraphQLQueryBuilder\n\t\t.newQuery(\"query ($id: ID!) { hero(id: $id) { id name age } }\")\n\t\t.var(\"id\", 10)\n\t\t.build();\n\nwebClient.post(\"/graphql\").as(GraphQLBodyCodec.queryResult()).sendJson(query, res -\u003e {\n\tGraphQLQueryResult body = res.result().body();\n\tHero hero = body.getData(\"hero\", Hero.class);\n}\n ```\n \n### Minimal Vert.x HttpServer example\n\n```java\nimport com.github.tkocsis.vertx.graphql.codec.GraphQLBodyCodec;\nimport com.github.tkocsis.vertx.graphql.datafetcher.AsyncDataFetcher;\nimport com.github.tkocsis.vertx.graphql.model.GraphQLQueryResult;\nimport com.github.tkocsis.vertx.graphql.routehandler.GraphQLPostRouteHandler;\nimport com.github.tkocsis.vertx.graphql.utils.GraphQLQueryBuilder;\n\nimport graphql.Scalars;\nimport graphql.schema.GraphQLFieldDefinition;\nimport graphql.schema.GraphQLObjectType;\nimport graphql.schema.GraphQLSchema;\nimport io.vertx.core.Vertx;\nimport io.vertx.core.json.JsonObject;\nimport io.vertx.ext.web.Router;\nimport io.vertx.ext.web.client.WebClient;\nimport io.vertx.ext.web.client.WebClientOptions;\nimport io.vertx.ext.web.handler.BodyHandler;\n\n@Ignore\npublic class MinimalVertxExample {\n\n\tpublic static void main(String[] args) {\n\t\tVertx vertx = Vertx.vertx();\n\t\t// create the router\n\t\tRouter router = Router.router(vertx);\n\t\t\n\t\t// setup an async datafetcher\n\t\tAsyncDataFetcher\u003cString\u003e helloFieldFetcher = (env, handler) -\u003e {\n\t\t\tvertx.\u003cString\u003e executeBlocking(fut -\u003e {\n\t\t\t\tfut.complete(\"world\");\n\t\t\t}, handler);\n\t\t};\n\t\t\n\t\t// setup graphql helloworld schema\n\t\tGraphQLObjectType query = GraphQLObjectType.newObject()\n\t\t        .name(\"query\")\n\t\t        .field(GraphQLFieldDefinition.newFieldDefinition()\n\t\t                .name(\"hello\")\n\t\t                .type(Scalars.GraphQLString)\n\t\t                .dataFetcher(helloFieldFetcher))\n\t\t        .build(); \n\t\t\n\t\tGraphQLSchema schema = GraphQLSchema.newSchema()\n\t\t\t\t.query(query)\n\t\t\t\t.build();\n\t\t\n\t\trouter.post(\"/graphql\").handler(BodyHandler.create()); // we need the body\n\t\t// create the graphql endpoint\n\t\trouter.post(\"/graphql\").handler(GraphQLPostRouteHandler.create(schema));\n\t\t\n\t\t// start the http server and make a call\n\t\tvertx.createHttpServer().requestHandler(router::accept).listen(8080);\n\t\t\n\t\t// test with vert.x webclient\n\t\tWebClient webClient= WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));\n\t\tJsonObject gqlQuery = GraphQLQueryBuilder.newQuery(\"query { hello }\").build();\n\t\twebClient.post(\"/graphql\").as(GraphQLBodyCodec.queryResult()).sendJson(gqlQuery, res -\u003e {\n\t\t\tGraphQLQueryResult queryResult = res.result().body();\n\t\t\tSystem.out.println(queryResult.getData(\"hello\", String.class)); // prints world\n\t\t});\n\t}\n}\n```\n\nFetching data with GraphiQL Chrome extension: \n\n![GrapiQL Chrome extension](https://raw.githubusercontent.com/tibor-kocsis/vertx-graphql-utils/master/doc/graphiql.png \"GraphiQL Chrome extension\")\n\nSee the unit tests for more detailed examples and use cases.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftibor-kocsis%2Fvertx-graphql-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftibor-kocsis%2Fvertx-graphql-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftibor-kocsis%2Fvertx-graphql-utils/lists"}