gRPC 프로젝트 디렉토리를 만들고 Go 모듈로 초기화 합니다.
$ mkdir grpc
$ cd grpc
GraphQL Mesh 예제에서 authors_service.proto 파일을 가져옵니다.
$ mkdir -p proto/v1
$ cd proto/v1
Go 모듈로 사용하기 위해 option go_package = "github.com/songtomtom/graphql-mesh-gateway/grpc/proto/v1";
을 추가 합니다.
authors_service.proto
syntax = "proto3";
option go_package = "github.com/songtomtom/graphql-mesh-gateway/grpc/proto/v1";
package authors.v1;
service AuthorsService {
rpc GetAuthor(GetAuthorRequest) returns (Author) {}
rpc ListAuthors(ListAuthorsRequest) returns (ListAuthorsResponse) {}
}
message ListAuthorsRequest {}
message GetAuthorRequest {
string id = 1;
}
message ListAuthorsResponse {
repeated Author items = 1;
}
message Author {
string id = 1;
string name = 2;
string editor = 3;
}
authors_service.proto
를 컴파일합니다. 컴파일이 성공하면 authors_service.go
, authors_service.pb.go
파일이 생성됩니다.
gRPC 서버 만들기는 gRPC 서버, 클라이언트 만들기 참고 합니다.
/grpc/server.go
package main
import (
"context"
v1 "github.com/songtomtom/graphql-mesh-gateway/grpc/proto/v1"
"google.golang.org/grpc"
"log"
"net"
)
type server struct {
v1.UnimplementedAuthorsServiceServer
}
func (s *server) GetAuthor(ctx context.Context, in *v1.GetAuthorRequest) (*v1.Author, error) {
return &v1.Author{
Id: in.GetId(),
Name: "dummy_name",
Editor: "dummy_editor",
}, nil
}
func (s *server) ListAuthors(ctx context.Context, in *v1.ListAuthorsRequest) (*v1.ListAuthorsResponse, error) {
return &v1.ListAuthorsResponse{Items: []*v1.Author{}}, nil
}
func main() {
lis, err := net.Listen("tcp", ":3003")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
v1.RegisterAuthorsServiceServer(s, &server{})
log.Printf("server listening at %v", lis.Addr())
if err = s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
gRPC 서버를 실행합니다.
$ go run server.go
server listening at [::]:3003
gRPC 를 Mesh Gateway 에 연결 하려면 @graphql-mesh/grpc
를 설치 해야 합니다.
yarn add @graphql-mesh/grpc
.meshrc.yaml
에 gRPC 를 연결할 protobuf 경로를 추가합니다.
.meshrc.yaml
sources:
- name: Books
handler:
openapi:
endpoint: http://localhost:3002/
source: ../rest_api/openapi3-definition.json
- name: Authors
handler:
grpc:
endpoint: http://localhost:3003/
source: ../grpc/proto/v1/authors_service.proto
# - name: Stores
# handler:
# graphql:
# endpoint: http://localhost:3004/
serve:
hostname: 0.0.0.0
port: 80
endpoint: /
browser: false
playground: true
GraphQL 프로젝트 디렉토리를 만들고 Go 모듈로 초기화 합니다.
$ mkdir graphql
$ cd graphql
GraphQL Mesh 예제에서 schema.graphql 파일을 가져옵니다.
schema.graphql
type Store {
id: ID!
name: String!
location: String!
}
type Sells {
bookId: ID!
sellsCount: Int!
monthYear: String
storeId: ID!
}
type Query {
stores: [Store!]!
bookSells(storeId: ID!): [Sells!]!
}
gqlgen 을 사용하여 GraphQL 서버를 만듭니다.
server.go
package main
import (
"log"
"net/http"
"os"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/songtomtom/graphql-mesh-gateway/graphql/graph"
)
const defaultPort = "3004"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
$ go run server.go
connect to http://localhost:3004/ for GraphQL playground
GraphQL 를 Mesh Gateway 에 연결 하려면 @graphql-mesh/graphql
를 설치 해야 합니다.
yarn add @graphql-mesh/graphql
.meshrc.yaml
sources:
- name: Books
handler:
openapi:
endpoint: http://localhost:3002/
source: ../rest_api/openapi3-definition.json
- name: Authors
handler:
grpc:
endpoint: http://localhost:3003/
source: ../grpc/proto/v1/authors_service.proto
- name: Stores
handler:
graphql:
endpoint: http://localhost:3004/
serve:
hostname: 0.0.0.0
port: 80
endpoint: /
browser: false
playground: true
Rest API, gRPC, GrpahQL 서버를 실행하고 Mesh Gateway 를 시작합니다.
cd mesh_gateway
$ yarn start
yarn run v1.22.18
warning package.json: No license field
$ mesh start
💡 🕸️ Mesh - Server Starting GraphQL Mesh...
💡 🕸️ Mesh - Books Processing annotations for the execution layer
💡 🕸️ Mesh - Server Serving GraphQL Mesh: http://0.0.0.0:80