Skip to content

Commit

Permalink
allow to config naming-convention for Relay fragment convention `<m…
Browse files Browse the repository at this point in the history
…odule_name>_<property_name>` via `requiredPattern` option (#2838)

* aa

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Merge branch 'master' into use-search-params

* Update .changeset/polite-impalas-float.md

* Apply suggestions from code review
  • Loading branch information
dimaMachina authored Dec 9, 2024
1 parent 0b5bf61 commit 10b9975
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 197 deletions.
8 changes: 8 additions & 0 deletions .changeset/polite-impalas-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@graphql-eslint/eslint-plugin': minor
---

- allow to config `naming-convention` for Relay fragment convention `<module_name>_<property_name>`
via `requiredPattern` option

- replace `requiredPatterns: RegEx[]` by `requiredPattern: RegEx` option
12 changes: 7 additions & 5 deletions packages/plugin/src/rules/match-document-filename/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ const schemaOption = {
oneOf: [{ $ref: '#/definitions/asString' }, { $ref: '#/definitions/asObject' }],
} as const;

const caseSchema = {
enum: CASE_STYLES,
description: `One of: ${CASE_STYLES.map(t => `\`${t}\``).join(', ')}`,
};

const schema = {
definitions: {
asString: {
enum: CASE_STYLES,
description: `One of: ${CASE_STYLES.map(t => `\`${t}\``).join(', ')}`,
},
asString: caseSchema,
asObject: {
type: 'object',
additionalProperties: false,
minProperties: 1,
properties: {
style: { enum: CASE_STYLES },
style: caseSchema,
suffix: { type: 'string' },
prefix: { type: 'string' },
},
Expand Down
113 changes: 102 additions & 11 deletions packages/plugin/src/rules/naming-convention/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,48 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
},
],
},
{
name: 'requiredPattern with case style in prefix',
options: [
{
FragmentDefinition: {
style: 'PascalCase',
requiredPattern: /^(?<camelCase>.+?)_/,
},
},
],
code: /* GraphQL */ `
fragment myUser_UserProfileFields on User {
id
}
`,
parserOptions: {
graphQLConfig: {
schema: 'type User',
},
},
},
{
name: 'requiredPattern with case style in suffix',
options: [
{
FragmentDefinition: {
style: 'PascalCase',
requiredPattern: /_(?<snake_case>.+?)$/,
},
},
],
code: /* GraphQL */ `
fragment UserProfileFields_my_user on User {
id
}
`,
parserOptions: {
graphQLConfig: {
schema: 'type User',
},
},
},
],
invalid: [
{
Expand Down Expand Up @@ -446,15 +488,42 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
`,
options: (rule.meta.docs!.configOptions as any).operations,
errors: [
{ message: 'Query "TestQuery" should not have "Query" suffix' },
{ message: 'Query "QueryTest" should not have "Query" prefix' },
{ message: 'Query "GetQuery" should not have "Get" prefix' },
{ message: 'Mutation "TestMutation" should not have "Mutation" suffix' },
{ message: 'Mutation "MutationTest" should not have "Mutation" prefix' },
{ message: 'Subscription "TestSubscription" should not have "Subscription" suffix' },
{ message: 'Subscription "SubscriptionTest" should not have "Subscription" prefix' },
{ message: 'Fragment "TestFragment" should not have "Fragment" suffix' },
{ message: 'Fragment "FragmentTest" should not have "Fragment" prefix' },
{
message:
'Query "TestQuery" should not contain the forbidden pattern "/(query|mutation|subscription)$/i"',
},
{
message:
'Query "QueryTest" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
},
{
message:
'Query "GetQuery" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
},
{
message:
'Mutation "TestMutation" should not contain the forbidden pattern "/(query|mutation|subscription)$/i"',
},
{
message:
'Mutation "MutationTest" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
},
{
message:
'Subscription "TestSubscription" should not contain the forbidden pattern "/(query|mutation|subscription)$/i"',
},
{
message:
'Subscription "SubscriptionTest" should not contain the forbidden pattern "/^(query|mutation|subscription|get)/i"',
},
{
message:
'Fragment "TestFragment" should not contain the forbidden pattern "/(^fragment)|(fragment$)/i"',
},
{
message:
'Fragment "FragmentTest" should not contain the forbidden pattern "/(^fragment)|(fragment$)/i"',
},
],
},
{
Expand Down Expand Up @@ -536,17 +605,39 @@ ruleTester.run<RuleOptions>('naming-convention', rule, {
errors: 2,
},
{
name: 'requiredPatterns',
name: 'requiredPattern',
code: 'type Test { enabled: Boolean! }',
options: [
{
'FieldDefinition[gqlType.gqlType.name.value=Boolean]': {
style: 'camelCase',
requiredPatterns: [/^(is|has)/],
requiredPattern: /^(is|has)/,
},
},
],
errors: 1,
},
{
name: 'requiredPattern with case style in suffix',
options: [
{
FragmentDefinition: {
style: 'PascalCase',
requiredPattern: /_(?<camelCase>.+?)$/,
},
},
],
code: /* GraphQL */ `
fragment UserProfileFields on User {
id
}
`,
parserOptions: {
graphQLConfig: {
schema: 'type User',
},
},
errors: 1,
},
],
});
Loading

0 comments on commit 10b9975

Please sign in to comment.