forked from graph-gophers/graphql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.go
More file actions
59 lines (54 loc) · 2.11 KB
/
selection.go
File metadata and controls
59 lines (54 loc) · 2.11 KB
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 graphql
import (
"context"
"sort"
"github.com/graph-gophers/graphql-go/internal/selections"
)
// SelectedFieldNames returns the set of immediate child field names selected
// on the value returned by the current resolver. It returns an empty slice
// when the current field's return type is a leaf (scalar / enum) or when the
// feature was disabled at schema construction via DisableFieldSelections.
// The returned slice is a copy and is safe for the caller to modify.
//
// It is intentionally simple and does not expose the internal AST. If more
// detailed information is needed in the future (e.g. arguments per child,
// nested trees) a separate API can be added without breaking this one.
//
// Notes:
// - Fragment spreads & inline fragments are flattened; the union of all
// possible child fields is returned (deduplicated, preserving first
// appearance order in the query document).
// - Field aliases are ignored; the original schema field names are returned.
// - Meta fields beginning with "__" (including __typename) are excluded.
func SelectedFieldNames(ctx context.Context) []string {
// If no selection info is present (leaf field or no child selections), return empty slice.
lazy := selections.FromContext(ctx)
if lazy == nil {
return []string{}
}
return lazy.Names()
}
// HasSelectedField returns true if the immediate child selection list contains
// the provided field name (case sensitive). It returns false for leaf return
// types and when DisableFieldSelections was used.
func HasSelectedField(ctx context.Context, name string) bool {
lazy := selections.FromContext(ctx)
if lazy == nil {
return false
}
return lazy.Has(name)
}
// SortedSelectedFieldNames returns the same data as SelectedFieldNames but
// sorted lexicographically for deterministic ordering scenarios (e.g. cache
// key generation). It will also return an empty slice when selections are
// disabled.
func SortedSelectedFieldNames(ctx context.Context) []string {
names := SelectedFieldNames(ctx)
if len(names) <= 1 {
return names
}
out := make([]string, len(names))
copy(out, names)
sort.Strings(out)
return out
}