gqlgen golang graphql server 基本試用

來源:互聯網
上載者:User

gqlgen golang 的graphql server

具體代碼參考https://github.com/rongfengliang/gqlgen-demo

特點

  • 模型優先
  • 型別安全
  • 代碼產生

安裝

go get -u github.com/vektah/gqlgen

建立schema

schema.graphql

type Todo {id: ID!text: String!done: Boolean!user: User!}type User {id: ID!name: String!}type Query {todos: [Todo!]!}input NewTodo {text: String!userId: String!}type Mutation {createTodo(input: NewTodo!): Todo!}

建立model

  • User (model/user.go)
package model// User user infotype User struct {    ID string    Name string}
  • Todo (model/todo.go)
package model// Todo todo inotype Todo struct {    ID string    Text string    Done bool    UserID string}

產生graphql 運行時代碼

  • 配置模型關聯

    gqlgen.yml

schema: schema.graphqlexec:  filename: graph/generated.gomodel:  filename: model/generated.gomodels:  Todo:    model: github.com/rongfengliang/gqlgen-app/model.Todo  User:    model: github.com/rongfengliang/gqlgen-app/model.User
  • 產生bingings
gqlgen

  • 編寫 resolver

    graph/graph.go

package graphimport (    "context"    "fmt"    "math/rand"    model "github.com/rongfengliang/gqlgen-app/model")type App struct {    todos []model.Todo}func (a *App) Mutation() MutationResolver {    return &mutationResolver{a}}func (a *App) Query() QueryResolver {    return &queryResolver{a}}func (a *App) Todo() TodoResolver {    return &todoResolver{a}}type queryResolver struct{ *App }func (a *queryResolver) Todos(ctx context.Context) ([]model.Todo, error) {    return a.todos, nil}type mutationResolver struct{ *App }func (a *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (model.Todo, error) {    todo := model.Todo{        Text: input.Text,        ID: fmt.Sprintf("T%d", rand.Int()),        UserID: input.UserId,    }    a.todos = append(a.todos, todo)    return todo, nil}type todoResolver struct{ *App }func (a *todoResolver) User(ctx context.Context, it *model.Todo) (model.User, error) {    return model.User{ID: it.UserID, Name: "user " + it.UserID}, nil}

入口代碼編寫

  • main.go
package mainimport (    "fmt"    "log"    "net/http"    "github.com/rongfengliang/gqlgen-app/graph"    "github.com/vektah/gqlgen/handler")func main() {    http.Handle("/", handler.Playground("Todo", "/query"))    http.Handle("/query", handler.GraphQL(graph.NewExecutableSchema(&graph.App{})))    fmt.Println("Listening on :8080")    log.Fatal(http.ListenAndServe(":8080", nil))}

運行

go run main.go

效果


整合dep

dep init 

參考資料

https://gqlgen.com/getting-started/
https://github.com/vektah/gqlgen
https://github.com/rongfengliang/gqlgen-demo

    
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.