Skip to content

Undeclared variable in fragment not used by operation #322

@xiongtx

Description

@xiongtx

When multiple queries are declared in a query document but the chosen operation doesn't reference a schema that requires a variable, there is a parse error.

Given the following schema, which we'll call schema:

{:objects
 {:Business {:fields {:name {:type String}
                      :owners {:type (list :Owner)}}}
  :Owner {:fields {:name {:type String}
                   :owned_businesses {:type (list :Business)
                                      :args {:location {:type String}}
                                      :resolve :placeholder}}}}

 :queries
 {:owners {:type (list :Owner)
           :resolve :placeholder}
  :businesses {:type (list :Business)
               :args {:location {:type String}}
               :resolve :placeholder}}}

And query document, which we'll call query-doc:

query BusinessesQuery($location: String) {
 businesses(location: $location) {
  owners {
   ...OwnerFields
  }
 }
}

fragment OwnerFields on Owner {
 name
 owned_businesses(location: $location)
}

query OwnerNamesQuery {
 owners {
  name
 }
}

This parses fine:

(require '[com.walmartlabs.lacinia.parser :as lacinia-parser])

(lacinia-parser/parse-query schema query-doc "BusinessesQuery")

But this throws an exception:

(lacinia-parser/parse-query schema query-doc "OwnerNamesQuery")
Argument references undeclared variable `location'.
   {:locations [{:line 10, :column 2}],
    :field :Owner/owned_businesses,
    :argument :Owner/owned_businesses.location,
    :unknown-variable :location,
    :declared-variables ()}

The problem appears to be that fragments are included without considering whether the chosen operation makes use of them.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions