Skip to content

Commit

Permalink
Merge pull request apollographql#154 from neoziro/add-fraql
Browse files Browse the repository at this point in the history
feat: add fraql env
  • Loading branch information
stubailo authored Nov 12, 2018
2 parents 42d353a + bdb32b8 commit 0ff1824
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
### v2.1.1
- Fix support for InlineFragments with the `required-fields` rule in [#140](https://github.com/apollographql/eslint-plugin-graphql/pull/140/files) by [Steve Hollaar](https://github.com/stevehollaar)
- Fix error location information for literal .graphql files and strings with leading newlines in [#122](https://github.com/apollographql/eslint-plugin-graphql/pull/122) by [Dan Freeman](https://github.com/dfreeman)
- Add [`fraql`](https://github.com/smooth-code/fraql) environment

### v2.1.0
- Retrieves `.graphqlconfig` relative to the file being linted, which re-enables support for `vscode-eslint` using `.graphqlconfig` in [#108](https://github.com/apollographql/eslint-plugin-graphql/pull/108) by [Jon Wong][https://github.com/jnwng/]
Expand Down
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ npm install eslint-plugin-graphql
1. [Apollo client](http://docs.apollostack.com/apollo-client/index.html)
2. [Relay](https://facebook.github.io/relay/)
3. [Lokka](https://github.com/kadirahq/lokka)
3. [FraQL](https://github.com/smooth-code/fraql)

If you want to lint your GraphQL schema, rather than queries, check out [cjoudrey/graphql-schema-linter](https://github.com/cjoudrey/graphql-schema-linter).

Expand All @@ -31,9 +32,9 @@ You'll need to import your [introspection query result](https://github.com/graph

### Common options

All 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 and literal files further down.
All 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.

- `env`: Import default settings for your GraphQL client. Supported values: `'apollo'`, `'relay'`, `'lokka'`, `'literal'`. Defaults to `'apollo'`. This is used for the slight parsing differences in the GraphQL syntax between Apollo, Relay and Lokka, as well as giving nice defaults to some other options.
- `env`: Import default settings for your GraphQL client. Supported values: `'apollo'`, `'relay'`, `'lokka'`, `'fraql'` `'literal'`. Defaults to `'apollo'`. This is used for the slight parsing differences in the GraphQL syntax between Apollo, Relay, Lokka and FraQL as well as giving nice defaults to some other options.

- `tagName`: The name of the template literal tag that this plugin should look for when searching for GraphQL queries. It has different defaults depending on the `env` option:

Expand Down Expand Up @@ -96,7 +97,7 @@ module.exports = {
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'literal'
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'apollo',

// Import your schema JSON here
Expand Down Expand Up @@ -126,7 +127,7 @@ module.exports = {
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'literal'
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'relay',

// Import your schema JSON here
Expand Down Expand Up @@ -156,7 +157,7 @@ module.exports = {
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'literal'
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'lokka',

// Import your schema JSON here
Expand All @@ -178,6 +179,37 @@ module.exports = {
}
```

### Example config for FraQL

```js
// 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

```js
Expand All @@ -187,7 +219,7 @@ module.exports = {
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'literal'
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'literal',

// Import your schema JSON here
Expand Down Expand Up @@ -244,7 +276,7 @@ module.exports = {
rules: {
"graphql/template-strings": ['error', {
// Import default settings for your GraphQL client. Supported values:
// 'apollo', 'relay', 'lokka', 'literal'
// 'apollo', 'relay', 'lokka', 'fraql', 'literal'
env: 'literal'
// no need to specify schema here, it will be automatically determined using .graphqlconfig
}]
Expand Down
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const envGraphQLValidatorNames = {
'KnownFragmentNames',
'NoUnusedFragments',
),
fraql: without(allGraphQLValidatorNames,
'KnownFragmentNames',
'NoUnusedFragments',
),
relay: without(allGraphQLValidatorNames,
'KnownDirectives',
'KnownFragmentNames',
Expand All @@ -57,6 +61,7 @@ const defaultRuleProperties = {
env: {
enum: [
'lokka',
'fraql',
'relay',
'apollo',
'literal',
Expand Down Expand Up @@ -295,8 +300,8 @@ function parseOptions(optionGroup, context) {
}

// Validate env
if (env && env !== 'lokka' && env !== 'relay' && env !== 'apollo' && env !== 'literal') {
throw new Error('Invalid option for env, only `apollo`, `lokka`, `relay`, and `literal` supported.')
if (env && env !== 'lokka' && env !== 'fraql' && env !== 'relay' && env !== 'apollo' && env !== 'literal') {
throw new Error('Invalid option for env, only `apollo`, `lokka`, `fraql`, `relay`, and `literal` supported.')
}

// Validate tagName and set default
Expand Down Expand Up @@ -385,7 +390,7 @@ function handleTemplateTag(node, context, schema, env, validators) {

// Re-implement syntax sugar for fragment names, which is technically not valid
// graphql
if ((env === 'lokka' || env === 'relay') && /fragment\s+on/.test(text)) {
if ((env === 'lokka' || env === 'relay' || env === 'fraql') && /fragment\s+on/.test(text)) {
text = text.replace('fragment', `fragment _`);
}

Expand Down Expand Up @@ -481,6 +486,10 @@ function replaceExpressions(node, context, env) {
// In Apollo, fragment interpolation is only valid outside of brackets
// Since we don't know what we'd interpolate here (that occurs at runtime),
// we're not going to do anything with this interpolation.
} else if (env === 'fraql') {
if (chunk.lastIndexOf('{') > chunk.lastIndexOf('}')) {
chunks.push('__typename');
}
} else {
// Invalid interpolation
context.report({
Expand Down
42 changes: 42 additions & 0 deletions test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,48 @@ const parserOptions = {
});
}

{
const options = [
{ schemaJson, env: 'fraql' },
];

ruleTester.run('fraql', rule, {
valid: [
{
options,
parserOptions,
code: 'const x = gql`{ number } ${x}`',
},
{
options,
parserOptions,
code: 'const x = gql`query { ${x} }`',
},
{
options,
parserOptions,
code: `const x = gql\`
fragment _ on Film {
title
director
releaseDate
... on Film {
title
\${xxx}
}
... on Film {
\${xxx}
}
}
\`
`,
},
],

invalid: [],
})
}

{
const options = [
{
Expand Down

0 comments on commit 0ff1824

Please sign in to comment.