Skip to content

Commit

Permalink
first mobile gql sample
Browse files Browse the repository at this point in the history
  • Loading branch information
haifeng-li-at-salesforce committed May 8, 2024
1 parent 3f36e90 commit fd6a741
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 167 deletions.
6 changes: 1 addition & 5 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
{
"root": true,
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/stylistic"
],
"extends": ["eslint:recommended"],
"env": {
"jest": true,
"node": true
Expand Down
106 changes: 0 additions & 106 deletions lib/rules/no-more-than-10-fields.ts

This file was deleted.

79 changes: 79 additions & 0 deletions lib/rules/no-more-than-2-fields.ts
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);
}
}
}
}
}
};
}
};
56 changes: 0 additions & 56 deletions test/lib/rules/no-more-than-10-fields.spec.ts

This file was deleted.

65 changes: 65 additions & 0 deletions test/lib/rules/no-more-than-2-fields.ts
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' }]
}
]
});

0 comments on commit fd6a741

Please sign in to comment.