在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):apollographql/eslint-plugin-graphql开源软件地址(OpenSource Url):https://github.com/apollographql/eslint-plugin-graphql开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):eslint-plugin-graphql
An ESLint plugin that checks tagged query strings inside JavaScript, or queries inside
If you want to lint your GraphQL schema, rather than queries, check out cjoudrey/graphql-schema-linter. Importing schema JSONYou'll need to import your introspection query result or the schema as a string in the Schema Language format. This can be done if you define your ESLint config in a JS file. Retrieving a remote GraphQL schemagraphql-cli provides a apollo-codegen also provides an introspect-schema command that can get your remote schemas as well Common optionsAll of the rules provided by this plugin have a few options in common. There are examples of how to use these with Apollo, Relay, Lokka, FraQL and literal files further down.
You also have to specify a schema. You can either do it using one of these options:
Alternatively, you can use a .graphqlconfig file instead of the above three options. If you do there's one more option to know about:
There's an example on how to use a Identity template literal tagThis plugin relies on GraphQL queries being prefixed with a special tag. In Relay and Apollo this is done often, but some other clients take query strings without a tag. In this case, import gql from "fake-tag";
const QUERY_VIEWER_NAME = gql`
query ViewerName {
viewer {
name
}
}
`; Fake tags won’t be necessary once GraphQL literal filesThis plugin also lints GraphQL literal files ending on eslint . --ext .js --ext .gql --ext .graphql Example config for Apollo// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'apollo',
// Import your schema JSON here
schemaJson: require('./schema.json'),
// OR provide absolute path to your schema JSON (but not if using `eslint --cache`!)
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),
// tagName is gql by default
}]
},
plugins: [
'graphql'
]
} Example config for Relay// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'relay',
// Import your schema JSON here
schemaJson: require('./schema.json'),
// OR provide absolute path to your schema JSON (but not if using `eslint --cache`!)
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),
// tagName is set for you to Relay.QL
}]
},
plugins: [
'graphql'
]
} Example config for Lokka// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'lokka',
// Import your schema JSON here
schemaJson: require('./schema.json'),
// OR provide absolute path to your schema JSON (but not if using `eslint --cache`!)
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),
// Optional, the name of the template tag, defaults to 'gql'
tagName: 'gql'
}]
},
plugins: [
'graphql'
]
} Example config for FraQL// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'fraql',
// Import your schema JSON here
schemaJson: require('./schema.json'),
// OR provide absolute path to your schema JSON
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),
// Optional, the name of the template tag, defaults to 'gql'
tagName: 'gql'
}]
},
plugins: [
'graphql'
]
} Example config for literal graphql files// In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'literal',
// Import your schema JSON here
schemaJson: require('./schema.json'),
// OR provide absolute path to your schema JSON (but not if using `eslint --cache`!)
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),
// tagName is set automatically
}]
},
plugins: [
'graphql'
]
} Additional Schemas or TagsThis plugin can be used to validate against multiple schemas by identifying them with different tags. This is useful for applications interacting with multiple GraphQL systems. Additional schemas can simply be appended to the options list: module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
tagName: 'FirstGQL',
schemaJson: require('./schema-first.json')
}, {
env: 'relay',
tagName: 'SecondGQL',
schemaJson: require('./schema-second.json')
}]
},
plugins: [
'graphql'
]
} .graphqlconfigExample config when usingIf you have // In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'literal'
// no need to specify schema here, it will be automatically determined using .graphqlconfig
}]
},
plugins: [
'graphql'
]
} In case you use additional schemas, specify module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
tagName: 'FirstGQL',
projectName: 'FirstGQLProject'
}, {
env: 'relay',
tagName: 'SecondGQL',
projectName: 'SecondGQLProject'
}]
},
plugins: [
'graphql'
]
} Selecting Validation RulesGraphQL validation rules can be configured in the eslint rule configuration using the The module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
validators: 'all',
tagName: 'FirstGQL',
schemaJson: require('./schema-first.json')
}, {
validators: ['FieldsOnCorrectType'],
tagName: 'SecondGQL',
schemaJson: require('./schema-second.json')
}]
},
plugins: [
'graphql'
]
} The full list of available validators is:
Named Operations Validation RuleThe Named Operation rule validates that all operations are named. Naming operations is valuable for including in server-side logs and debugging. Pass
Fail
The rule is defined as // In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
schemaJson: require('./schema.json'),
}],
"graphql/named-operations": ['warn', {
schemaJson: require('./schema.json'),
}],
},
plugins: [
'graphql'
]
} Required Fields Validation RuleThe 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. Pass
Pass
Fail
The rule is defined as // In a file called .eslintrc.js
module.exports = {
rules: {
'graphql/required-fields': [
'error',
{
env: 'apollo',
schemaJsonFilepath: path.resolve(__dirname, './schema.json'),
requiredFields: ['uuid'],
},
],
},
plugins: [
'graphql'
]
} Capitalization of a first letter of a Type nameThis rule enforces that first letter of types is capitalized Pass
Fail
The rule is defined as // In a file called .eslintrc.js
module.exports = {
parser: "babel-eslint",
rules: {
"graphql/template-strings": ['error', {
env: 'apollo',
schemaJson: require('./schema.json'),
}],
"graphql/capitalized-type-name": ['warn', {
schemaJson: require('./schema.json'),
}],
},
plugins: [
'graphql'
]
} No Deprecated Fields Validation RuleThe No Deprecated Fields rule validates that no deprecated fields are part of the query. This is useful to discover fields that have been marked as deprecated and shouldn't be used. Fail
The rule is defined as // In a file called .eslintrc.js
module.exports = {
rules: {
'graphql/no-deprecated-fields': [
'error',
{
env: 'relay',
schemaJson: require('./schema.json')
},
],
},
plugins: [
'graphql'
]
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论