mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 12:46:03 +03:00
added graphql
This commit is contained in:
parent
1d8a410b93
commit
33a9afa2cc
41
graphql/main.go
Normal file
41
graphql/main.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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”}}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user