Skip to content

Commit

Permalink
add mobile graphql test rule
Browse files Browse the repository at this point in the history
  • Loading branch information
haifeng-li-at-salesforce committed May 8, 2024
1 parent 5a8f00c commit 3f36e90
Show file tree
Hide file tree
Showing 4 changed files with 1,042 additions and 1 deletion.
106 changes: 106 additions & 0 deletions lib/rules/no-more-than-10-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Kind, NameNode } from 'graphql';

Check failure on line 1 in lib/rules/no-more-than-10-fields.ts

View workflow job for this annotation

GitHub Actions / Linting on Ubuntu with Node 18

'NameNode' is defined but never used

Check failure on line 1 in lib/rules/no-more-than-10-fields.ts

View workflow job for this annotation

GitHub Actions / Linting on Ubuntu with Node 20

'NameNode' is defined but never used
import { GraphQLESLintRule } from '@graphql-eslint/eslint-plugin/';
//import { GraphQLESLintRuleContext,} from '@graphql-eslint/eslint-plugin/';
const RULE_ID = 'no-more-than-10-fields';

export const rule: GraphQLESLintRule = {
meta: {
type: 'suggestion',
hasSuggestions: true,
docs: {
description: 'Object should have less than 11 fields.',
category: 'Operations',
recommended: true,
examples: [
{
title: 'Incorrect',
code: /* GraphQL */ `
query getSA($recoridId) {
uiapi {
query {
ServiceAppointment() {
edges {
node {
Id
Subject {
value
}
AccountId {
value
}
City {
value
}
Description {
value
}
OwnerId {
value
}
PostalCode {
value
}
SchedStartTime {
value
}
Status {
value
}
Street {
value
}
}
}
}
}
}
}
`
}
]
},
messages: {
[RULE_ID]: 'Field number should not exceed 10!'
},
schema: []
},
create(context) {
function checkNode(usedFields: Set<string>, node: Any): void {
const fieldName = node.value;

usedFields.add(fieldName);
if (usedFields.size === 10) {
context.report({
node,
messageId: RULE_ID,
suggest: [
{
desc: 'Reduce object fields to less than 10',
fix(fixer) {
return fixer.remove(node as any);

Check failure on line 80 in lib/rules/no-more-than-10-fields.ts

View workflow job for this annotation

GitHub Actions / Linting on Ubuntu with Node 18

Unexpected any. Specify a different type

Check failure on line 80 in lib/rules/no-more-than-10-fields.ts

View workflow job for this annotation

GitHub Actions / Linting on Ubuntu with Node 20

Unexpected any. Specify a different type
}
}
]
});
}
}

return {
SelectionSet(node) {
if (
node.parent.kind === 'Field' &&
node.parent.name.kind === 'Name' &&
node.parent.name.value === 'node'
) {
const set = new Set<string>();

for (const selection of node.selections) {
if (selection.kind === Kind.FIELD) {
checkNode(set, selection.alias || selection.name);
}
}
}
}
};
}
};
Loading

0 comments on commit 3f36e90

Please sign in to comment.