diff --git a/CHANGELOG.md b/CHANGELOG.md index eae5b13..35baf16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### vNEXT - Updated `graphql` dependency to resolve test failures ([wording change](https://github.com/graphql/graphql-js/commit/ba401e154461bca5323ca9121c6dacaee10ebe88), no API change) [jnwng](https://github.com/jnwng) +- Add lint rule to enforce that required fields are specified. [rgoldfinger](https://github.com/rgoldfinger) in [#47](https://github.com/apollographql/eslint-plugin-graphql/pull/50) ### v0.7.0 - Add lint rule to enforce that operations have names [gauravmk](https://github.com/gauravmk) in [#47](https://github.com/apollographql/eslint-plugin-graphql/pull/47) diff --git a/README.md b/README.md index 7c17ba8..8768c19 100644 --- a/README.md +++ b/README.md @@ -263,7 +263,7 @@ query { } ``` -The rule is defined as `graphql/named-operations` and requires a `schema` and optional `tagName` +The rule is defined as `graphql/named-operations` and requires a `schema` and optional `tagName`. ```js // In a file called .eslintrc.js @@ -283,3 +283,86 @@ module.exports = { ] } ``` +### Required Fields Validation Rule + +The Required Fields rule validates that any specified required field is part of the query, but only if that field is available in schema. This is useful to ensure that query results are cached properly in the client. + +**Pass** +``` +// 'uuid' required + +schema { + query { + viewer { + uuid + name + } + } +} + +query ViewerName { + viewer { + name + uuid + } +} +``` + +**Pass** +``` +// 'uuid' required + +schema { + query { + viewer { + name + } + } +} + +query ViewerName { + viewer { + name + } +} +``` + +**Fail** +``` +// 'uuid' required + +schema { + query { + viewer { + uuid + name + } + } +} + +query ViewerName { + viewer { + name + } +} +``` + +The rule is defined as `graphql/required-fields` and requires a `schema` and `requiredFields`, with an optional `tagName`. + +```js +// In a file called .eslintrc.js +module.exports = { + rules: { + 'graphql/required-fields': [ + 'error', + { + schemaJsonFilepath: require('./schema.json'), + requiredFields: ['uuid'], + }, + ], + }, + plugins: [ + 'graphql' + ] +} +```