collect lists the response keys a selection asks for on an object whose __typename is typename, merging the selections of fields that share a key. visiting guards against fragments that spread themselves, which GitHub would reject.
(sel ast.SelectionSet, typename string, visiting map[string]bool, keys *[]string, groups map[string][]*ast.Field)
| 111 | // visiting guards against fragments that spread themselves, which GitHub |
| 112 | // would reject. |
| 113 | func (t *trimmer) collect(sel ast.SelectionSet, typename string, visiting map[string]bool, keys *[]string, groups map[string][]*ast.Field) { |
| 114 | for _, item := range sel { |
| 115 | switch x := item.(type) { |
| 116 | case *ast.Field: |
| 117 | if skippedBy(x.Directives, t.vars) { |
| 118 | continue |
| 119 | } |
| 120 | if _, seen := groups[x.Alias]; !seen { |
| 121 | *keys = append(*keys, x.Alias) |
| 122 | } |
| 123 | groups[x.Alias] = append(groups[x.Alias], x) |
| 124 | case *ast.InlineFragment: |
| 125 | if !skippedBy(x.Directives, t.vars) && t.applies(x.TypeCondition, typename) { |
| 126 | t.collect(x.SelectionSet, typename, visiting, keys, groups) |
| 127 | } |
| 128 | case *ast.FragmentSpread: |
| 129 | def := t.doc.Fragments.ForName(x.Name) |
| 130 | if def == nil || visiting[x.Name] || skippedBy(x.Directives, t.vars) || !t.applies(def.TypeCondition, typename) { |
| 131 | continue |
| 132 | } |
| 133 | visiting[x.Name] = true |
| 134 | t.collect(def.SelectionSet, typename, visiting, keys, groups) |
| 135 | delete(visiting, x.Name) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // applies reports whether a fragment on cond applies to an object whose |
| 141 | // __typename is typename. Without a __typename, every fragment applies. |