Created
September 28, 2020 13:25
-
-
Save sarthakpranesh/a1380b02aafbd575d6499eec5be18c45 to your computer and use it in GitHub Desktop.
100DaysOfCode - Day 42/100 - exploring GraphQL in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"github.com/graphql-go/graphql" | |
) | |
type Tutorial struct { | |
ID int | |
Title string | |
Author Author | |
Commnets []Comment | |
} | |
type Author struct { | |
Name string | |
Tutorials []int | |
} | |
type Comment struct { | |
Body string | |
} | |
func populate() []Tutorial { | |
author := Author{Name: "Sarthak", Tutorials: []int{1}} | |
tutorials := []Tutorial{ | |
Tutorial{ | |
ID: 1, | |
Title: "Go go lang", | |
Author: author, | |
Commnets: []Comment{ | |
Comment{Body: "Some comment"}, | |
}, | |
}, | |
} | |
return tutorials | |
} | |
func main() { | |
tutorials := populate() | |
var commentType = graphql.NewObject( | |
graphql.ObjectConfig{ | |
Name: "Comment", | |
Fields: graphql.Fields{ | |
"body": &graphql.Field{ | |
Type: graphql.String, | |
}, | |
}, | |
}, | |
) | |
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 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), | |
}, | |
}, | |
}, | |
) | |
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 { | |
for _, tutorial := range tutorials { | |
if tutorial.ID == id { | |
return tutorial, nil | |
} | |
} | |
} | |
return nil, nil | |
}, | |
}, | |
"list": &graphql.Field{ | |
Type: graphql.NewList(tutorialType), | |
Description: "Get full Tutorial list", | |
Resolve: func(p 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 Graph Schema, %v", err) | |
} | |
query := ` | |
{ | |
tutorial(id:1) { | |
title | |
author { | |
name | |
tutorials | |
} | |
} | |
list { | |
id | |
title | |
} | |
} | |
` | |
params := graphql.Params{Schema: schema, RequestString: query} | |
r := graphql.Do(params) | |
if len(r.Errors) > 0 { | |
log.Fatal("Request error: %-v \n", 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