Skip to content

Commit 7d39059

Browse files
committed
create graphql schema
1 parent a175dc1 commit 7d39059

File tree

5 files changed

+215
-0
lines changed

5 files changed

+215
-0
lines changed

go-graphql/beastData.json

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "Amphisbaena",
5+
"description": "A serpent with two heads, one at either end",
6+
"otherNames": [
7+
"Amfivena",
8+
"Anphine",
9+
"Anphivena",
10+
"Fenmine"
11+
],
12+
"imageUrl": "https://bestiary.ca/beastimage/img100489.jpg"
13+
},
14+
{
15+
"id": 2,
16+
"name": "Monocerus",
17+
"description": "A fierce beast with a single long horn",
18+
"otherNames": [
19+
"Monoceros",
20+
"Monocheros",
21+
"Rhinoceros",
22+
"Rynoceron",
23+
"Unicorn"
24+
],
25+
"imageUrl": "https://bestiary.ca/beastimage/img1012.jpg"
26+
},
27+
{
28+
"id": 3,
29+
"name": "Manticore",
30+
"description": "A composite beast with a man's face, a lion's body, and the stinger of a scorpion",
31+
"otherNames": [
32+
"Harpy"
33+
],
34+
"imageUrl": "https://bestiary.ca/beastimage/img101860.jpg"
35+
},
36+
{
37+
"id": 4,
38+
"name": "Mandrake",
39+
"description": "A plant with human-shaped roots, that shrieks when it is pulled from the earth",
40+
"otherNames": [],
41+
"imageUrl": "https://bestiary.ca/beastimage/img4521.jpg"
42+
},
43+
{
44+
"id": 5,
45+
"name": "Parandrus",
46+
"description": "A beast that can conceal itself by changing its appearance",
47+
"otherNames": [
48+
"Tarandrus",
49+
"Tharandus"
50+
],
51+
"imageUrl": "https://bestiary.ca/beastimage/img184.jpg"
52+
},
53+
{
54+
"id": 6,
55+
"name": "Peridexion Tree",
56+
"description": "A tree in India that attracts doves and repels dragons",
57+
"otherNames": [
58+
"Aradixon",
59+
"Circa dexteram",
60+
"Environ destre",
61+
"Paradixion",
62+
"Pendens",
63+
"Peredixion",
64+
"Perindens"
65+
],
66+
"imageUrl": "https://bestiary.ca/beastimage/img2140.jpg"
67+
},
68+
{
69+
"id": 7,
70+
"name": "Scitalis",
71+
"otherNames": [
72+
"Scytale"
73+
],
74+
"description": "A serpent with such a marvellous appearance that it stuns the viewer",
75+
"imageUrl": "https://bestiary.ca/beastimage/img102075.jpg"
76+
},
77+
{
78+
"id": 8,
79+
"name": "Sea-pig",
80+
"description": "Sea-pigs dig up the ground under water",
81+
"otherNames": [
82+
"Dugong",
83+
"Porco"
84+
],
85+
"imageUrl": "https://bestiary.ca/beastimage/img418.jpg"
86+
},
87+
{
88+
"id": 9,
89+
"name": "Hippocampus",
90+
"description": "A beast with the front half of a horse and tail of a fish or serpent",
91+
"otherNames": [
92+
"Sea-horse"
93+
],
94+
"imageUrl": "https://bestiary.ca/beastimage/img101258.jpg"
95+
},
96+
{
97+
"id": 10,
98+
"name": "Yale",
99+
"description": "A beast with flexible horns that it can move at will",
100+
"otherNames": [
101+
"Centicore"
102+
],
103+
"imageUrl": "https://bestiary.ca/beastimage/img4955.jpg"
104+
}
105+
]

go-graphql/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/sushanpth/learn-go/go-graphql
2+
3+
go 1.20
4+
5+
require (
6+
github.com/graphql-go/graphql v0.8.1
7+
github.com/graphql-go/handler v0.2.3
8+
)

go-graphql/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/graphql-go/graphql v0.8.1 h1:p7/Ou/WpmulocJeEx7wjQy611rtXGQaAcXGqanuMMgc=
2+
github.com/graphql-go/graphql v0.8.1/go.mod h1:nKiHzRM0qopJEwCITUuIsxk9PlVlwIiiI8pnJEhordQ=
3+
github.com/graphql-go/handler v0.2.3 h1:CANh8WPnl5M9uA25c2GBhPqJhE53Fg0Iue/fRNla71E=
4+
github.com/graphql-go/handler v0.2.3/go.mod h1:leLF6RpV5uZMN1CdImAxuiayrYYhOk33bZciaUGaXeU=

go-graphql/schema/schema.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package schema
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
8+
"github.com/graphql-go/graphql"
9+
)
10+
11+
// Helper function to import json from file to map
12+
func importJSONDataFromFile(fileName string, result interface{}) bool {
13+
14+
content, err := ioutil.ReadFile(fileName)
15+
if err != nil {
16+
fmt.Println("Error: ", err)
17+
return false
18+
}
19+
err = json.Unmarshal(content, result)
20+
if err != nil {
21+
fmt.Println("Error: ", err)
22+
return false
23+
}
24+
return true
25+
}
26+
27+
var BeastList []Beast
28+
var _ = importJSONDataFromFile("beastData.json", &BeastList)
29+
30+
// if err != nil {
31+
// fmt.Println("Error: ", err)
32+
// }
33+
34+
type Beast struct {
35+
ID int `json:"id"`
36+
Name string `json:"name"`
37+
Description string `json:"description"`
38+
OtherNames []string `json:"otherNames"`
39+
ImageURL string `json:"imageUrl"`
40+
}
41+
42+
var beastType = graphql.NewObject(graphql.ObjectConfig{
43+
Name: "Beast",
44+
Fields: graphql.Fields{
45+
"name": &graphql.Field{
46+
Type: graphql.String,
47+
},
48+
"description": &graphql.Field{
49+
Type: graphql.String,
50+
},
51+
"id": &graphql.Field{
52+
Type: graphql.Int,
53+
},
54+
"otherNames": &graphql.Field{
55+
Type: graphql.NewList(graphql.String),
56+
},
57+
},
58+
})
59+
60+
var rootQuery = graphql.NewObject(graphql.ObjectConfig{
61+
Name: "RootQuery",
62+
Fields: graphql.Fields{
63+
"beast": &graphql.Field{
64+
Type: beastType,
65+
Description: "Get single beast",
66+
Args: graphql.FieldConfigArgument{
67+
"name": &graphql.ArgumentConfig{
68+
Type: graphql.String,
69+
},
70+
},
71+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
72+
nameQuery, isOk := p.Args["name"].(string)
73+
if isOk {
74+
// search for el with name
75+
for _, beast := range BeastList {
76+
if beast.Name == nameQuery {
77+
return beast, nil
78+
}
79+
}
80+
}
81+
return Beast{}, nil
82+
},
83+
},
84+
"beastList": &graphql.Field{
85+
Type: graphql.NewList(beastType),
86+
Description: "List of beasts",
87+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
88+
return BeastList, nil
89+
},
90+
},
91+
},
92+
})
93+
94+
// define schema, with our rootQuery
95+
var BeastSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
96+
Query: rootQuery,
97+
})

go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ go 1.20
33
use (
44
./di-fx-1
55
./di-fx-modular
6+
./go-graphql
67
./go-jwt
78
./go-routine-channel
89
./redis-elastic-cache

0 commit comments

Comments
 (0)