-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
56 lines (51 loc) · 1.27 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
"github.com/graphql-go/graphql"
"github.com/racerxdl/pokemon-gopher/database"
"github.com/racerxdl/pokemon-gopher/gqlmodels"
)
var QueryType = graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Description: "Query any pokemon by number or name",
Fields: graphql.Fields{
"pokemons": {
Type: graphql.NewList(gqlmodels.Pokemon),
Args: graphql.FieldConfigArgument{
"first": {
Type: graphql.NewNonNull(graphql.Int),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return database.GetPokemons(p.Args)
},
},
"pokemon": {
Type: gqlmodels.Pokemon,
Args: graphql.FieldConfigArgument{
"id": {
Type: graphql.String,
},
"name": {
Type: graphql.String,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if p.Args["id"] != nil {
id := p.Args["id"].(string)
return database.GetPokemonById(id)
}
if p.Args["name"] != nil {
name := p.Args["name"].(string)
return database.GetPokemonByName(name)
}
return nil, fmt.Errorf("you need to specify \"id\" or \"name\" of the pokemon")
},
},
},
})
func GetSchema() (graphql.Schema, error) {
return graphql.NewSchema(graphql.SchemaConfig{
Query: QueryType,
})
}