File: selection.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,452 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (59 lines) | stat: -rw-r--r-- 2,161 bytes parent folder | download
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
}