• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

google/rejoiner: Generates a unified GraphQL schema from gRPC microservices and ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

google/rejoiner

开源软件地址(OpenSource Url):

https://github.com/google/rejoiner

开源编程语言(OpenSource Language):

Java 96.5%

开源软件介绍(OpenSource Introduction):

Rejoiner

Build Status Coverage Status

Maven Central

  • Creates a uniform GraphQL schema from microservices
  • Allows the GraphQL schema to be flexibly defined and composed as shared components
  • Generates GraphQL types from Proto definitions
  • Populates request Proto based on GraphQL query parameters
  • Supplies a DSL to modify the generated schema
  • Joins data sources by annotating methods that fetch data
  • Creates Proto FieldMasks based on GraphQL selectors

Rejoiner Overview

Experimental Features

These features are actively being developed.

  • Expose any GraphQL schema as a gRPC service.
  • Lossless end to end proto scalar types when using gRPC.
  • Relay support [Example]
  • GraphQL Stream (based on gRPC streaming) [Example]

Schema Module

SchemaModule is a Guice module that is used to generate parts of a GraphQL schema. It finds methods and fields that have Rejoiner annotations when it's instantiated. It then looks at the parameters and return type of these methods in order to generate the appropriate GraphQL schema. Examples of queries, mutations, and schema modifications are presented below.

GraphQL Query

final class TodoQuerySchemaModule extends SchemaModule {
  @Query("listTodo")
  ListenableFuture<ListTodoResponse> listTodo(ListTodoRequest request, TodoClient todoClient) {
    return todoClient.listTodo(request);
  }
}

In this example request is of type ListTodoRequest (a protobuf message), so it's used as a parameter in the generated GraphQL query. todoService isn't a protobuf message, so it's provided by the Guice injector.

This is useful for providing rpc services or database access objects for fetching data. Authentication data can also be provided here.

Common implementations for these annotated methods:

  • Make gRPC calls to microservices which can be implemented in any language
  • Load protobuf messages directly from storage
  • Perform arbitrary logic to produce the result

GraphQL Mutation

final class TodoMutationSchemaModule extends SchemaModule {
  @Mutation("createTodo")
  ListenableFuture<Todo> createTodo(
      CreateTodoRequest request, TodoService todoService, @AuthenticatedUser String email) {
    return todoService.createTodo(request, email);
  }
}

Adding edges between GraphQL types

In this example we are adding a reference to the User type on the Todo type.

final class TodoToUserSchemaModule extends SchemaModule {
  @SchemaModification(addField = "creator", onType = Todo.class)
  ListenableFuture<User> todoCreatorToUser(UserService userService, Todo todo) {
    return userService.getUserByEmail(todo.getCreatorEmail());
  }
}

In this case the Todo parameter is the parent object which can be referenced to get the creator's email.

This is how types are joined within and across APIs.

Rejoiner API Joining

Removing a field

final class TodoModificationsSchemaModule extends SchemaModule {
  @SchemaModification
  TypeModification removePrivateTodoData =
      Type.find(Todo.getDescriptor()).removeField("privateTodoData");
}

Building the GraphQL schema

import com.google.api.graphql.rejoiner.SchemaProviderModule;

public final class TodoModule extends AbstractModule {
  @Override
  protected void configure() {
    // Guice module that provides the generated GraphQLSchema instance
    install(new SchemaProviderModule());

    // Install schema modules
    install(new TodoQuerySchemaModule());
    install(new TodoMutationSchemaModule());
    install(new TodoModificationsSchemaModule());
    install(new TodoToUserSchemaModule());
  }
}

Getting started

Dependency information

Apache Maven

<dependency>
    <groupId>com.google.api.graphql</groupId>
    <artifactId>rejoiner</artifactId>
    <version>0.0.4</version>
</dependency>

Gradle/Grails compile 'com.google.api.graphql:rejoiner:0.0.4'

Scala SBT libraryDependencies += "com.google.api.graphql" % "rejoiner" % "0.0.4"

Supported return types

All generated proto messages extend Message.

  • Any subclass of Message
  • ImmutableList<? extends Message>
  • ListenableFuture<? extends Message>
  • ListenableFuture<ImmutableList<? extends Message>>

Project information

  • Rejoiner is built on top of GraphQL-Java which provides the core GraphQL capabilities such as query parsing, validation, and execution.
  • Java code is formatted using google-java-format.
  • Note: This is not an official Google product.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap