Skip to content

Commit

Permalink
Add env option to required-fields rule config
Browse files Browse the repository at this point in the history
Allows .graphql and .gql literal files to be linted with the required-fields rule.
  • Loading branch information
Justin Schulz committed Jul 12, 2017
1 parent 26c63eb commit 6160158
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change log

### vNEXT
- Add env config option for required-fields rule [Justin Schulz](https://github.com/PepperTeasdale) in [#75](https://github.com/apollographql/eslint-plugin-graphql/pull/75)

### v1.0.0
- Fix template env for older runtimes (eg. node 5 and earlier) [Justin Schulz](https://github.com/PepperTeasdale) in [#73](https://github.com/apollographql/eslint-plugin-graphql/pull/73)
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ The full list of available validators is:
- `FragmentsOnCompositeTypes`
- `KnownArgumentNames`
- `KnownDirectives` (*disabled by default in `relay`*)
- `KnownFragmentNames` (*disabled by default in `apollo`, `lokka`, and `relay`*)
- `KnownFragmentNames` (*disabled by default in all envs*)
- `KnownTypeNames`
- `LoneAnonymousOperation`
- `NoFragmentCycles`
- `NoUndefinedVariables` (*disabled by default in `relay`*)
- `NoUnusedFragments` (*disabled by default in `apollo`, `lokka`, and `relay`*)
- `NoUnusedFragments` (*disabled by default in all envs*)
- `NoUnusedVariables`
- `OverlappingFieldsCanBeMerged`
- `PossibleFragmentSpreads`
Expand Down Expand Up @@ -347,7 +347,7 @@ query ViewerName {
}
```

The rule is defined as `graphql/required-fields` and requires a `schema` and `requiredFields`, with an optional `tagName`.
The rule is defined as `graphql/required-fields` and requires a `schema` and `requiredFields`, with an optional `tagName` and `env`.

```js
// In a file called .eslintrc.js
Expand All @@ -356,7 +356,8 @@ module.exports = {
'graphql/required-fields': [
'error',
{
schemaJsonFilepath: require('./schema.json'),
env: 'apollo',
schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
requiredFields: ['uuid'],
},
],
Expand Down
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ const rules = {
additionalProperties: false,
properties: {
...defaultRuleProperties,
env: {
enum: [
'lokka',
'relay',
'apollo',
'literal',
],
},
requiredFields: {
type: 'array',
items: {
Expand Down
15 changes: 14 additions & 1 deletion test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,20 @@ const requiredFieldsTestCases = {
invalid: values(namedOperationsValidatorCases).map(({fail: code, errors}) => ({options, parserOptions, code, errors})),
});

// Validate the required-fields rule
// Validate the required-fields rule with env specified
options = [{
schemaJson,
env: 'apollo',
tagName: 'gql',
requiredFields: ['id'],
}];

ruleTester.run('testing required-fields rule', rules['required-fields'], {
valid: requiredFieldsTestCases.pass.map((code) => ({options, parserOptions, code})),
invalid: requiredFieldsTestCases.fail.map(({code, errors}) => ({options, parserOptions, code, errors})),
});

// Validate required-fields without optional env argument
options = [{
schemaJson,
tagName: 'gql',
Expand Down

0 comments on commit 6160158

Please sign in to comment.