This repository has been archived by the owner on May 2, 2021. It is now read-only.
forked from functionalfoundry/graphqlws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.go
59 lines (53 loc) · 1.33 KB
/
ast.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
package graphqlws
import (
"github.com/graphql-go/graphql/language/ast"
)
func operationDefinitionsWithOperation(
doc *ast.Document,
op string,
) []*ast.OperationDefinition {
defs := []*ast.OperationDefinition{}
for _, node := range doc.Definitions {
if node.GetKind() == "OperationDefinition" {
if def, ok := node.(*ast.OperationDefinition); ok {
if def.Operation == op {
defs = append(defs, def)
}
}
}
}
return defs
}
func selectionSetsForOperationDefinitions(
defs []*ast.OperationDefinition,
) []*ast.SelectionSet {
sets := []*ast.SelectionSet{}
for _, def := range defs {
if set := def.GetSelectionSet(); set != nil {
sets = append(sets, set)
}
}
return sets
}
func nameForSelectionSet(set *ast.SelectionSet) (string, bool) {
if len(set.Selections) >= 1 {
if field, ok := set.Selections[0].(*ast.Field); ok {
return field.Name.Value, true
}
}
return "", false
}
func namesForSelectionSets(sets []*ast.SelectionSet) []string {
names := []string{}
for _, set := range sets {
if name, ok := nameForSelectionSet(set); ok {
names = append(names, name)
}
}
return names
}
func subscriptionFieldNamesFromDocument(doc *ast.Document) []string {
defs := operationDefinitionsWithOperation(doc, "subscription")
sets := selectionSetsForOperationDefinitions(defs)
return namesForSelectionSets(sets)
}