graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

Variables don't seem to be validated

Open JakeDawkins opened this issue 4 years ago • 6 comments

We noticed recently that when we built a query and accidentally added a query variable, and then passed that variable as an argument to a field in an operation that was invalid, we didn't receive any kind of build errors, which made me wonder if arguments and variables are being properly validated, and I don't think they are.

I intend on looking into this deeper, but as a first pass, to reproduce what I'm talking about, you can add any argument you want in the keywords_query test here, and the test will not fail.

The example I used to reproduce this was

query searchQuery($criteria: extern!, $invalid_var: Float!) {
  search {
    transactions(criteria: $searchID, invalid_var: $invalid_var) {
      for
      status
    }
  }
}

Where invalid_var doesn't exist in the schema.

In the meantime, before I have the time to look into this, does anyone know if (and if so, where) these variables are being validated? Or are they not currently part of validation?

JakeDawkins avatar Feb 11 '21 19:02 JakeDawkins

There is very little query validation built into the crate — it is something that can be implemented, but it's work that nobody has done yet.

tomhoule avatar Feb 11 '21 21:02 tomhoule

@tomhoule I think we'd like to take a look at adding this! Would you be able to help review a PR? 😄

JakeDawkins avatar Feb 15 '21 03:02 JakeDawkins

As you probably noticed I sadly don't have free time to work on graphql-client at the moment, but I can definitely make time to review a PR with tests. So yes, that would be awesome :)

tomhoule avatar Feb 15 '21 07:02 tomhoule

I've been using eslint to validate my query documents. https://github.com/apollographql/eslint-plugin-graphql eslint --ext .graphql .

// .eslintrc.js
module.exports = {
  parser: "babel-eslint",
  rules: {
    "graphql/template-strings": [
      "error",
      {
        env: "literal",
        schemaJson: require("./schema.json"),
      },
    ],
  },
  plugins: ["graphql"],
};

ronanyeah avatar Jun 25 '21 21:06 ronanyeah

I also ran into this issue recently. I have to say it kinda shook my faith in the whole "Typed, correct" tagline...

What work needs to be done in order to do query validation?

samuela avatar Oct 27 '21 04:10 samuela

Query validation is part of the graphql spec. Someone would need to implement it.

tomhoule avatar Oct 27 '21 06:10 tomhoule