diff --git a/CHANGELOG.md b/CHANGELOG.md index 48ed3e5..1f87d6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index bc7b77f..1cef0ac 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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 @@ -356,7 +356,8 @@ module.exports = { 'graphql/required-fields': [ 'error', { - schemaJsonFilepath: require('./schema.json'), + env: 'apollo', + schemaJsonFilepath: path.resolve(__dirname, './schema.json'), requiredFields: ['uuid'], }, ], diff --git a/src/index.js b/src/index.js index 660e920..28b6996 100644 --- a/src/index.js +++ b/src/index.js @@ -161,6 +161,14 @@ const rules = { additionalProperties: false, properties: { ...defaultRuleProperties, + env: { + enum: [ + 'lokka', + 'relay', + 'apollo', + 'literal', + ], + }, requiredFields: { type: 'array', items: { diff --git a/test/makeRule.js b/test/makeRule.js index cc143e6..5049e99 100644 --- a/test/makeRule.js +++ b/test/makeRule.js @@ -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',