Skip to content

Instantly share code, notes, and snippets.

@elliotforbes
Created December 30, 2018 13:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elliotforbes/becdd2b6d57260e88ac0698cb6c83d0b to your computer and use it in GitHub Desktop.
Save elliotforbes/becdd2b6d57260e88ac0698cb6c83d0b to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/graphql-go/graphql"
)
var tutorials []Tutorial
type Tutorial struct {
ID int
Title string
Author Author
Comments []Comment
}
type Author struct {
Name string
Tutorials []int
}
type Comment struct {
Body string
}
func populate() []Tutorial {
author := &Author{Name: "Elliot Forbes", Tutorials: []int{1, 2}}
tutorial := Tutorial{
ID: 1,
Title: "Go GraphQL Tutorial",
Author: *author,
Comments: []Comment{
Comment{Body: "First Comment"},
},
}
tutorial2 := Tutorial{
ID: 2,
Title: "Go GraphQL Tutorial - Part 2",
Author: *author,
Comments: []Comment{
Comment{Body: "Second Comment"},
},
}
var tutorials []Tutorial
tutorials = append(tutorials, tutorial)
tutorials = append(tutorials, tutorial2)
return tutorials
}
var authorType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Author",
Fields: graphql.Fields{
"Name": &graphql.Field{
Type: graphql.String,
},
"Tutorials": &graphql.Field{
Type: graphql.NewList(graphql.Int),
},
},
},
)
var commentType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Comment",
Fields: graphql.Fields{
"body": &graphql.Field{
Type: graphql.String,
},
},
},
)
var tutorialType = graphql.NewObject(
graphql.ObjectConfig{
Name: "Tutorial",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.Int,
},
"title": &graphql.Field{
Type: graphql.String,
},
"author": &graphql.Field{
Type: authorType,
},
"comments": &graphql.Field{
Type: graphql.NewList(commentType),
},
},
},
)
var mutationType = graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"create": &graphql.Field{
Type: tutorialType,
Description: "Create a new Tutorial",
Args: graphql.FieldConfigArgument{
"title": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.String),
},
},
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
tutorial := Tutorial{
Title: params.Args["title"].(string),
}
tutorials = append(tutorials, tutorial)
return tutorial, nil
},
},
},
})
func main() {
tutorials = populate()
// Schema
fields := graphql.Fields{
"tutorial": &graphql.Field{
Type: tutorialType,
Description: "Get Tutorial By ID",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id, ok := p.Args["id"].(int)
if ok {
// Find tutorial
for _, tutorial := range tutorials {
if int(tutorial.ID) == id {
return tutorial, nil
}
}
}
return nil, nil
},
},
"list": &graphql.Field{
Type: graphql.NewList(tutorialType),
Description: "Get Tutorial List",
Resolve: func(params graphql.ResolveParams) (interface{}, error) {
return tutorials, nil
},
},
}
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
schemaConfig := graphql.SchemaConfig{
Query: graphql.NewObject(rootQuery),
Mutation: mutationType,
}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
// Query
query := `
mutation {
create(title: "Hello World") {
title
}
}
`
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)
// Query
query = `
{
list {
id
title
}
}
`
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment