Skip to content

Handle spread fragments for the required fields #300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### vNEXT

- _Nothing yet!_
- Handle spread fragments for the required fields. [PR #300](https://github.com/apollographql/eslint-plugin-graphql/pull/300) by [Arnav Mishra](https://github.com/arnmishra).

### v4.0.0

Expand Down
24 changes: 24 additions & 0 deletions src/customGraphQLValidationRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ function getFieldWasRequestedOnNode(node, field) {
});
}

function getNodeHasSpreadFragment(node) {
return node.selectionSet.selections.some(n => {
return n.kind === "FragmentSpread"
});
}

function fieldAvailableOnType(type, field) {
if (!type) {
return false;
Expand All @@ -38,6 +44,12 @@ export function RequiredFields(context, options) {
const type = context.getType();

if (fieldAvailableOnType(type, field)) {
// If there is a spread fragment, that spread fragment will already be checked for every field.
const nodeHasSpreadFragment = getNodeHasSpreadFragment(node);
if (nodeHasSpreadFragment) {
return
}

const fieldWasRequested = getFieldWasRequestedOnNode(node, field);
if (!fieldWasRequested) {
context.reportError(
Expand All @@ -58,6 +70,12 @@ export function RequiredFields(context, options) {
const type = context.getType();

if (fieldAvailableOnType(type, field)) {
// If there is a spread fragment, that spread fragment will already be checked for every field.
const nodeHasSpreadFragment = getNodeHasSpreadFragment(node);
if (nodeHasSpreadFragment) {
return
}

// First, check the selection set on this inline fragment
if (node.selectionSet && getFieldWasRequestedOnNode(node, field)) {
return true;
Expand Down Expand Up @@ -118,6 +136,12 @@ export function RequiredFields(context, options) {

requiredFields.forEach(field => {
if (fieldAvailableOnType(def.type, field)) {
// If there is a spread fragment, that spread fragment will already be checked for every field.
const nodeHasSpreadFragment = getNodeHasSpreadFragment(node);
if (nodeHasSpreadFragment) {
return
}

const fieldWasRequested = getFieldWasRequestedOnNode(node, field);
if (!fieldWasRequested) {
context.reportError(
Expand Down
10 changes: 1 addition & 9 deletions test/validationRules/required-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const requiredFieldsTestCases = {
"const x = gql`fragment Foo on FooBar { id, hello, foo }`",
"const x = gql`fragment Id on Node { id ... on NodeA { fieldA } }`",
"const x = gql`query { nodes { id ... on NodeA { fieldA } } }`",
"const x = gql`query { greetings { hello ...GreetingsFragment} }`"
],
fail: [
{
Expand Down Expand Up @@ -48,15 +49,6 @@ const requiredFieldsTestCases = {
}
]
},
{
code: 'const x = gql`query { greetings { hello ...GreetingsFragment} }`',
errors: [
{
message: `'id' field required on 'greetings'`,
type: 'TaggedTemplateExpression',
},
],
},
{
code: 'const x = gql`fragment Name on Greetings { hello }`',
errors: [
Expand Down