Skip to content

Commit

Permalink
Merge pull request #13 from walmartlabs/better-error-reporting-for-fr…
Browse files Browse the repository at this point in the history
…agments

Throw exception for inline fragments with invalid type conditions.
  • Loading branch information
bcarrell committed Mar 24, 2017
2 parents 122dfbb + b7f1631 commit 68f6474
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
29 changes: 15 additions & 14 deletions src/com/walmartlabs/lacinia/parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -797,17 +797,21 @@
(let [defaults (default-node-map sel q-path)]
(with-exception-context (node-context defaults)
(let [m (reduce node-reducer defaults (rest (second sel)))
fragment-type (get schema (:type m))
concrete-types (expand-fragment-type-to-concrete-types fragment-type)
fragment-path-term (keyword "..." (-> m :type name))
inline-fragment (-> m
(assoc :selection-type :inline-fragment
:concrete-types concrete-types)
(update :directives #(convert-directives schema %)))]
(normalize-selections schema
inline-fragment
fragment-type
(-> m :query-path (conj fragment-path-term)))))))
type-name (:type m)
fragment-type (get schema type-name)]
(if (nil? fragment-type)
(throw-exception (format "Inline fragment has a type condition on unknown type %s."
(q type-name)))
(let [concrete-types (expand-fragment-type-to-concrete-types fragment-type)
fragment-path-term (keyword "..." (name type-name))
inline-fragment (-> m
(assoc :selection-type :inline-fragment
:concrete-types concrete-types)
(update :directives #(convert-directives schema %)))]
(normalize-selections schema
inline-fragment
fragment-type
(-> m :query-path (conj fragment-path-term)))))))))

(defmethod selection :fragmentSpread
[schema sel _type q-path]
Expand Down Expand Up @@ -975,6 +979,3 @@
(throw (ex-info "Failed to parse GraphQL query."
{:errors failures})))))]
(xform-query schema antlr-tree operation-name))))



13 changes: 11 additions & 2 deletions test/com/walmartlabs/lacinia_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,17 @@
:homePlanet "Tatooine"
:appears_in ["NEWHOPE" "EMPIRE" "JEDI"]}
:leia {:appears_in ["NEWHOPE" "EMPIRE" "JEDI"]}}}
(execute *schema* q nil nil))))
(let [q "query InvalidInlineFragment {
human(id: \"1001\") {
... on foo {
name
}
}
}"]
(is (= {:errors [{:message "Inline fragment has a type condition on unknown type `foo'."
:query-path [:human]
:locations [{:line 2 :column 31}]}]}
(execute *schema* q nil nil)))))

(deftest invalid-query
Expand Down Expand Up @@ -777,5 +788,3 @@
:query-path [:events :lookup]}]}
(execute schema q2 nil nil))
"should return error message"))))))


0 comments on commit 68f6474

Please sign in to comment.