Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a rule to capitalize named operations #276

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,50 @@ module.exports = {
]
}
```

### Capitalization of a first letter of Named Operation rule

The Capitalize Named Operation rule validates that all existing operation names are capitalized.

**Pass**
```
query FetchUsername {
viewer {
name
}
}
```

**Fail**
```
query fetchUsername {
viewer {
name
}
}
```

The rule is defined as `graphql/capitalized-named-operations`.

```js
// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
schemaJson: require('./schema.json'),
}],
"graphql/capitalized-named-operations": ['warn', {
schemaJson: require('./schema.json'),
}],
},
plugins: [
'graphql'
]
}
```

### Required Fields Validation Rule

The Required Fields rule validates that any specified required field is part of the query, but only if that field is available in schema. This is useful to ensure that query results are cached properly in the client.
Expand Down
12 changes: 12 additions & 0 deletions src/customGraphQLValidationRules.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import { GraphQLError, getNamedType } from "graphql";

export function OperationNamesMustBeCapitalized(context) {
return {
OperationDefinition(node) {
if (node.name && node.name.value && (node.name.value[0] == node.name.value[0].toLowerCase())) {
context.reportError(
new GraphQLError("All operations must be capitalized", [node])
);
}
}
};
}

export function OperationsMustHaveNames(context) {
return {
OperationDefinition(node) {
Expand Down
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ export const rules = {
);
}
},
"capitalized-named-operations": {
meta: {
schema: {
type: "array",
items: {
additionalProperties: false,
properties: { ...defaultRuleProperties },
...schemaPropsExclusiveness
}
}
},
create: context => {
return createRule(context, optionGroup =>
parseOptions(
{
validators: ["OperationNamesMustBeCapitalized"],
...optionGroup
},
context
)
);
}
},
"required-fields": {
meta: {
schema: {
Expand Down
41 changes: 41 additions & 0 deletions test/validationRules/capitalized-named-operations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { rules } from "../../src";
import schemaJson from "../schema.json";

import { ruleTester, parserOptions } from "../helpers";

const capitalizedNamedOperationsValidatorCases = {
pass: [
"const x = gql`query Test { sum(a: 1, b: 2) }`",
"const x = gql`query { sum(a: 1, b: 2) }`",
],
fail: [{
code: "const x = gql`query test { sum(a: 1, b: 2) }`",
errors: [
{
message: "All operations must be capitalized",
type: "TaggedTemplateExpression"
}
]
}]
};

// Validate the named-operations rule
const options = [
{
schemaJson,
tagName: "gql"
}
];
ruleTester.run("testing capitalized-named-operations rule", rules["capitalized-named-operations"], {
valid: capitalizedNamedOperationsValidatorCases.pass.map(code => ({
options,
parserOptions,
code
})),
invalid: capitalizedNamedOperationsValidatorCases.fail.map(({ code, errors }) => ({
options,
parserOptions,
code,
errors
}))
});
1 change: 1 addition & 0 deletions test/validationRules/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './named-operations';
import './capitalized-named-operations';
import './required-fields';
import './no-deprecated-fields';
import './capitalized-type-name';
Expand Down