Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
58 lines (51 sloc) 1.29 KB
//
// Copied from https://github.com/graphql-go/graphql
//
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/graphql-go/graphql"
)
func main() {
// Schema
fields := graphql.Fields{
"message": &graphql.Field{
Args: graphql.FieldConfigArgument{
"locale": &graphql.ArgumentConfig{
Type: graphql.String,
DefaultValue: "en-US",
Description: "locale of the caller used in translations",
},
},
Type: graphql.String,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
argLocale, ok := p.Args["locale"].(string)
if ok {
return "your message was in " + argLocale, nil
}
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 := `
{
message(locale:"en-JP")
}
`
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”}}
}
You can’t perform that action at this time.