Skip to content

danny-yamamoto/go-graphql-federation-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-graphql-federation-example

Implement GraphQL Federation.

flowchart LR
    Gateway["Gateway"]
    Subgraph1["Subgraph1
    users"]
    Subgraph2["Subgraph2
    contact"]
    Client -->|8082| Gateway -->|4000| Subgraph1
    Gateway -->|4001| Subgraph2

Installation

go install github.com/99designs/gqlgen@latest
go get github.com/99designs/gqlgen/graphql/handler

Add Subgraph

Add Service

  • schema.graphqls
type Service {
  name: String!
  version: String!
  schema: String!
}

Generate

gqlgen generate

Add resolver

Running Subgraph1

vscode ➜ /workspaces/go-graphql-federation-example/users (main) $ go run server.go 
2023/07/25 04:31:12 connect to http://localhost:4000/ for GraphQL playground
vscode ➜ /workspaces/go-graphql-federation-example (main) $ curl -X POST -H "Content-Type: Application/json" -d '{"query":"{ todos { id } }"}' http://localhost:4000/query
{"data":{"todos":[{"id":"TODO-1"},{"id":"TODO-2"}]}}
vscode ➜ /workspaces/go-graphql-federation-example (main) $ 

Running Gateway

go install github.com/movio/bramble/cmd/bramble@latest
touch config.json
cd cmd/bramble/
go run main.go -conf ./config.json 

Querying Gateway

users | id only

vscode ➜ /workspaces/go-graphql-federation-example (main) $ curl -X POST -H "Content-Type: Application/json" -d '{"query":"{ todos { id } }"}' http://localhost:8082/query
{"data":{"todos":[{"id":"TODO-1"},{"id":"TODO-2"}]}}
vscode ➜ /workspaces/go-graphql-federation-example (main) $ 

users | id and text

vscode ➜ /workspaces/go-graphql-federation-example (main) $ curl -X POST -H "Content-Type: Application/json" -d '{"query":"{ todos { id text } }"}' http://localhost:8082/query
{"data":{"todos":[{"id":"TODO-1","text":"My Todo 1"},{"id":"TODO-2","text":"My Todo 2"}]}}vscode ➜ /workspaces/go-graphql-federation-example (main) $

contact

root ➜ /workspaces/go-graphql-federation-example/contact (main) $ curl -X POST -H "Content-Type: Application/json" -d '{"query":"{ contacts { firstname } }"}' http://localhost:8082/query
{"data":{"contacts":[{"firstname":"mirai"}]}}
root ➜ /workspaces/go-graphql-federation-example/contact (main) $ 

Implement your own schema

Create directory

mkdir -p contact
go mod init github.com/danny-yamamoto/go-graphql-federation-example/contact
go get -u github.com/99designs/gqlgen
go install github.com/99designs/gqlgen@latest
go get github.com/99designs/gqlgen/graphql/handler

Add a package directory to go.work

Create a schema file

touch schema.graphqls
touch gqlgen.yml
gqlgen generate

Reference

About

Implement GraphQL Federation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published