-
Notifications
You must be signed in to change notification settings - Fork 58
/
plural.go
49 lines (44 loc) · 1.35 KB
/
plural.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
package relay
import (
"github.com/graphql-go/graphql"
)
type ResolveSingleInputFn func(input interface{}) interface{}
type PluralIdentifyingRootFieldConfig struct {
ArgName string `json:"argName"`
InputType graphql.Input `json:"inputType"`
OutputType graphql.Output `json:"outputType"`
ResolveSingleInput ResolveSingleInputFn `json:"resolveSingleInput"`
Description string `json:"description"`
}
func PluralIdentifyingRootField(config PluralIdentifyingRootFieldConfig) *graphql.Field {
inputArgs := graphql.FieldConfigArgument{}
if config.ArgName != "" {
inputArgs[config.ArgName] = &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(config.InputType))),
}
}
return &graphql.Field{
Description: config.Description,
Type: graphql.NewList(config.OutputType),
Args: inputArgs,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
inputs, ok := p.Args[config.ArgName]
if !ok {
return nil, nil
}
if config.ResolveSingleInput == nil {
return nil, nil
}
switch inputs := inputs.(type) {
case []interface{}:
res := []interface{}{}
for _, input := range inputs {
r := config.ResolveSingleInput(input)
res = append(res, r)
}
return res, nil
}
return nil, nil
},
}
}