queryFields lists the response paths a query asks for, such as data.repository.issue.title. Fragments are flattened, as they are in the response, and nil means the query couldn't be read.
(query, operation string)
| 153 | // data.repository.issue.title. Fragments are flattened, as they are in the |
| 154 | // response, and nil means the query couldn't be read. |
| 155 | func queryFields(query, operation string) map[string]bool { |
| 156 | doc, err := parser.ParseQuery(&ast.Source{Name: "query", Input: query}) |
| 157 | if err != nil || len(doc.Operations) == 0 { |
| 158 | return nil |
| 159 | } |
| 160 | op := doc.Operations.ForName(operation) |
| 161 | if op == nil { |
| 162 | return nil |
| 163 | } |
| 164 | fields := map[string]bool{} |
| 165 | var walk func(prefix string, sel ast.SelectionSet, visiting map[string]bool) |
| 166 | walk = func(prefix string, sel ast.SelectionSet, visiting map[string]bool) { |
| 167 | for _, item := range sel { |
| 168 | switch x := item.(type) { |
| 169 | case *ast.Field: |
| 170 | path := prefix + "." + x.Alias |
| 171 | fields[path] = true |
| 172 | walk(path, x.SelectionSet, visiting) |
| 173 | case *ast.InlineFragment: |
| 174 | walk(prefix, x.SelectionSet, visiting) |
| 175 | case *ast.FragmentSpread: |
| 176 | def := doc.Fragments.ForName(x.Name) |
| 177 | if def == nil || visiting[x.Name] { |
| 178 | continue |
| 179 | } |
| 180 | visiting[x.Name] = true |
| 181 | walk(prefix, def.SelectionSet, visiting) |
| 182 | delete(visiting, x.Name) |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | walk("data", op.SelectionSet, map[string]bool{}) |
| 187 | return fields |
| 188 | } |
| 189 | |
| 190 | // onlyIn lists the fields in a that b doesn't ask for. A new object is listed |
| 191 | // once, without the fields inside it. |