Skip to content

Commit

Permalink
implement no-aggregate-query-supported eslint rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-zhang-at-salesforce committed May 16, 2024
1 parent 9a12b87 commit d5a7449
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 7 deletions.
3 changes: 2 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export default tsEslint.config(
{
rules: {
strict: ['error', 'global'],
'@typescript-eslint/no-extra-non-null-assertion': 'off'
'@typescript-eslint/no-extra-non-null-assertion': 'off',
'@typescript-eslint/no-explicit-any':'off'
}
},
);
20 changes: 19 additions & 1 deletion src/configs/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,23 @@
import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';

export = {
plugins: ['@salesforce/lwc-mobile']
plugins: ['@salesforce/lwc-mobile', '@graphql-eslint'],
overrides: [
{
files: ['*.js'],
processor: '@graphql-eslint/graphql'
},
{
files: ['*.graphql'],
parser: '@graphql-eslint/eslint-plugin',

parserOptions: {
skipGraphQLConfig: true
},
rules: {
'@graphql-eslint/no-duplicate-fields': 'error',
'@salesforce/lwc-mobile/no-aggregate-query-supported': 'warn'
}
}
]
} satisfies ClassicConfig.Config;
2 changes: 1 addition & 1 deletion src/configs/recommended.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import type { ClassicConfig } from '@typescript-eslint/utils/ts-eslint';
export = {
extends: ['./configs/base'],
rules: {
'@salesforce/lwc-mobile/enforce-foo-bar': 'warn'
'@salesforce/lwc-mobile/enforce-foo-bar': 'error'
}
} satisfies ClassicConfig.Config;
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/

import type { Linter } from '@typescript-eslint/utils/ts-eslint';

import base from './configs/base.js';
import recommended from './configs/recommended.js';
import enforceFooBar from './rules/dummy/enforce-foo-bar.js';
import { rule as noAggregateQuerySupported } from './rules/graphql/no-aggregate-query-supported.js';

import { name, version } from '../package.json';

export = {
configs: {
base,
Expand All @@ -21,6 +22,7 @@ export = {
version
},
rules: {
'enforce-foo-bar': enforceFooBar
'enforce-foo-bar': enforceFooBar,
'no-aggregate-query-supported': noAggregateQuerySupported
}
} satisfies Linter.Plugin;
};
43 changes: 43 additions & 0 deletions src/rules/graphql/no-aggregate-query-supported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { FieldNode } from 'graphql';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '@graphql-eslint/eslint-plugin';

export const rule: GraphQLESLintRule = {
meta: {
type: 'suggestion',
docs: {
category: 'Operations',
description:
'Inform that aggregate operation in graphql query is not supported for mobile offline.'
// url:
},
messages: {
aggregateQueryNotSupported:
'Aggregate operation in graphql query is not supported for mobile offline'
},
schema: []
},

create(context: GraphQLESLintRuleContext) {
return {
Field(node) {
// report
if (
node.name.value !== 'aggregate' ||
node.parent?.parent === undefined ||
node.parent.parent.type !== 'Field'
) {
return;
}

const upperField = node.parent.parent as unknown as FieldNode;

if (upperField.name.value === 'uiapi') {
context.report({
node: node.name,
messageId: 'aggregateQueryNotSupported'
});
}
}
};
}
};
File renamed without changes.
40 changes: 40 additions & 0 deletions test/rules/graphql/no-aggregate-query-supported.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RuleTester } from '@typescript-eslint/rule-tester';
import { rule } from '../../../src/rules/graphql/no-aggregate-query-supported';

const ruleTester = new RuleTester({
parser: '@graphql-eslint/eslint-plugin',
parserOptions: {
graphQLConfig: {}
}
});

ruleTester.run('@salesforce/lwc-mobile/no-aggregate-query-supported', rule as any, {
valid: [],
invalid: [
{
code: /* GraphQL */ `
query AvgOpportunityExample {
uiapi {
aggregate {
Opportunity {
edges {
node {
aggregate {
Amount {
avg {
value
displayValue
}
}
}
}
}
}
}
}
}
`,
errors: [{ messageId: 'aggregateQueryNotSupported' }]
}
]
});

0 comments on commit d5a7449

Please sign in to comment.