Skip to content

Commit ba26511

Browse files
Multi projects example (#1201)
* add multi project example * chore(dependencies): updated changesets for modified dependencies * fix Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f193b5e commit ba26511

16 files changed

+276
-63
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
dependencies updates:
5+
- Updated dependency [`graphql-config@^4.3.6` ↗︎](https://www.npmjs.com/package/graphql-config/v/4.3.6) (from `^4.3.5`, in `dependencies`)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
root: true,
3+
// ❗️ It's very important that you don't have any rules configured at the top-level config,
4+
// and to move all configurations into the overrides section. Since JavaScript rules
5+
// can't run on GraphQL files and vice versa, if you have rules configured at the top level,
6+
// they will try to also execute for all overrides, as ESLint's configs cascade
7+
overrides: [
8+
{
9+
files: ['*.js'],
10+
processor: '@graphql-eslint/graphql',
11+
extends: ['eslint:recommended'],
12+
parserOptions: {
13+
sourceType: 'module',
14+
},
15+
env: {
16+
es6: true,
17+
},
18+
},
19+
{
20+
files: ['schema.*.graphql'],
21+
extends: ['plugin:@graphql-eslint/schema-recommended'],
22+
rules: {
23+
'@graphql-eslint/require-description': 'off',
24+
},
25+
},
26+
{
27+
files: ['*.js/*.graphql'],
28+
extends: ['plugin:@graphql-eslint/operations-recommended'],
29+
},
30+
],
31+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { IGraphQLConfig } from 'graphql-config';
2+
import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
3+
4+
const config: IGraphQLConfig = {
5+
projects: {
6+
firstProject: {
7+
schema: 'schema.first-project.graphql',
8+
documents: 'query.first-project.js',
9+
},
10+
secondProject: {
11+
schema: 'schema.second-project.graphql',
12+
documents: 'query.second-project.js',
13+
extensions: {
14+
// in case you want to use different names for magic comment and module identifier
15+
pluckConfig: <GraphQLTagPluckOptions>{
16+
modules: [{ name: 'custom-graphql-tag', identifier: 'custom' }],
17+
globalGqlIdentifierName: 'custom',
18+
gqlMagicComment: 'MyGraphQL',
19+
},
20+
},
21+
},
22+
},
23+
};
24+
25+
export default config;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@graphql-eslint/example-multiple-projects-graphql-config",
3+
"version": "0.0.0",
4+
"private": true,
5+
"author": "Dimitri POSTOLOV",
6+
"scripts": {
7+
"lint": "eslint ."
8+
},
9+
"dependencies": {
10+
"graphql": "16.6.0"
11+
},
12+
"devDependencies": {
13+
"@graphql-eslint/eslint-plugin": "3.11.2",
14+
"eslint": "8.24.0"
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { gql } from 'graphql-tag';
2+
3+
/* GraphQL */ `
4+
fragment UserFields on User {
5+
firstname
6+
lastname
7+
}
8+
`;
9+
10+
gql`
11+
{
12+
user {
13+
...UserFields
14+
}
15+
}
16+
`;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { custom } from 'custom-graphql-tag';
2+
3+
/* MyGraphQL */ `
4+
fragment UserFields on User {
5+
firstName
6+
lastName
7+
}
8+
`;
9+
10+
custom`
11+
{
12+
users {
13+
...UserFields
14+
}
15+
}
16+
`;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type User {
2+
firstname: String
3+
lastname: String
4+
}
5+
6+
type Query {
7+
user: User
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type User {
2+
firstName: String
3+
lastName: String
4+
}
5+
6+
type Query {
7+
users: [User]
8+
}

packages/plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"chalk": "^4.1.2",
4242
"debug": "^4.3.4",
4343
"fast-glob": "^3.2.12",
44-
"graphql-config": "^4.3.5",
44+
"graphql-config": "^4.3.6",
4545
"graphql-depth-limit": "^1.1.0",
4646
"lodash.lowercase": "^4.3.0"
4747
},

packages/plugin/src/graphql-config.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
11
import { dirname } from 'path';
22
import debugFactory from 'debug';
3-
import {
4-
GraphQLConfig,
5-
GraphQLExtensionDeclaration,
6-
loadConfigSync,
7-
SchemaPointer,
8-
} from 'graphql-config';
9-
import { GraphQLTagPluckOptions } from '@graphql-tools/graphql-tag-pluck';
3+
import { GraphQLConfig, loadConfigSync, SchemaPointer } from 'graphql-config';
104
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
115
import { ParserOptions } from './types';
126

137
const debug = debugFactory('graphql-eslint:graphql-config');
148
let graphQLConfig: GraphQLConfig;
159

1610
export function loadOnDiskGraphQLConfig(filePath: string): GraphQLConfig {
17-
const rootDir = dirname(filePath);
18-
const config = loadConfigSync({
11+
return loadConfigSync({
1912
// load config relative to the file being linted
20-
rootDir,
13+
rootDir: dirname(filePath),
2114
throwOnEmpty: false,
2215
throwOnMissing: false,
23-
});
24-
if (!config) {
25-
return null;
26-
}
27-
const project = config.getProjectForFile(filePath);
28-
return loadConfigSync({
29-
rootDir,
30-
extensions: [codeFileLoaderExtension(project.extensions.pluckConfig)],
16+
extensions: [codeFileLoaderExtension],
3117
});
3218
}
3319

@@ -62,17 +48,15 @@ export function loadGraphQLConfig(options: ParserOptions): GraphQLConfig {
6248
config: configOptions,
6349
filepath: 'virtual-config',
6450
},
65-
[codeFileLoaderExtension(options.extensions?.pluckConfig)],
51+
[codeFileLoaderExtension],
6652
);
6753

6854
return graphQLConfig;
6955
}
7056

71-
const codeFileLoaderExtension =
72-
(pluckConfig: GraphQLTagPluckOptions): GraphQLExtensionDeclaration =>
73-
api => {
74-
const { schema, documents } = api.loaders;
75-
schema.register(new CodeFileLoader({ pluckConfig }));
76-
documents.register(new CodeFileLoader({ pluckConfig }));
77-
return { name: 'graphql-eslint-loaders' };
78-
};
57+
const codeFileLoaderExtension = api => {
58+
const { schema, documents } = api.loaders;
59+
schema.register(new CodeFileLoader());
60+
documents.register(new CodeFileLoader());
61+
return { name: 'graphql-eslint-loaders' };
62+
};

0 commit comments

Comments
 (0)