{"id":26927426,"url":"https://github.com/neelance/graphql-go","last_synced_at":"2025-04-02T04:01:58.403Z","repository":{"id":38343021,"uuid":"71253874","full_name":"graph-gophers/graphql-go","owner":"graph-gophers","description":"GraphQL server with a focus on ease of use","archived":false,"fork":false,"pushed_at":"2025-02-22T12:02:51.000Z","size":1689,"stargazers_count":4687,"open_issues_count":43,"forks_count":490,"subscribers_count":80,"default_branch":"master","last_synced_at":"2025-04-01T16:07:31.353Z","etag":null,"topics":["go","golang","graphql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/graph-gophers.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-18T13:57:24.000Z","updated_at":"2025-03-31T14:56:04.000Z","dependencies_parsed_at":"2023-07-13T10:24:21.667Z","dependency_job_id":"c14075b1-d3ef-4437-8831-72ffe461ed91","html_url":"https://github.com/graph-gophers/graphql-go","commit_stats":{"total_commits":569,"total_committers":95,"mean_commits":5.989473684210527,"dds":0.5518453427065027,"last_synced_commit":"8fb462468c986c352c3da5e490f573582cc049ee"},"previous_names":["neelance/graphql-go"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graph-gophers%2Fgraphql-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graph-gophers%2Fgraphql-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graph-gophers%2Fgraphql-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graph-gophers%2Fgraphql-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graph-gophers","download_url":"https://codeload.github.com/graph-gophers/graphql-go/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246752620,"owners_count":20827987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","graphql"],"created_at":"2025-04-02T04:01:58.093Z","updated_at":"2025-04-02T04:01:58.392Z","avatar_url":"https://github.com/graph-gophers.png","language":"Go","funding_links":[],"categories":["Query Language","查询语言","Libraries","Go语言包管理","\u003cspan id=\"查询语言-query-language\"\u003e查询语言 Query Language\u003c/span\u003e"],"sub_categories":["HTTP Clients","HTTP客户端","Go Libraries","Advanced Console UIs","查询语","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"readme":"# graphql-go [![Sourcegraph](https://sourcegraph.com/github.com/graph-gophers/graphql-go/-/badge.svg)](https://sourcegraph.com/github.com/graph-gophers/graphql-go?badge) [![Build Status](https://graph-gophers.semaphoreci.com/badges/graphql-go/branches/master.svg?style=shields)](https://graph-gophers.semaphoreci.com/projects/graphql-go) [![Go Report](https://goreportcard.com/badge/github.com/graph-gophers/graphql-go)](https://goreportcard.com/report/github.com/graph-gophers/graphql-go) [![GoDoc](https://godoc.org/github.com/graph-gophers/graphql-go?status.svg)](https://godoc.org/github.com/graph-gophers/graphql-go)\n\n\u003cp align=\"center\"\u003e\u003cimg src=\"docs/img/logo.png\" width=\"300\"\u003e\u003c/p\u003e\n\nThe goal of this project is to provide full support of the [October 2021 GraphQL specification](https://spec.graphql.org/October2021/) with a set of idiomatic, easy to use Go packages.\n\nWhile still under development (`internal` APIs are almost certainly subject to change), this library is safe for production use.\n\n## Features\n\n- minimal API\n- support for `context.Context`\n- support for the `OpenTelemetry` and `OpenTracing` standards\n- schema type-checking against resolvers\n- resolvers are matched to the schema based on method sets (can resolve a GraphQL schema with a Go interface or Go struct).\n- handles panics in resolvers\n- parallel execution of resolvers\n- subscriptions\n  - [sample WS transport](https://github.com/graph-gophers/graphql-transport-ws)\n\n## (Some) Documentation [![GoDoc](https://godoc.org/github.com/graph-gophers/graphql-go?status.svg)](https://godoc.org/github.com/graph-gophers/graphql-go)\n\n### Getting started\n\nIn order to run a simple GraphQL server locally create a `main.go` file with the following content:\n```go\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\n\tgraphql \"github.com/graph-gophers/graphql-go\"\n\t\"github.com/graph-gophers/graphql-go/relay\"\n)\n\ntype query struct{}\n\nfunc (query) Hello() string { return \"Hello, world!\" }\n\nfunc main() {\n\ts := `\n        type Query {\n                hello: String!\n        }\n    `\n\tschema := graphql.MustParseSchema(s, \u0026query{})\n\thttp.Handle(\"/query\", \u0026relay.Handler{Schema: schema})\n\tlog.Fatal(http.ListenAndServe(\":8080\", nil))\n}\n\n```\nThen run the file with `go run main.go`. To test:\n\t    \n```sh\ncurl -XPOST -d '{\"query\": \"{ hello }\"}' localhost:8080/query\n```\nFor more realistic usecases check our [examples section](https://github.com/graph-gophers/graphql-go/wiki/Examples).\n\n### Resolvers\n\nA 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](https://golang.org/ref/spec#Exported_identifiers) and match the schema's field's name in a non-case-sensitive way.\nYou can use struct fields as resolvers by using `SchemaOpt: UseFieldResolvers()`. For example,\n```\nopts := []graphql.SchemaOpt{graphql.UseFieldResolvers()}\nschema := graphql.MustParseSchema(s, \u0026query{}, opts...)\n```   \n\nWhen using `UseFieldResolvers` schema option, a struct field will be used *only* when:\n- there is no method for a struct field\n- a struct field does not implement an interface method\n- a struct field does not have arguments\n\nThe method has up to two arguments:\n\n- Optional `context.Context` argument.\n- Mandatory `*struct { ... }` argument if the corresponding GraphQL field has arguments. The names of the struct fields have to be [exported](https://golang.org/ref/spec#Exported_identifiers) and have to match the names of the GraphQL arguments in a non-case-sensitive way.\n\nThe method has up to two results:\n\n- The GraphQL field's value as determined by the resolver.\n- Optional `error` result.\n\nExample for a simple resolver method:\n\n```go\nfunc (r *helloWorldResolver) Hello() string {\n\treturn \"Hello world!\"\n}\n```\n\nThe following signature is also allowed:\n\n```go\nfunc (r *helloWorldResolver) Hello(ctx context.Context) (string, error) {\n\treturn \"Hello world!\", nil\n}\n```\n\n### Separate resolvers for different operations\n\u003e **NOTE**: This feature is not in the stable release yet. In order to use it you need to run `go get github.com/graph-gophers/graphql-go@master` and in your `go.mod` file you will have something like:\n\u003e  ```\n\u003e  v1.5.1-0.20230216224648-5aa631d05992\n\u003e  ```\n\u003e It is expected to be released in `v1.6.0` soon.\n\nThe GraphQL specification allows for fields with the same name defined in different query types. For example, the schema below is a valid schema definition:\n```graphql\nschema {\n  query: Query\n  mutation: Mutation\n}\n\ntype Query {\n  hello: String!\n}\n\ntype Mutation {\n  hello: String!\n}\n```\nThe above schema would result in name collision if we use a single resolver struct because fields from both operations correspond to methods in the root resolver (the same Go struct). In order to resolve this issue, the library allows resolvers for query, mutation and subscription operations to be separated using the `Query`, `Mutation` and `Subscription` methods of the root resolver. These special methods are optional and if defined return the resolver for each opeartion. For example, the following is a resolver corresponding to the schema definition above. Note that there is a field named `hello` in both the query and the mutation definitions:\n\n```go\ntype RootResolver struct{}\ntype QueryResolver struct{}\ntype MutationResolver struct{}\n\nfunc(r *RootResolver) Query() *QueryResolver {\n  return \u0026QueryResolver{}\n}\n\nfunc(r *RootResolver) Mutation() *MutationResolver {\n  return \u0026MutationResolver{}\n}\n\nfunc (*QueryResolver) Hello() string {\n\treturn \"Hello query!\"\n}\n\nfunc (*MutationResolver) Hello() string {\n\treturn \"Hello mutation!\"\n}\n\nschema := graphql.MustParseSchema(sdl, \u0026RootResolver{}, nil)\n...\n```\n\n### Schema Options\n\n- `UseStringDescriptions()` enables the usage of double quoted and triple quoted. When this is not enabled, comments are parsed as descriptions instead.\n- `UseFieldResolvers()` specifies whether to use struct field resolvers.\n- `MaxDepth(n int)` specifies the maximum field nesting depth in a query. The default is 0 which disables max depth checking.\n- `MaxParallelism(n int)` specifies the maximum number of resolvers per request allowed to run in parallel. The default is 10.\n- `Tracer(tracer trace.Tracer)` is used to trace queries and fields. It defaults to `noop.Tracer`.\n- `Logger(logger log.Logger)` is used to log panics during query execution. It defaults to `exec.DefaultLogger`.\n- `PanicHandler(panicHandler errors.PanicHandler)` is used to transform panics into errors during query execution. It defaults to `errors.DefaultPanicHandler`.\n- `DisableIntrospection()` disables introspection queries.\n\n### Custom Errors\n\nErrors returned by resolvers can include custom extensions by implementing the `ResolverError` interface:\n\n```go\ntype ResolverError interface {\n\terror\n\tExtensions() map[string]interface{}\n}\n```\n\nExample of a simple custom error:\n\n```go\ntype droidNotFoundError struct {\n\tCode    string `json:\"code\"`\n\tMessage string `json:\"message\"`\n}\n\nfunc (e droidNotFoundError) Error() string {\n\treturn fmt.Sprintf(\"error [%s]: %s\", e.Code, e.Message)\n}\n\nfunc (e droidNotFoundError) Extensions() map[string]interface{} {\n\treturn map[string]interface{}{\n\t\t\"code\":    e.Code,\n\t\t\"message\": e.Message,\n\t}\n}\n```\n\nWhich could produce a GraphQL error such as:\n\n```go\n{\n  \"errors\": [\n    {\n      \"message\": \"error [NotFound]: This is not the droid you are looking for\",\n      \"path\": [\n        \"droid\"\n      ],\n      \"extensions\": {\n        \"code\": \"NotFound\",\n        \"message\": \"This is not the droid you are looking for\"\n      }\n    }\n  ],\n  \"data\": null\n}\n```\n\n### Tracing\n\nBy default the library uses `noop.Tracer`. If you want to change that you can use the OpenTelemetry or the OpenTracing implementations, respectively:\n\n```go\n// OpenTelemetry tracer\npackage main\n\nimport (\n\t\"github.com/graph-gophers/graphql-go\"\n\t\"github.com/graph-gophers/graphql-go/example/starwars\"\n\totelgraphql \"github.com/graph-gophers/graphql-go/trace/otel\"\n\t\"github.com/graph-gophers/graphql-go/trace/tracer\"\n)\n// ...\n_, err := graphql.ParseSchema(starwars.Schema, nil, graphql.Tracer(otelgraphql.DefaultTracer()))\n// ...\n```\nAlternatively you can pass an existing trace.Tracer instance:\n```go\ntr := otel.Tracer(\"example\")\n_, err = graphql.ParseSchema(starwars.Schema, nil, graphql.Tracer(\u0026otelgraphql.Tracer{Tracer: tr}))\n```\n\n\n```go\n// OpenTracing tracer\npackage main\n\nimport (\n\t\"github.com/graph-gophers/graphql-go\"\n\t\"github.com/graph-gophers/graphql-go/example/starwars\"\n\t\"github.com/graph-gophers/graphql-go/trace/opentracing\"\n\t\"github.com/graph-gophers/graphql-go/trace/tracer\"\n)\n// ...\n_, err := graphql.ParseSchema(starwars.Schema, nil, graphql.Tracer(opentracing.Tracer{}))\n\n// ...\n```\n\nIf you need to implement a custom tracer the library would accept any tracer which implements the interface below:\n```go\ntype Tracer interface {\n    TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, varTypes map[string]*introspection.Type) (context.Context, func([]*errors.QueryError))\n    TraceField(ctx context.Context, label, typeName, fieldName string, trivial bool, args map[string]interface{}) (context.Context, func(*errors.QueryError))\n    TraceValidation(context.Context) func([]*errors.QueryError)\n}\n```\n\n\n### [Examples](https://github.com/graph-gophers/graphql-go/wiki/Examples)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneelance%2Fgraphql-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneelance%2Fgraphql-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneelance%2Fgraphql-go/lists"}