cjoudrey / graphql-schema-linter

Validate GraphQL schema definitions against a set of rules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Option to Run Custom Rules Only

sh0stat opened this issue · comments

I would like to add a additional option which would allow custom rules to be execute without running the default rules also. Concern is that some of the new rules I created conflict with some of the default rules. I tried to execute with just "-p" and noticed that default rules are executed along with the custom rules.

couple of rules we have created is EMUN are snake_cased and mutation must contain a input and types are in Pascal Case.

I am willing to assist in making this happen.

Hello @sh0stat, thanks for opening this issue.

You should be able to specify the rules you want to run using the -r option.

You'll still want to include your custom rules using -p.

Hope I understood your question correctly!

Hi @cjoudrey ,
Yes that is correct. As it currently stands I am not able to run both -r and -p at the same time as it will only load the rules based on the -r flag and ignore the -p flag. Per documentation if I select the -p flag it will run all rules in the rules folder along with the rules set in the -p directory which may conflict with some of our conventions.

Hello @sh0stat, maybe this behaviour should be better explained in the README, but:

  • -p makes graphql-schema-linter aware of your custom rules.
  • By default, graphql-schema-linter will use all rules that it knows (base rules + custom rules).
  • When using -r, it will only use the specified rules.

What you want to do is:

-p path/to/your/rules.js
-r some-base-rule,another-base-rule,custom-rule,custom-rule-2

Hope this answers your question!

@cjoudrey,
I set up a test to see if both rules are loaded when the -p and -r flags are used. You can see in the below screenshots that I only get a error when I have a file in the rules directory and not the customRules directory. If you look at the left hand side you can see where each file is being loaded from. I removed all rules and just used two to test it out. When using the debugger I am able to see that both directories are loaded but when it gets to line 67 in the runners.js file it only returns one rule and that is the rule from the rules directory. Please take a look and let me know if I may have did something incorrect which is causing this issue.

Debugging with a rule that will fail on the schema being loaded from the rules directory "-r type-mutation-prefixed-pascal-case" "-p C:\GIT\g_test\graphql-schema-linter\lib\customRules\*.js"
image

Debugging with the same rule that should fail but being loaded from the customRules directory using the -p flag. As you can see the validation passes. "-r enum-values-have-descriptions" "-p C:\GIT\graphql\customRules\*.js"
image

Another way to see this is:

  • -p simply require(...) the source code containing the extra rules.
  • -r when specified will only run those rules, but when not specified, will run all rules that it finds (including the ones in extra files loaded using -p)

Suppose you want to run the rules enum-values-have-descriptions and type-mutation-prefixed-pascal-case then you'd want to do:

graphql-schema-linter -r enum-values-have-descriptions type-mutation-prefixed-pascal-case -p path/to/custom_rules/*.js

So, the -p is making graphql-schema-linter require the extra code and then -r is telling graphql-schema-linter to enable those rules.

If you omit the -r and only use -p, then you'll include your custom rules and graphql-schema-linter will run ALL rules it knows about (i.e. all of the built in rules AND the rules you specify).

Hope this answers your question. If I misunderstood, let me know. 😄

@cjoudrey ,
I think it was a complete misunderstanding on my part. So even when running the -p flag I still use the -r flag to pull in the rules that I want to be executed from the -p flag directory.

Thanks for your time.

No worries! I'm happy it's solved. 😄

Thanks for reaching out and do not hesitate to open an issue again if you run into issues.