|
| 1 | +// Package selections is for internal use to share selection context between |
| 2 | +// the execution engine and the public graphql package without creating an |
| 3 | +// import cycle. |
| 4 | +// |
| 5 | +// The execution layer stores the flattened child selection set for the field |
| 6 | +// currently being resolved. The public API converts this into user-friendly |
| 7 | +// helpers (SelectedFieldNames, etc.). |
| 8 | +package selections |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "sync" |
| 13 | + |
| 14 | + "github.com/graph-gophers/graphql-go/internal/exec/selected" |
| 15 | +) |
| 16 | + |
| 17 | +// ctxKey is an unexported unique type used as context key. |
| 18 | +type ctxKey struct{} |
| 19 | + |
| 20 | +// Lazy holds raw selections and computes the flattened, deduped name list once on demand. |
| 21 | +type Lazy struct { |
| 22 | + raw []selected.Selection |
| 23 | + once sync.Once |
| 24 | + names []string |
| 25 | + set map[string]struct{} |
| 26 | +} |
| 27 | + |
| 28 | +// Names returns the deduplicated child field names computing them once. |
| 29 | +func (l *Lazy) Names() []string { |
| 30 | + if l == nil { |
| 31 | + return nil |
| 32 | + } |
| 33 | + l.once.Do(func() { |
| 34 | + seen := make(map[string]struct{}, len(l.raw)) |
| 35 | + ordered := make([]string, 0, len(l.raw)) |
| 36 | + for _, s := range l.raw { |
| 37 | + switch s := s.(type) { |
| 38 | + case *selected.SchemaField: |
| 39 | + name := s.Name |
| 40 | + if len(name) >= 2 && name[:2] == "__" { |
| 41 | + continue |
| 42 | + } |
| 43 | + if _, ok := seen[name]; !ok { |
| 44 | + seen[name] = struct{}{} |
| 45 | + ordered = append(ordered, name) |
| 46 | + } |
| 47 | + case *selected.TypeAssertion: |
| 48 | + collectFromTypeAssertion(&ordered, seen, s.Sels) |
| 49 | + case *selected.TypenameField: |
| 50 | + continue |
| 51 | + } |
| 52 | + } |
| 53 | + l.names = ordered |
| 54 | + l.set = seen |
| 55 | + }) |
| 56 | + // Return a copy to keep internal slice immutable to callers. |
| 57 | + out := make([]string, len(l.names)) |
| 58 | + copy(out, l.names) |
| 59 | + return out |
| 60 | +} |
| 61 | + |
| 62 | +// Has reports if a field name is in the selection list. |
| 63 | +func (l *Lazy) Has(name string) bool { |
| 64 | + if l == nil { |
| 65 | + return false |
| 66 | + } |
| 67 | + if l.set == nil { // ensure computed |
| 68 | + _ = l.Names() |
| 69 | + } |
| 70 | + _, ok := l.set[name] |
| 71 | + return ok |
| 72 | +} |
| 73 | + |
| 74 | +// collectFromTypeAssertion flattens selections under a type assertion fragment. |
| 75 | +func collectFromTypeAssertion(dst *[]string, seen map[string]struct{}, sels []selected.Selection) { |
| 76 | + for _, s := range sels { |
| 77 | + switch s := s.(type) { |
| 78 | + case *selected.SchemaField: |
| 79 | + name := s.Name |
| 80 | + if len(name) >= 2 && name[:2] == "__" { |
| 81 | + continue |
| 82 | + } |
| 83 | + if _, ok := seen[name]; !ok { |
| 84 | + seen[name] = struct{}{} |
| 85 | + *dst = append(*dst, name) |
| 86 | + } |
| 87 | + case *selected.TypeAssertion: |
| 88 | + collectFromTypeAssertion(dst, seen, s.Sels) |
| 89 | + case *selected.TypenameField: |
| 90 | + continue |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// With stores a lazy wrapper for selections in the context. |
| 96 | +func With(ctx context.Context, sels []selected.Selection) context.Context { |
| 97 | + if len(sels) == 0 { |
| 98 | + return ctx |
| 99 | + } |
| 100 | + return context.WithValue(ctx, ctxKey{}, &Lazy{raw: sels}) |
| 101 | +} |
| 102 | + |
| 103 | +// FromContext retrieves the lazy wrapper (may be nil). |
| 104 | +func FromContext(ctx context.Context) *Lazy { |
| 105 | + v, _ := ctx.Value(ctxKey{}).(*Lazy) |
| 106 | + return v |
| 107 | +} |
0 commit comments