gqlgen golang graphql server 基本试用

rongfengliang · · 2290 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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 info
type User struct {
    ID string
    Name string
}
  • Todo (model/todo.go)
package model

// Todo todo ino
type Todo struct {
    ID string
    Text string
    Done bool
    UserID string
}

生成graphql 运行时代码

  • 配置模型关联

    gqlgen.yml

schema: schema.graphql
exec:
  filename: graph/generated.go
model:
  filename: model/generated.go

models:
  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 graph

import (
    "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 main

import (
    "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


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:rongfengliang

查看原文:gqlgen golang graphql server 基本试用

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2290 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传