forked from graphql-go/relay
/
plural.go
49 lines (44 loc) · 1.4 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 gqlrelay
import (
"github.com/chris-ramon/graphql-go/types"
)
type ResolveSingleInputFn func(input interface{}) interface{}
type PluralIdentifyingRootFieldConfig struct {
ArgName string `json:"argName"`
InputType types.GraphQLInputType `json:"inputType"`
OutputType types.GraphQLOutputType `json:"outputType"`
ResolveSingleInput ResolveSingleInputFn `json:"resolveSingleInput"`
Description string `json:"description"`
}
func PluralIdentifyingRootField(config PluralIdentifyingRootFieldConfig) *types.GraphQLFieldConfig {
inputArgs := types.GraphQLFieldConfigArgumentMap{}
if config.ArgName != "" {
inputArgs[config.ArgName] = &types.GraphQLArgumentConfig{
Type: types.NewGraphQLNonNull(types.NewGraphQLList(types.NewGraphQLNonNull(config.InputType))),
}
}
return &types.GraphQLFieldConfig{
Description: config.Description,
Type: types.NewGraphQLList(config.OutputType),
Args: inputArgs,
Resolve: func(p types.GQLFRParams) interface{} {
inputs, ok := p.Args[config.ArgName]
if !ok {
return nil
}
if config.ResolveSingleInput == nil {
return nil
}
switch inputs := inputs.(type) {
case []interface{}:
res := []interface{}{}
for _, input := range inputs {
r := config.ResolveSingleInput(input)
res = append(res, r)
}
return res
}
return nil
},
}
}