forked from infiotinc/gqlgenc
/
query.go
94 lines (74 loc) · 2.67 KB
/
query.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package clientgen
import (
"fmt"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/parser"
"github.com/vektah/gqlparser/v2/validator"
)
func ParseQueryDocuments(schema *ast.Schema, querySources []*ast.Source) (*ast.QueryDocument, error) {
var queryDocument ast.QueryDocument
for _, querySource := range querySources {
query, gqlerr := parser.ParseQuery(querySource)
if gqlerr != nil {
return nil, fmt.Errorf(": %w", gqlerr)
}
mergeQueryDocument(&queryDocument, query)
}
if errs := validator.Validate(schema, &queryDocument); errs != nil {
return nil, fmt.Errorf(": %w", errs)
}
return &queryDocument, nil
}
func mergeQueryDocument(q, other *ast.QueryDocument) {
q.Operations = append(q.Operations, other.Operations...)
q.Fragments = append(q.Fragments, other.Fragments...)
}
func QueryDocumentsByOperations(schema *ast.Schema, operations ast.OperationList) ([]*ast.QueryDocument, error) {
queryDocuments := make([]*ast.QueryDocument, 0, len(operations))
for _, operation := range operations {
fragments := fragmentsInOperationDefinition(operation)
queryDocument := &ast.QueryDocument{
Operations: ast.OperationList{operation},
Fragments: fragments,
Position: nil,
}
if errs := validator.Validate(schema, queryDocument); errs != nil {
return nil, fmt.Errorf(": %w", errs)
}
queryDocuments = append(queryDocuments, queryDocument)
}
return queryDocuments, nil
}
func fragmentsInOperationDefinition(operation *ast.OperationDefinition) ast.FragmentDefinitionList {
fragments := fragmentsInOperationWalker(operation.SelectionSet)
uniqueFragments := fragmentsUnique(fragments)
return uniqueFragments
}
func fragmentsUnique(fragments ast.FragmentDefinitionList) ast.FragmentDefinitionList {
uniqueMap := make(map[string]*ast.FragmentDefinition)
for _, fragment := range fragments {
uniqueMap[fragment.Name] = fragment
}
uniqueFragments := make(ast.FragmentDefinitionList, 0, len(uniqueMap))
for _, fragment := range uniqueMap {
uniqueFragments = append(uniqueFragments, fragment)
}
return uniqueFragments
}
func fragmentsInOperationWalker(selectionSet ast.SelectionSet) ast.FragmentDefinitionList {
var fragments ast.FragmentDefinitionList
for _, selection := range selectionSet {
var selectionSet ast.SelectionSet
switch selection := selection.(type) {
case *ast.Field:
selectionSet = selection.SelectionSet
case *ast.InlineFragment:
selectionSet = selection.SelectionSet
case *ast.FragmentSpread:
fragments = append(fragments, selection.Definition)
selectionSet = selection.Definition.SelectionSet
}
fragments = append(fragments, fragmentsInOperationWalker(selectionSet)...)
}
return fragments
}