golang+gin框架使用graphql-go

高吉星
2023-12-01

golang+gin框架使用graphql-go

1、安装组件

	go get github.com/graphql-go/graphql
	go get github.com/graphql-go/handler

2、router(路由)里初始化

func init(){
	AdminApiRouterGroup = Router.Group("/api/v1")
	{
		// GET方法用于支持GraphQL的web界面操作
		// 如果不需要web界面,可根据自己需求用GET/POST或者其他都可以
		AdminApiRouterGroup.POST("/graphql", GraphqlHandler())
		AdminApiRouterGroup.GET("/graphql", GraphqlHandler())
	}
}

3、GraphqlHandler() 文件,新建一个router_graphql.go文件与router文件同目录,内容:

package router

import (
	"c2matica.com/productcenter/graphql/schemas"
	"github.com/gin-gonic/gin"
	"github.com/graphql-go/graphql"
	"github.com/graphql-go/handler"
)

// 定义根查询节点
var query = graphql.NewObject(graphql.ObjectConfig{
	Name:        "query",
	Description: "query",
	Fields: graphql.Fields{
	//schemas文件夹名称,放graphql接收和返回字段
		"getList":           &schemas.GetList,
	},
})

// 定义根增删改节点
var mutation = graphql.NewObject(graphql.ObjectConfig{
	Name:        "mutation",
	Description: "mutation",
	Fields: graphql.Fields{
		"addProducts":              &schemas.AddProducts,
	},
})

// 定义Schema用于http handler处理
var Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
	Query:    query,
	Mutation: mutation, // 需要通过GraphQL更新数据,可以定义Mutation
})

// 将graphql中的schema 放入gin的路由中
func GraphqlHandler() gin.HandlerFunc {
	h := handler.New(&handler.Config{
		Schema:   &Schema,
		Pretty:   true,
		GraphiQL: true,
	})

	// 只需要通过Gin简单封装即可
	return func(c *gin.Context) {
		h.ContextHandler(c, c.Writer, c.Request)
	}
}

4、在schemas创建product.go文件,写入接收参数和返回参数

package schemas

import (
	//resolves是具体要处理逻辑的方法所在的文件夹
	"c2matica.com/productcenter/graphql/resolves"
	//types是返回值封装到的文件夹
	"c2matica.com/productcenter/graphql/types"
	"github.com/graphql-go/graphql"
)
var GetBomList = graphql.Field{
	Name:        "GetList",
	Description: "GetList",
	//GetListOutputType在types文件夹里,也可以放到当前文件里
	Type:        types.GetListOutputType,
	// Args是定义在GraphQL查询中支持的查询字段,
	// 可自行随意定义,如加上limit,start这类
	Args: graphql.FieldConfigArgument{
		"name": &graphql.ArgumentConfig{
			Type: graphql.String,
		},
		"after": &graphql.ArgumentConfig{
			Type: graphql.Int,
		},
		"first": &graphql.ArgumentConfig{
			Type: graphql.Int,
		},
	},
	// Resolve是一个处理请求的函数,具体处理逻辑可在此进行
	Resolve: resolves.GetListResolve,
}

5、GetListOutputType里的内容

package types

import (
	"github.com/graphql-go/graphql"
)

// 定义查询对象的字段,支持嵌套
var GetListOutputType= graphql.NewObject(graphql.ObjectConfig{
	Name:        "GetListOutputType",
	Description: "GetListOutputType Model",
	Fields: graphql.Fields{
		"code": &graphql.Field{
			Type: graphql.String,
		},
		"message": &graphql.Field{
			Type: graphql.String,
		},
		"success": &graphql.Field{
			Type: graphql.Boolean,
		},
		"output": &graphql.Field{
		//InfoOutputType代表output是一维数组字段
			Type: InfoOutputType,
		},
		//graphql.NewList(ListNodeType)代表edges是二维数组字段
		"edges": &graphql.Field{
			Type: graphql.NewList(ListNodeType),
		},
		"totalCount": &graphql.Field{
			Type: graphql.Int,
		},
	},
})

var InfoOutputType= graphql.NewObject(graphql.ObjectConfig{
	Name:        "InfoOutputType",
	Description: "InfoOutputType Model",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.Int,
		},
	},
})

var ListNodeType = graphql.NewObject(graphql.ObjectConfig{
	Name:        "ListNodeType ",
	Description: "ListNodeType Model",
	Fields: graphql.Fields{
		"node": &graphql.Field{
			Type: DetailType,
		},
	},
})

var DetailType= graphql.NewObject(graphql.ObjectConfig{
	Name:        "DetailType",
	Description: "DetailType Model",
	Fields: graphql.Fields{
		"id": &graphql.Field{
			Type: graphql.String,
		},
	},
})

6、在resolve文件夹里创建go文件,实现GetListResolve方法

//p graphql.ResolveParams规定的接收方法,(data interface{}, err error)规定的返回方法,p和data和err可以改名字
func GetListResolve(p graphql.ResolveParams) (data interface{}, err error){
	//处理逻辑
	return
}
 类似资料: