All Projects → ysugimoto → grpc-graphql-gateway

ysugimoto / grpc-graphql-gateway

Licence: MIT license
A protoc plugin that generates graphql execution code from Protocol Buffers.

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects
Makefile
30231 projects

Projects that are alternatives of or similar to grpc-graphql-gateway

Protoc Gen Struct Transformer
Transformation functions generator for Protocol Buffers.
Stars: ✭ 105 (-56.07%)
Mutual labels:  protocol-buffers, grpc
Go Micro Boilerplate
The boilerplate of the GoLang application with a clear microservices architecture.
Stars: ✭ 147 (-38.49%)
Mutual labels:  protocol-buffers, grpc
Protodot
transforming your .proto files into .dot files (and .svg, .png if you happen to have graphviz installed)
Stars: ✭ 107 (-55.23%)
Mutual labels:  protocol-buffers, grpc
Ts Protoc Gen
Protocol Buffers Compiler (protoc) plugin for TypeScript and gRPC-Web.
Stars: ✭ 913 (+282.01%)
Mutual labels:  protocol-buffers, grpc
Go Grpc Examples
This repo contains examples and implementations of different types of GRPC services and APIs using Golang.
Stars: ✭ 180 (-24.69%)
Mutual labels:  protocol-buffers, grpc
Google Assistant Java Demo
A simple Google Assistant Client in Java
Stars: ✭ 53 (-77.82%)
Mutual labels:  protocol-buffers, grpc
Protoeasy Go
Simpler usage of protoc. Deprecated.
Stars: ✭ 129 (-46.03%)
Mutual labels:  protocol-buffers, grpc
Protobuf
[Looking for new ownership] Protocol Buffers for Go with Gadgets
Stars: ✭ 4,998 (+1991.21%)
Mutual labels:  protocol-buffers, grpc
Evans
Evans: more expressive universal gRPC client
Stars: ✭ 2,710 (+1033.89%)
Mutual labels:  protocol-buffers, grpc
Buf
A new way of working with Protocol Buffers.
Stars: ✭ 3,328 (+1292.47%)
Mutual labels:  protocol-buffers, grpc
Protoreflect
Reflection (Rich Descriptors) for Go Protocol Buffers
Stars: ✭ 651 (+172.38%)
Mutual labels:  protocol-buffers, grpc
Mu Haskell
Mu (μ) is a purely functional framework for building micro services.
Stars: ✭ 215 (-10.04%)
Mutual labels:  protocol-buffers, grpc
Startup Os
Working examples of Google's Open Source stack and deployment to the cloud.
Stars: ✭ 564 (+135.98%)
Mutual labels:  protocol-buffers, grpc
Grpc Swift
The Swift language implementation of gRPC.
Stars: ✭ 1,270 (+431.38%)
Mutual labels:  protocol-buffers, grpc
Prototool
Your Swiss Army Knife for Protocol Buffers
Stars: ✭ 4,932 (+1963.6%)
Mutual labels:  protocol-buffers, grpc
Go Proto Gql
Protobuff plugins for generating graphql schema and golang to graphql bindings. Also supports a graphql gateway (Alpha)
Stars: ✭ 127 (-46.86%)
Mutual labels:  protocol-buffers, grpc
Kroto Plus
gRPC Kotlin Coroutines, Protobuf DSL, Scripting for Protoc
Stars: ✭ 400 (+67.36%)
Mutual labels:  protocol-buffers, grpc
Awesome Grpc
A curated list of useful resources for gRPC
Stars: ✭ 4,932 (+1963.6%)
Mutual labels:  protocol-buffers, grpc
Protodep
Collect necessary .proto files (Protocol Buffers IDL) and manage dependencies
Stars: ✭ 167 (-30.13%)
Mutual labels:  protocol-buffers, grpc
Rules protobuf
Bazel rules for building protocol buffers and gRPC services (java, c++, go, ...)
Stars: ✭ 206 (-13.81%)
Mutual labels:  protocol-buffers, grpc

grpc-graphql-gateway

CircleCI

grpc-graphql-gateway is a protoc plugin that generates graphql execution code from Protocol Buffers.

image

Motivation

On API development, frequently we choose some IDL, in order to manage API definitions from a file. Considering two of IDL -- GraphQL and Protocol Buffers (for gRPC) -- these have positive point respectively:

  • GraphQL -- Can put together multiple resources getting into one HTTP request, appropriate for BFF
  • gRPC -- Easy syntax in Protocol Buffers, and easy to implement API server using HTTP/2

But sometimes it's hard to maintain both GraphQL and Protocol Buffers, so we created this plugin in order to generate GraphQL Schema from Protocol Buffers.

This project much refers to grpc-gateway how to generate a file, provide a plugin. many thanks!

Installation

Get plugin binary

Get protoc-gen-graphql binary from releases page and set $PATH to be executable.

Or, simply get the latest one:

go get github.com/ysugimoto/grpc-graphql-gateway/protoc-gen-graphql/...

Then the binary will place in $GOBIN.

Get protobuf file

Get include/graphql.proto from this repository and put it into your project under the protobuf files.

git submodule add https://github.com/ysugimoto/grpc-graphql-gateway.git grpc-graphql-gateway
## Or another way...

Usage

Please replace [your/project] section to your appropriate project.

Write Protocol Buffers

Declare gRPC service with protobuf using grpc-graphql-gateway options. This example has two RPCs that names SayHello and SayGoodbye:

// greeter.proto
syntax = "proto3";

import "graphql.proto";

service Greeter {
  // gRPC service information
  option (graphql.service) = {
    host: "localhost:50051"
    insecure: true
  };

  rpc SayHello (HelloRequest) returns (HelloReply) {
    // Here is plugin definition
    option (graphql.schema) = {
      type: QUERY   // declare as Query
      name: "hello" // query name
    };
  }

  rpc SayGoodbye (GoodbyeRequest) returns (GoodbyeReply) {
    // Here is plugin definition
    option (graphql.schema) = {
      type: QUERY     // declare as Query
      name: "goodbye" // query name
    };
  }
}

message HelloRequest {
  // Below line means the "name" field is required in GraphQL argument
  string name = 1 [(graphql.field) = {required: true}];
}

message HelloReply {
  string message = 1;
}

message GoodbyeRequest {
  // Below line means the "name" field is required in GraphQL argument
  string name = 1 [(graphql.field) = {required: true}];
}

message GoodbyeReply {
  string message = 1;
}

Compile to Go code

Compile protobuf file with the plugin:

protoc \
  -I. \
  --go_out=./greeter \
  --go-grpc_out=./greeter \
  --graphql_out=./greeter \
  greeter.proto

Then you can see greeter/greeter.pb.go and greeter/greeter.graphql.go.

Implement service

For example, gRPC service will be:

// service/main.go
package main

import (
    "context"
    "fmt"
    "net"
    "log"

    "github.com/[your/project]/greeter"
    "google.golang.org/grpc"
)

type Server struct{}

func (s *Server) SayHello(ctx context.Context, req *greeter.HelloRequest) (*greeter.HelloReply, error) {
	return &greeter.HelloReply{
		Message: fmt.Sprintf("Hello, %s!", req.GetName()),
	}, nil
}

func (s *Server) SayGoodbye(ctx context.Context, req *greeter.GoodbyeRequest) (*greeter.GoodbyeReply, error) {
	return &greeter.GoodbyeReply{
		Message: fmt.Sprintf("Good-bye, %s!", req.GetName()),
	}, nil
}

func main() {
	conn, err := net.Listen("tcp", ":50051")
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close()

	server := grpc.NewServer()
	greeter.RegisterGreeterServer(server, &Server{})
	server.Serve(conn)
}

Then let's start service:

go run service/main.go

The gRPC service will start on localhost:50051.

Next, GraphQL gateway service should be:

// gateway/main.go
package main

import (
    "log"

    "net/http"

    "github.com/[your/project]/greeter"
    "github.com/ysugimoto/grpc-graphql-gateway/runtime"
)

func main() {
    mux := runtime.NewServeMux()

    if err := greeter.RegisterGreeterGraphql(mux); err != nil {
        log.Fatalln(err)
    }
    http.Handle("/graphql", mux)
    log.Fatalln(http.ListenAndServe(":8888", nil))
}

Then let's start gateway:

go run gateway/main.go

The GraphQL gateway will start on localhost:8888

Send request via the gateway

Now you can access gRPC service via GraphQL gateway!

curl -g "http://localhost:8888/graphql" -d '
{
  hello(name: "GraphQL Gateway") {
    message
  }
}'
#=> {"data":{"hello":{"message":"Hello, GraphQL Gateway!"}}}

You can also send request via POST method with operation name like:

curl -XPOST "http://localhost:8888/graphql" -d '
query greeting($name: String = "GraphQL Gateway") {
  hello(name: $name) {
    message
  }
  goodbye(name: $name) {
    message
  }
}'
#=> {"data":{"goodbye":{"message":"Good-bye, GraphQL Gateway!"},"hello":{"message":"Hello, GraphQL Gateway!"}}}

This is the most simplest way :-)

Resources

To learn more, please see the following resources:

  • graphql.proto Plugin option definition. See a comment section for custom usage (e.g mutation).
  • example/greeter Files of above usage.
  • example/starwars Common implementation for GraphQL explanation, the StarWars API example

This plugin generates graphql execution code using graphql-go/graphql, see that repository in detail.

Limitations

This plugin just aims to generate a simple gateway of gRPC.

Some of things could be solved and could not be solved. The most of limitations come from the IDL's power of expression -- some kind of GraphQL schema feature cannot implement by Protocol Buffers X(

Currently we don't support some Protobuf types:

  • Builtin oneof type

Contribute

  • Fork this repository
  • Customize / Fix problem
  • Send PR :-)
  • Or feel free to create issue for us. We'll look into it

Author

Yoshiaki Sugimoto

License

MIT

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