graphql-gateway

module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2020 License: MIT

README

graphql-gateway

This project is currently in WIP.

Installation

go get -u github.com/grpc-custom/graphql-gateway/cmd/protoc-gen-graphql-gateway

Usage

  1. Define your gRPC service using protocol buffers

    example.proto

    syntax = "proto3";
    package example;
    
    import "github.com/grpc-custom/graphql-gateway/graphql.proto";
    
    message User {
      int32 id    = 1;
      string name = 2;
    }
    
    message GetRequest {
      int32 id = 1;
    }
    
    message GetResponse {
      User user = 1;
    }
    
    service UserService {
      rpc Get(GetRequest) returns (GetResponse) {
        option (grpc_custom.graphql.schema) = {
          query: "getUser"
        };
      }
    }
    
  2. Generate GraphQL server using protoc-gen-graphql-gateway

    protoc -I=${GOPATH}/src:. \
        --go_out=plugins=grpc:. \
        --graphql-gateway_out=. \
        path/to/example.proto
    

    It will generate a GraphQL server path/to/example.pb.gql.go

  3. Write an entrypoint for the GraphQL server

    package main
    
    import (
        "context"
        "log"
        "net/http"
    
        "github.com/grpc-custom/graphql-gateway/runtime"
        "google.golang.org/grpc"
    
        gql "path/to/example_service_package"
    )
    
    func main() {
        ctx := context.Background()
        ctx, cancel := context.WithCancel(ctx)
        defer cancel()
    
        mux, err := runtime.NewServeMux()
        if err != nil {
            log.Fatal(err)
        }
        // Register gRPC server endpoint
        opts := []grpc.DialOption{grpc.WithInsecure()}
        host := "localhost:9090" // gRPC server endpoint
        err = gql.RegisterUserServiceFromEndpoint(ctx, mux, host, opts)
        if err != nil {
            log.Fatal(err)
        }
        // Start GraphQL server
        err = http.ListenAndServe(":8080", mux)
        if err != nil {
            log.Fatal(err)
        }
    }
    

Examples

examples

TODO

  • Apollo Persisted Queries
  • complexity
  • data loader
  • custom error handling
  • subscribe
    • websocket
  • enum (string)
  • message pack
  • cache control
    • redis
    • memcache
  • graphql schema
  • open trace

Directories

Path Synopsis
cmd
example
federation/proto/account
Package account is a reverse proxy.
Package account is a reverse proxy.
federation/proto/product
Package product is a reverse proxy.
Package product is a reverse proxy.
federation/proto/review
Package review is a reverse proxy.
Package review is a reverse proxy.
photo_share/proto/photo
Package photo is a reverse proxy.
Package photo is a reverse proxy.
photo_share/proto/user
Package user is a reverse proxy.
Package user is a reverse proxy.
sample/proto/green
Package green is a reverse proxy.
Package green is a reverse proxy.
sample/proto/red
Package red is a reverse proxy.
Package red is a reverse proxy.
pkg
template
Code generated by pkg/template/gen.go DO NOT EDIT.
Code generated by pkg/template/gen.go DO NOT EDIT.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL