forked from wundergraph/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_fragment_definition.go
73 lines (63 loc) · 1.86 KB
/
ast_fragment_definition.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
package ast
import (
"bytes"
"github.com/dchukmasov/graphql-go-tools/internal/pkg/unsafebytes"
"github.com/dchukmasov/graphql-go-tools/pkg/lexer/position"
)
// TypeCondition
// example:
// on User
type TypeCondition struct {
On position.Position // on
Type int // NamedType
}
// FragmentDefinition
// example:
//
// fragment friendFields on User {
// id
// name
// profilePic(size: 50)
// }
type FragmentDefinition struct {
FragmentLiteral position.Position // fragment
Name ByteSliceReference // Name but not on, e.g. friendFields
TypeCondition TypeCondition // e.g. on User
HasDirectives bool
Directives DirectiveList // optional, e.g. @foo
SelectionSet int // e.g. { id }
HasSelections bool
}
func (d *Document) FragmentDefinitionRef(byName ByteSlice) (ref int, exists bool) {
for i := range d.FragmentDefinitions {
if bytes.Equal(byName, d.Input.ByteSlice(d.FragmentDefinitions[i].Name)) {
return i, true
}
}
return -1, false
}
func (d *Document) FragmentDefinitionTypeName(ref int) ByteSlice {
return d.ResolveTypeNameBytes(d.FragmentDefinitions[ref].TypeCondition.Type)
}
func (d *Document) FragmentDefinitionNameBytes(ref int) ByteSlice {
return d.Input.ByteSlice(d.FragmentDefinitions[ref].Name)
}
func (d *Document) FragmentDefinitionNameString(ref int) string {
return unsafebytes.BytesToString(d.Input.ByteSlice(d.FragmentDefinitions[ref].Name))
}
func (d *Document) FragmentDefinitionIsLastRootNode(ref int) bool {
for i := range d.RootNodes {
if d.RootNodes[i].Kind == NodeKindFragmentDefinition && d.RootNodes[i].Ref == ref {
return len(d.RootNodes)-1 == i
}
}
return false
}
func (d *Document) FragmentDefinitionIsUsed(name ByteSlice) bool {
for _, i := range d.Index.ReplacedFragmentSpreads {
if bytes.Equal(name, d.FragmentSpreadNameBytes(i)) {
return true
}
}
return false
}