Skip to content

Instantly share code, notes, and snippets.

@elliotforbes
Created December 28, 2018 21:04
Show Gist options
  • Save elliotforbes/9b8400ef5154eb3420e409aeffe39633 to your computer and use it in GitHub Desktop.
Save elliotforbes/9b8400ef5154eb3420e409aeffe39633 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/graphql-go/graphql"
)
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),
},
},
},
)
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)}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
// Query
query := `
{
list {
title
author {
Name
Tutorials
}
}
}
`
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