Skip to content

Instantly share code, notes, and snippets.

@EmperorEarth
Forked from sogko/schema.go
Last active August 3, 2016 06:12
Show Gist options
  • Save EmperorEarth/a27a6ec5fc8b6ebdb8936107264cddcd to your computer and use it in GitHub Desktop.
Save EmperorEarth/a27a6ec5fc8b6ebdb8936107264cddcd to your computer and use it in GitHub Desktop.
hello-world-relay-part-2 - Schema definition (golang)
package data
import (
"github.com/graphql-go/graphql"
"github.com/graphql-go/relay"
)
var postType *graphql.Object
var queryType *graphql.Object
var Schema graphql.Schema
func init() {
postType = graphql.NewObject(graphql.ObjectConfig{
Name: "Post",
Fields: graphql.Fields{
// Define `id` field as a Relay GlobalID field.
// It helps with translating your GraphQL object's id into a global id
// For eg:
// For a `Post` type, with an id of `1`, it's global id will be `UG9zdDox`
// which is a base64 encoded version of `Post:1` string
// We will explore more in the next part of this series.
"id": relay.GlobalIDField("Post", nil),
"text": &graphql.Field{
Type: graphql.String,
},
},
})
queryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"latestPost": &graphql.Field{
Type: postType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return GetLatestPost()
},
},
},
})
Schema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: queryType,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment