Skip to content
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

required-fields: inline fragments without a field ancestor #240

Merged
merged 4 commits into from
Dec 27, 2019
Merged
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### vNEXT

- Update the `required-fields` rule to handle inline fragments without field ancestors. [PR #240](https://github.com/apollographql/eslint-plugin-graphql/pull/240) by [Henry Q. Dineen](https://github.com/henryqdineen)

### v3.1.0

- Fix an issue that caused `graphql/required-fields` to throw on non-existent field references. [PR #231](https://github.com/apollographql/eslint-plugin-graphql/pull/231) by [Vitor Balocco](https://github.com/vitorbal)
Expand Down
22 changes: 13 additions & 9 deletions src/customGraphQLValidationRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ export function RequiredFields(context, options) {

const ancestorClone = [...ancestors];

let nearestField;
let nearestFieldOrExecutableDefinition;
let nextAncestor;

// Now, walk up the ancestors, until you see a field.
while (!nearestField) {
// Now, walk up the ancestors, until you see a field or executable definition.
while (!nearestFieldOrExecutableDefinition) {
nextAncestor = ancestorClone.pop();

if (
Expand All @@ -79,19 +79,23 @@ export function RequiredFields(context, options) {
return true;
}

if (nextAncestor.kind === "Field") {
nearestField = nextAncestor;
if (
nextAncestor.kind === "Field" ||
nextAncestor.kind === "FragmentDefinition" ||
nextAncestor.kind === "OperationDefiniton"
) {
nearestFieldOrExecutableDefinition = nextAncestor;
}
}

// If we never found a field, the query is malformed
if (!nearestField) {
// If we never found a field or executable definition, the query is malformed
if (!nearestFieldOrExecutableDefinition) {
throw new Error(
"Inline fragment found inside document without a parent field."
"Inline fragment found inside document without a parent field, fragment definition, or operation definition."
);
}

// We found a field, but we never saw the field we were looking for in
// We found a field or executable definition, but we never saw the field we were looking for in
// the intermediate selection sets.
context.reportError(
new GraphQLError(
Expand Down
14 changes: 14 additions & 0 deletions test/validationRules/required-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const requiredFieldsTestCases = {
"const x = gql`query { greetings { id, hello, hi } }`",
"const x = gql`query { greetings { id, hello, foo } }`",
"const x = gql`query { greetings { id ... on Greetings { hello } } }`",
"const x = gql`query { greetingOrStory { ... on Greetings { id hello } } }`",
"const x = gql`query { greetingOrStory { id ... on Greetings { hello } } }`",
"const x = gql`fragment Name on GreetingOrStory { ... on Greetings { id hello } }`",
"const x = gql`fragment Name on GreetingOrStory { id ... on Greetings { hello } }`",
"const x = gql`fragment Name on Greetings { id hello }`",
"const x = gql`fragment Foo on FooBar { id, hello, foo }`",
"const x = gql`fragment Id on Node { id ... on NodeA { fieldA } }`",
Expand Down Expand Up @@ -82,6 +86,16 @@ const requiredFieldsTestCases = {
}
]
},
{
code:
"const x = gql`fragment Name on GreetingOrStory { ... on Greetings { hello } }`",
errors: [
{
message: `'id' field required on '... on Greetings'`,
type: "TaggedTemplateExpression"
}
]
},
]
};

Expand Down