-
Notifications
You must be signed in to change notification settings - Fork 2
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
5a8f00c
commit 3f36e90
Showing
4 changed files
with
1,042 additions
and
1 deletion.
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
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
|
||
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
|
||
} | ||
} | ||
] | ||
}); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
Oops, something went wrong.