Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go_GraphQL_in_LINE_SPOT.pdf

 Go_GraphQL_in_LINE_SPOT.pdf

Denny Tsai (LINE Taiwan)

Golang Taipei Gathering #40
https://www.meetup.com/golang-taipei-meetup/events/260166791/

LINE Developers

April 23, 2019
Tweet

More Decks by LINE Developers

Other Decks in Technology

Transcript

  1. https://api.github.com/search/repositories?q=language:go&sort=stars&order=desc { "items": [ { "id": 23096959, "node_id": "MDEwOlJlcG9zaXRvcnkyMzA5Njk1OQ==", "name":

    "go", "full_name": "golang/go", "owner": { "login": "golang" }, "url": "https://api.github.com/repos/golang/go", "description": "The Go programming language", "issues_url": "https://api.github.com/repos/golang/go/issues{/number}", "stargazers_count": 56341 } ] }
  2. https://api.github.com/repos/golang/go/issues [ { "url": "https://api.github.com/repos/golang/go/issues/31459", "number": 31459, "title": "cmd/link: Apple's

    symbols tool unable to read DWARF data from c-archive go.o", "user": { "login": "tmm1", "avatar_url": "https://avatars2.githubusercontent.com/u/2567?v=4", }, "state": "open" }, { "url": "https://api.github.com/repos/golang/go/issues/31458", "number": 31458, "title": "cmd/go: mod meta tag causes infinite loop in GOPROXY", "user": { "login": "marwan-at-work", "avatar_url": "https://avatars0.githubusercontent.com/u/16294261?v=4", }, "state": "open" } ]
  3. { repository( owner: "golang" name: "go" ) { name issues(

    first: 2 orderBy: { field: CREATED_AT direction: DESC } ) { totalCount nodes { number title } } } } { "data": { "repository": { "name": "go", "issues": { "totalCount": 30761, "nodes": [ { "number": 31459, "title": "cmd/link: Apple's symbols tool ..." }, { "number": 31458, "title": "cmd/go: mod meta tag causes ..." } ] } } } }
  4. { repository( owner: "golang" name: "go" ) { name issues(

    first: 2 orderBy: { field: CREATED_AT direction: DESC } ) { totalCount nodes { number title } } } } { "data": { "repository": { "name": "go", "issues": { "totalCount": 30761, "nodes": [ { "number": 31459, "title": "cmd/link: Apple's symbols tool ..." }, { "number": 31458, "title": "cmd/go: mod meta tag causes ..." } ] } } } }
  5. query { repository( owner: String! name: String! ): Repository }

    type Repository { name: String! issues( orderBy: IssueOrder first: Int ): IssueConnection! } type IssueConnection { totalCount: Int! nodes: [Issue] } type Issue { number: Int! title: String! } { repository( owner: "golang" name: "go" ) { name issues( first: 2 orderBy: { field: CREATED_AT direction: DESC } ) { totalCount nodes { number title } } } } type IssueOrder { field: IssueOrderField! direction: OrderDirection! } enum IssueOrderField { CREATED_AT UPDATED_AT COMMENTS } enum OrderDirection { ASC DESC }
  6. mutation { createIssue( input: CreateIssueInput! ): CreateIssuePayload } type CreateIssuePayload

    { clientMutationId: String issue: Issue } input CreateIssueInput { repositoryId: ID! title: String! body: String assigneeIds: [ID!] milestoneId: ID labelIds: [ID!] projectIds: [ID!] clientMutationId: String } mutation { createIssue( input: { repositoryId: "..." title: "New issue" body: "This is a new issue" } ) { clientMutationId issue { number title } } }
  7. Create a schema type Query { posts: [Post] post(id: Int!):

    Post } type Post { id: Int! title: String! body: String! }
  8. Initialize with gqlgen ➜ apigateway go run github.com/99designs/gqlgen init Exec

    "go run ./server/server.go" to start GraphQL server ➜ apigateway tree . ├── generated.go ├── go.mod ├── go.sum ├── gqlgen.yml ├── models_gen.go ├── resolver.go ├── schema.graphql └── server └── server.go
  9. Resolvers type Resolver struct{} func (r *Resolver) Query() QueryResolver {

    return &queryResolver{r} } type queryResolver struct{ *Resolver } func (r *queryResolver) Posts(ctx context.Context) ([]*Post, error) { panic("not implemented") } func (r *queryResolver) Post(ctx context.Context, id int) (*Post, error) { panic("not implemented") }
  10. Generated models type Post struct { ID int `json:"id"` Title

    string `json:"title"` Body string `json:"body"` }
  11. Configuration schema: - schema.graphql exec: filename: generated.go model: filename: models_gen.go

    resolver: filename: resolver.go type: Resolver models: Post: model: apigateway.Post
  12. Generated server const defaultPort = "8080" func main() { port

    := os.Getenv("PORT") if port == "" { port = defaultPort } http.Handle("/", handler.Playground("GraphQL playground", "/query")) http.Handle("/query", handler.GraphQL(apigateway.NewExecutableSchema(...))) log.Printf("connect to http://localhost:%s/ for GraphQL playground", port) log.Fatal(http.ListenAndServe(":"+port, nil)) }
  13. Create a GraphQL schema Initialize with gqlgen Implement resolvers Update

    GraphQL schema Generate new code with gqlgen Implement new resolvers