在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:graphql-go/graphql开源软件地址:https://github.com/graphql-go/graphql开源编程语言:Go 100.0%开源软件介绍:graphqlAn implementation of GraphQL in Go. Follows the official reference implementation Supports: queries, mutations & subscriptions. Documentationgodoc: https://pkg.go.dev/github.com/graphql-go/graphql Getting StartedTo install the library, run: go get github.com/graphql-go/graphql The following is a simple example which defines a schema with a single 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
Blog Posts |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论