The goal of this project is to provide full support of the GraphQL draft specification with a set of idiomatic, easy to use Go packages.
While still under heavy development (internal
APIs are almost certainly subject to change), this library issafe for production use.
context.Context
OpenTracing
standardWe're trying out the GitHub Project feature to manage graphql-go
's development roadmap.Feedback is welcome and appreciated.
package main
import (
"log"
"net/http"
graphql "github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
)
type query struct{}
func (_ *query) Hello() string { return "Hello, world!" }
func main() {
s := `
type Query {
hello: String!
}
`
schema := graphql.MustParseSchema(s, &query{})
http.Handle("/query", &relay.Handler{Schema: schema})
log.Fatal(http.ListenAndServe(":8080", nil))
}
To test:
$ curl -XPOST -d '{"query": "{ hello }"}' localhost:8080/query
A resolver must have one method or field for each field of the GraphQL type it resolves. The method or field name has to be exported and match the schema's field's name in a non-case-sensitive way.You can use struct fields as resolvers by using SchemaOpt: UseFieldResolvers()
. For example,
opts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}
schema := graphql.MustParseSchema(s, &query{}, opts...)
When using UseFieldResolvers
schema option, a struct field will be used only when:
The method has up to two arguments:
context.Context
argument.*struct { ... }
argument if the corresponding GraphQL field has arguments. The names of the struct fields have to be exported and have to match the names of the GraphQL arguments in a non-case-sensitive way.The method has up to two results:
error
result.Example for a simple resolver method:
func (r *helloWorldResolver) Hello() string {
return "Hello world!"
}
The following signature is also allowed:
func (r *helloWorldResolver) Hello(ctx context.Context) (string, error) {
return "Hello world!", nil
}
UseStringDescriptions()
enables the usage of double quoted and triple quoted. When this is not enabled, comments are parsed as descriptions instead.UseFieldResolvers()
specifies whether to use struct field resolvers.MaxDepth(n int)
specifies the maximum field nesting depth in a query. The default is 0 which disables max depth checking.MaxParallelism(n int)
specifies the maximum number of resolvers per request allowed to run in parallel. The default is 10.Tracer(tracer trace.Tracer)
is used to trace queries and fields. It defaults to trace.OpenTracingTracer
.ValidationTracer(tracer trace.ValidationTracer)
is used to trace validation errors. It defaults to trace.NoopValidationTracer
.Logger(logger log.Logger)
is used to log panics during query execution. It defaults to exec.DefaultLogger
.PanicHandler(panicHandler errors.PanicHandler)
is used to transform panics into errors during query execution. It defaults to errors.DefaultPanicHandler
.DisableIntrospection()
disables introspection queries.Errors returned by resolvers can include custom extensions by implementing the ResolverError
interface:
type ResolverError interface {
error
Extensions() map[string]interface{}
}
Example of a simple custom error:
type droidNotFoundError struct {
Code string `json:"code"`
Message string `json:"message"`
}
func (e droidNotFoundError) Error() string {
return fmt.Sprintf("error [%s]: %s", e.Code, e.Message)
}
func (e droidNotFoundError) Extensions() map[string]interface{} {
return map[string]interface{}{
"code": e.Code,
"message": e.Message,
}
}
Which could produce a GraphQL error such as:
{
"errors": [
{
"message": "error [NotFound]: This is not the droid you are looking for",
"path": [
"droid"
],
"extensions": {
"code": "NotFound",
"message": "This is not the droid you are looking for"
}
}
],
"data": null
}
我试图将一个对象作为参数传递给查询(而不是标量) . 从文档看来,这应该是可能的,但我无法弄清楚如何使其工作 . 我正在使用graphql-go,这是测试模式: var fileDocumentType = graphql.NewObject(graphql.ObjectConfig{ Name: "FileDocument", Fields: graphql.Fields{ "id": &gra
快速开始 GraphQL 是一种用于 API 的查询语言。这是 GraphQL 和 REST 之间一个很好的比较 (译者注: GraphQL 替代 REST 是必然趋势)。在这组文章中, 我们不会解释什幺是 GraphQL, 而是演示如何使用 @nestjs/GraphQL 模块。 GraphQLModule 只不过是 Apollo 服务器的包装器。我们没有造轮子, 而是提供一个现成的模块, 这让
GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。 向你的 API 发出一个 GraphQL 请求就能准确获得你想要的数据,不多不少。 GraphQL 查询总是返回可预测
Graphql editor 是一款 Graphql 的可视化编辑器和 IDE,帮助用户更容易理解 GraphQL 模式,通过使用可视化块系统创建模式。GraphQL Editor 将把它们转化为代码。通过 GraphQL Editor,用户可以在不写任何代码的情况下创建可视化的图表,或者以一种很好的方式呈现其模式。 GraphQL View Code Editor View Hierarchy View
GraphQL CLI Help us to improve new GraphQL CLI. Check out the new structure and commands below!Feel free to contact us in Discord channel. We would love to hear your feedback. Features Helpful command
Fullstack GraphQL Simple Demo Application API built with Node + Express + GraphQL + Sequelize (supports MySQL, Postgres, Sqlite and MSSQL). WebApp built with React + Redux. Written in ES6 using Babel
Hasura GraphQL Engine Hasura is an open source product that accelerates API development by 10x by giving you GraphQL or REST APIs with built in authorization on your data, instantly. Read more at hasu
这是利用Koa + GraphQL + Apollo-Server实现的,学生信息增、删、改、查的栗子 本栗子将搭配Koa实现一个GraphQL查询,逐步从简单Kao服务、到Mongodb的数据插入查询、再到GraphQL的使用,让大家快速看到: 搭建Koa搭建一个后台项目 后台路由简单处理方式 利用Mongoose简单操作Mongodb的增、删、改、查 掌握Apollo-Server简单操作数据
GraphQL Ruby 是 GraphQL 的一个 Ruby 实现。 Website API Documentation Newsletter 安装: # Gemfilegem 'graphql' $ bundle install 旨在: 实现 GraphQL 规范并支持一个 Relay 前端 在可能的情况下,提供与参考实现相似的习惯性的、简单的 Ruby API 支持 Ruby on Rails 和 Relay