forked from wundergraph/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ast_inline_fragment.go
88 lines (76 loc) · 2.62 KB
/
ast_inline_fragment.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
package ast
import (
"github.com/TykTechnologies/graphql-go-tools/v2/internal/pkg/unsafebytes"
"github.com/TykTechnologies/graphql-go-tools/v2/pkg/lexer/position"
)
// InlineFragment
// example:
//
// ... on User {
// friends {
// count
// }
// }
type InlineFragment struct {
Spread position.Position // ...
TypeCondition TypeCondition // on NamedType, e.g. on User
HasDirectives bool
Directives DirectiveList // optional, e.g. @foo
SelectionSet int // optional, e.g. { nextField }
HasSelections bool
IsOfTheSameType bool
}
func (d *Document) CopyInlineFragment(ref int) int {
var directives DirectiveList
var selectionSet int
if d.InlineFragments[ref].HasDirectives {
directives = d.CopyDirectiveList(d.InlineFragments[ref].Directives)
}
if d.InlineFragments[ref].HasSelections {
selectionSet = d.CopySelectionSet(d.InlineFragments[ref].SelectionSet)
}
return d.AddInlineFragment(InlineFragment{
TypeCondition: d.InlineFragments[ref].TypeCondition, // Value type; doesn't need to be copied.
HasDirectives: d.InlineFragments[ref].HasDirectives,
Directives: directives,
SelectionSet: selectionSet,
HasSelections: d.InlineFragments[ref].HasSelections,
})
}
func (d *Document) InlineFragmentTypeConditionName(ref int) ByteSlice {
if d.InlineFragments[ref].TypeCondition.Type == -1 {
return nil
}
return d.Input.ByteSlice(d.Types[d.InlineFragments[ref].TypeCondition.Type].Name)
}
func (d *Document) InlineFragmentTypeConditionNameString(ref int) string {
return unsafebytes.BytesToString(d.InlineFragmentTypeConditionName(ref))
}
func (d *Document) InlineFragmentHasTypeCondition(ref int) bool {
return d.InlineFragments[ref].TypeCondition.Type != -1
}
func (d *Document) InlineFragmentHasDirectives(ref int) bool {
return len(d.InlineFragments[ref].Directives.Refs) != 0
}
func (d *Document) InlineFragmentSelections(ref int) []int {
if !d.InlineFragments[ref].HasSelections {
return nil
}
return d.SelectionSets[d.InlineFragments[ref].SelectionSet].SelectionRefs
}
func (d *Document) AddInlineFragment(fragment InlineFragment) int {
d.InlineFragments = append(d.InlineFragments, fragment)
return len(d.InlineFragments) - 1
}
func (d *Document) InlineFragmentIsOfTheSameType(ref int) bool {
return d.InlineFragments[ref].IsOfTheSameType
}
func (d *Document) InlineFragmentSelectionSet(ref int) (selectionSetRef int, ok bool) {
if !d.InlineFragments[ref].HasSelections {
return InvalidRef, false
}
return d.InlineFragments[ref].SelectionSet, true
}
func (d *Document) InlineFragmentDirectives(ref int) []int {
return d.InlineFragments[ref].Directives.Refs
}