-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f36e90
commit fd6a741
Showing
5 changed files
with
145 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Kind, NameNode } from 'graphql'; | ||
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin/'; | ||
|
||
const RULE_ID: string = 'no-more-than-2-fields'; | ||
|
||
export const rule: GraphQLESLintRule = { | ||
meta: { | ||
type: 'suggestion', | ||
hasSuggestions: true, | ||
docs: { | ||
description: 'Object should have no more than 2 fields.', | ||
category: 'Operations', | ||
recommended: true, | ||
examples: [ | ||
{ | ||
title: 'Incorrect', | ||
code: /* GraphQL */ ` | ||
query getSA($recoridId) { | ||
uiapi { | ||
query { | ||
ServiceAppointment() { | ||
edges { | ||
node { | ||
Id | ||
Subject { | ||
value | ||
} | ||
AccountId { | ||
value | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
} | ||
] | ||
}, | ||
messages: { | ||
[RULE_ID]: 'Field number should not exceed 2!' | ||
}, | ||
schema: [] | ||
}, | ||
create(context: GraphQLESLintRuleContext) { | ||
function checkNode(usedFields: Set<string>, node: NameNode): void { | ||
const fieldName = node.value; | ||
|
||
usedFields.add(fieldName); | ||
if (usedFields.size === 3) { | ||
context.report({ | ||
node, | ||
messageId: RULE_ID | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
SelectionSet(node) { | ||
if (node.type === 'SelectionSet') { | ||
const rawNode = node.rawNode(); | ||
// Traverse backwards to find 'node' from GraphQL ASTNode | ||
const parentNode = node.parent.rawNode(); | ||
|
||
if (parentNode.kind === 'Field' && parentNode.name.value === 'node') { | ||
const set = new Set<string>(); | ||
|
||
for (const selection of rawNode.selections) { | ||
if (selection.kind === Kind.FIELD) { | ||
checkNode(set, selection.alias || selection.name); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
import { rule } from '../../../lib/rules/no-more-than-2-fields'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@graphql-eslint/eslint-plugin', | ||
parserOptions: { | ||
graphQLConfig: {} | ||
} | ||
}); | ||
|
||
ruleTester.run('@salesforce/lwc-mobile/no-more-than-2-fields', rule as any, { | ||
valid: [ | ||
{ | ||
code: /* GraphQL */ ` | ||
query getSA { | ||
uiapi { | ||
query { | ||
ServiceAppointment { | ||
edges { | ||
node { | ||
Id | ||
Subject { | ||
value | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: /* GraphQL */ ` | ||
query getSA($recId: string) { | ||
uiapi { | ||
query { | ||
ServiceAppointment( | ||
where: { | ||
and: [{ AccountId: { eq: "1" } }, { Status: { eq: "New" } }] | ||
} | ||
first: 2 | ||
) { | ||
edges { | ||
node { | ||
Id | ||
Subject { | ||
value | ||
} | ||
AccountId { | ||
value | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
errors: [{ messageId: 'no-more-than-2-fields' }] | ||
} | ||
] | ||
}); |