checkGraphQLQuery parses gh's query and validates it against the schema. It returns a usable operation even when validation finds non-cyclic problems, so the response can still be checked where the query is valid.
(schema *ast.Schema, query, operation string)
| 28 | // returns a usable operation even when validation finds non-cyclic problems, |
| 29 | // so the response can still be checked where the query is valid. |
| 30 | func checkGraphQLQuery(schema *ast.Schema, query, operation string) (*ast.OperationDefinition, []string) { |
| 31 | doc, err := parser.ParseQuery(&ast.Source{Name: "query", Input: query}) |
| 32 | if err != nil { |
| 33 | return nil, []string{graphQLErrorText(err)} |
| 34 | } |
| 35 | |
| 36 | rules := validatorrules.NewDefaultRules() |
| 37 | // GitHub accepts fragments on different types that ask for one field name |
| 38 | // with different types, such as state on both Issue and PullRequest. gh |
| 39 | // relies on that; retain other overlapping-field checks. |
| 40 | var problems []string |
| 41 | cyclic := false |
| 42 | for _, e := range validator.ValidateWithRules(schema, doc, rules) { |
| 43 | if exclusiveTypeConflict(schema, doc, e) { |
| 44 | continue |
| 45 | } |
| 46 | if e.Rule == validatorrules.NoFragmentCyclesRule.Name { |
| 47 | cyclic = true |
| 48 | } |
| 49 | problems = append(problems, graphQLErrorText(e)) |
| 50 | } |
| 51 | if cyclic { |
| 52 | return nil, dedupe(problems) |
| 53 | } |
| 54 | |
| 55 | if len(doc.Operations) == 0 { |
| 56 | return nil, append(problems, "the query has no operation") |
| 57 | } |
| 58 | if operation == "" && len(doc.Operations) > 1 { |
| 59 | return nil, append(problems, "operationName is required when the document has multiple operations") |
| 60 | } |
| 61 | op := doc.Operations.ForName(operation) |
| 62 | if op == nil { |
| 63 | return nil, append(problems, fmt.Sprintf("the query has no operation named %q", operation)) |
| 64 | } |
| 65 | return op, dedupe(problems) |
| 66 | } |
| 67 | |
| 68 | func exclusiveTypeConflict(schema *ast.Schema, doc *ast.QueryDocument, problem *gqlerror.Error) bool { |
| 69 | if problem.Rule != validatorrules.OverlappingFieldsCanBeMergedRule.Name || |
no test coverage detected