graphql README

The project's README from github.com/graphql-go/graphql. As one markdown file: README.md.

graphql CircleCI Go Reference Coverage Status Join the chat at https://gitter.im/graphql-go/graphql

An implementation of GraphQL in Go. Follows the official reference implementation graphql-js.

Supports: queries, mutations & subscriptions.

Documentation

godoc: https://pkg.go.dev/github.com/graphql-go/graphql

Contribute Back

Friendly reminder links are available in case you would like to contribute back into our commitment with Go and open-source.

Author PayPal Link
Hafiz Ismail Not available yet.
Chris Ramón https://www.paypal.com/donate/?hosted_button_id=WHUQQYEMTRQBJ

Getting Started

To install the library, run:

go get github.com/graphql-go/graphql

The following is a simple example which defines a schema with a single hello string-type field and a Resolve method which returns the string world. A GraphQL query is performed against this schema with the resulting output printed in JSON format.

package main

import (
	"encoding/json"
	"fmt"
	"log"

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

func main() {
	// Schema
	fields := graphql.Fields{
		"hello": &graphql.Field{
			Type: graphql.String,
			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				return "world", nil
			},
		},
	}
	rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
	schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
	schema, err := graphql.NewSchema(schemaConfig)
	if err != nil {
		log.Fatalf("failed to create new schema, error: %v", err)
	}

	// Query
	query := `
		{
			hello
		}
	`
	params := graphql.Params{Schema: schema, RequestString: query}
	r := graphql.Do(params)
	if len(r.Errors) > 0 {
		log.Fatalf("failed to execute graphql operation, errors: %+v", r.Errors)
	}
	rJSON, _ := json.Marshal(r)
	fmt.Printf("%s \n", rJSON) // {"data":{"hello":"world"}}
}

For more complex examples, refer to the examples/ directory and graphql_test.go.

Third Party Libraries

Name Author Description
graphql-go-handler Hafiz Ismail Middleware to handle GraphQL queries through HTTP requests.
graphql-relay-go Hafiz Ismail Lib to construct a graphql-go server supporting react-relay.
golang-relay-starter-kit Hafiz Ismail Barebones starting point for a Relay application with Golang GraphQL server.
dataloader Nick Randall DataLoader implementation in Go.

Blog Posts

graphql (graphql-go/graphql)

Searching Database…

10.1K ·GitHub
这是什么?一个用 Go 实现的 GraphQL,遵循官方 graphql-js 参考实现,支持查询(query)、变更(mutation)和订阅(subscription)。
它是我导入自己服务器的库,还是独立的 GraphQL 服务器?它是一个 Go 库,导入你自己的应用来构建和执行 GraphQL schema 和 resolver;通过 HTTP 提供服务由 graphql-go-handler 这类单独的配套包负责。
怎么让它跑起来?安装该包,定义带字段和 Resolve 方法的 schema,然后对该 schema 执行一个 GraphQL 查询并打印 JSON 结果;examples/ 目录和 graphqltest.go 提供了更完整的示例。
除了 Go 工具链,使用它还需要别的吗?不需要,核心库只需要 Go 本身;如果你需要 HTTP 处理、Relay 兼容或批量加载,可以添加 graphql-go-handler、graphql-relay-go 或 dataloader 等单独的包。
核心包开箱自带 HTTP 服务器吗?不带,graphql-go 只在内存中构建和执行 schema;通过 HTTP 暴露、Relay 支持和数据加载都留给单独的第三方库。
它开箱支持 Relay 风格的分页吗?核心包不直接支持,但配套的 graphql-relay-go 库可以帮你构建与 react-relay 兼容的 graphql-go 服务器。
dataloader 包为它增加了什么?它是 Facebook 的 DataLoader 模式在 Go 里的独立实现,与 graphql-go 搭配使用时用于对 resolver 调用做批量和缓存。
在哪里能看到更完整的使用示例?除了 README 里的基础 hello-world 片段,仓库中的 examples/ 目录和 graphqltest.go 包含更复杂的 schema 和 resolver 示例。
What are similar projects?echo, gqlgen, chi, node-restify, guzzle, mux, martini, fiber
Creator
graphql-go
Size
3.6 MB
Stars
10.1K
Age
11y
Commits · 90 Days