Giter Site home page Giter Site logo

go-graphql-federation-example's Introduction

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

go-graphql-federation-example's People

Contributors

danny-yamamoto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.