在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):graph-gophers/graphql-go开源软件地址(OpenSource Url):https://github.com/graph-gophers/graphql-go开源编程语言(OpenSource Language):Go 97.2%开源软件介绍(OpenSource Introduction):graphql-goThe 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 ( Features
RoadmapWe're trying out the GitHub Project feature to manage (Some) DocumentationGetting startedIn order to run a simple GraphQL server locally create a 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))
} Then run the file with curl -XPOST -d '{"query": "{ hello }"}' localhost:8080/query For more realistic usecases check our examples section. ResolversA 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
When using
The method has up to two arguments:
The method has up to two results:
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
} Schema Options
Custom ErrorsErrors returned by resolvers can include custom extensions by implementing the 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
} TracingBy default the library uses // OpenTelemetry tracer
package main
import (
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/starwars"
otelgraphql "github.com/graph-gophers/graphql-go/trace/otel"
"github.com/graph-gophers/graphql-go/trace/tracer"
)
// ...
_, err := graphql.ParseSchema(starwars.Schema, nil, graphql.Tracer(otelgraphql.DefaultTracer()))
// ... Alternatively you can pass an existing trace.Tracer instance: tr := otel.Tracer("example")
_, err = graphql.ParseSchema(starwars.Schema, nil, graphql.Tracer(&otelgraphql.Tracer{Tracer: tr})) // OpenTracing tracer
package main
import (
"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/example/starwars"
"github.com/graph-gophers/graphql-go/trace/opentracing"
"github.com/graph-gophers/graphql-go/trace/tracer"
)
// ...
_, err := graphql.ParseSchema(starwars.Schema, nil, graphql.Tracer(opentracing.Tracer{}))
// ... If you need to implement a custom tracer the library would accept any tracer which implements the interface below: type Tracer interface {
TraceQuery(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, varTypes map[string]*introspection.Type) (context.Context, func([]*errors.QueryError))
TraceField(ctx context.Context, label, typeName, fieldName string, trivial bool, args map[string]interface{}) (context.Context, func(*errors.QueryError))
TraceValidation(context.Context) func([]*errors.QueryError)
} Examples |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论