graphrpc

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: MIT Imports: 12 Imported by: 0

README

GraphRPC

GraphQL over gRPC.

GraphQL has made query and mutating data easier for front-end. How about using GraphQL to communicate between microservices? And lately, gRPC has been choosen over REST to communicate between microservices in backend because of the speed.

Here, GraphRPC made the transport of the GraphQL query to gRPC for easy communicate between microservices without neglecting the speed

Note: GraphRPC intended to extend graph-gophers handler over gRPC. GraphRPC need GraphQL resolver implementation from it.

Usages

Complete example see examples folder

Get Started
go get -u github.com/aeramu/graphrpc
Server Example
package main

import (
    "net"

    "github.com/aeramu/graphrpc"
    "github.com/graph-gophers/graphql-go"
)

func main() {
    schema := graphql.MustParseSchema(schemaString, &resolver{})
    server := graphrpc.NewGraphRPCServer(schema)

    listener, _ := net.Listen("tcp", ":8000")
    server.Serve(listener)
}

Client Example
package main

import (
    "context"
    "log"

    "github.com/aeramu/graphrpc"
    "google.golang.org/grpc"
)

func main() {
    conn, _ := grpc.Dial("localhost:8000", grpc.WithInsecure())
    client := graphrpc.NewGraphRPCClient(conn)

    res, err := client.
    	WithQuery(
    	`query($id: ID!){
            getFilm(id: $id) {
                id
                title
                synopsis
                rating
            }
        }`).
        ExecWithVariables(context.Background(), map[string]interface{}{
            "id": "e7098dc0-b407-41e2-9a60-aca4c9638bea",
        })  
    if err != nil {
    	log.Println(err)
    	return
    }
    
    var data Data
    if err := res.Consume(&data); err != nil {
    	log.Println(err)
    	return
    }
    fmt.Println(data)  	
}

TODO

  • Testing
  • Is API good enough? (Exec Request Builder)
  • Is it concurrency safe?
  • Add support to different GraphQL library(?) (or maybe make another own)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGraphRPCServer

func NewGraphRPCServer(schema *graphql.Schema) *grpc.Server

func ParseSchemaFromFile

func ParseSchemaFromFile(dir string, resolver interface{}) (*graphql.Schema, error)

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewGraphRPCClient

func NewGraphRPCClient(cc grpc.ClientConnInterface) *Client

func (*Client) Exec

func (c *Client) Exec(ctx context.Context, req Request) (*Response, error)

func (*Client) ExecQuery

func (c *Client) ExecQuery(ctx context.Context, query string) (*Response, error)

func (*Client) WithOperationName

func (c *Client) WithOperationName(operationName string) Request

func (*Client) WithQuery

func (c *Client) WithQuery(query string) Request

func (*Client) WithVariables

func (c *Client) WithVariables(variables map[string]interface{}) Request

type Request

type Request struct {
	Client        *Client
	Query         string
	OperationName string
	Variables     map[string]interface{}
}

func (Request) Exec

func (r Request) Exec(ctx context.Context) (*Response, error)

func (Request) ExecQuery

func (r Request) ExecQuery(ctx context.Context, query string) (*Response, error)

func (Request) ExecWithVariables

func (r Request) ExecWithVariables(ctx context.Context, variables map[string]interface{}) (*Response, error)

func (Request) WithOperationName

func (r Request) WithOperationName(operationName string) Request

func (Request) WithQuery

func (r Request) WithQuery(query string) Request

func (Request) WithVariables

func (r Request) WithVariables(variables map[string]interface{}) Request

type Response

type Response struct {
	Data   []byte
	Errors []*proto.Error
}

func (Response) Consume

func (r Response) Consume(v interface{}) error

type Server

type Server struct {
	Schema *graphql.Schema
}

func (*Server) Exec

func (h *Server) Exec(ctx context.Context, request *proto.ExecRequest) (*proto.ExecResponse, error)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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