Skip to content

Instantly share code, notes, and snippets.

@bbuck
Last active July 20, 2018 11:56
Show Gist options
  • Save bbuck/ced123f482fb199108b6cf16b23792e9 to your computer and use it in GitHub Desktop.
Save bbuck/ced123f482fb199108b6cf16b23792e9 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/graphql-go/graphql"
)
var orderByChoices = graphql.EnumValueConfigMap{
"EffectiveDateMax": &graphql.EnumValueConfig{Value: "EffectiveDateMax"},
"EffectiveDateMin": &graphql.EnumValueConfig{Value: "EffectiveDateMin"},
}
var orderByChoicesEnum = graphql.NewEnum(graphql.EnumConfig{
Name: "OrderBy",
Values: orderByChoices,
})
var EnumQuery = graphql.NewObject(graphql.ObjectConfig{
Name: "RootQuery",
Fields: graphql.Fields{
"echo": &graphql.Field{
Type: graphql.String,
Args: graphql.FieldConfigArgument{
"order_by": &graphql.ArgumentConfig{
Type: graphql.NewList(orderByChoicesEnum),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
fmt.Printf("%T %#v\n", params.Args["order_by"], params.Args["order_by"])
return "true", nil
},
},
},
})
var queryTest = `
{
echo(order_by: [EffectiveDateMax])
}
`
func main() {
schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: EnumQuery,
})
if err != nil {
log.Fatalf("failed to create schema: %s", err)
}
result := graphql.Do(graphql.Params{
Schema: schema,
RequestString: queryTest,
})
jsonStr, err := json.MarshalIndent(result, "", " ")
if err != nil {
log.Fatalf("fail to jsonify result: %s", err)
}
fmt.Println(string(jsonStr))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment