forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgql.go
More file actions
41 lines (39 loc) · 810 Bytes
/
Copy pathgql.go
File metadata and controls
41 lines (39 loc) · 810 Bytes
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
package gql
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
)
func AsGQL(ctx context.Context, req interface{}) (*string, error) {
mp := make(map[string]interface{})
bytes, err := json.Marshal(req)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &mp)
if err != nil {
return nil, err
}
fields := []string{}
for k, i := range mp {
// GQL Selection
switch i.(type) {
case bool:
// GQL Selection
fields = append(fields, k)
case map[string]interface{}:
// Nested GQL/Struct
nested, err := AsGQL(ctx, i)
if err != nil {
return nil, err
}
fields = append(fields, fmt.Sprintf("%s {\n%s\n}", k, *nested))
default:
return nil, errors.New("Unsupported Type! Cannot generate GQL")
}
}
q := strings.Join(fields, "\n")
return &q, nil
}