-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathgraphql-config.ts
More file actions
75 lines (66 loc) · 2.03 KB
/
graphql-config.ts
File metadata and controls
75 lines (66 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { ApolloEngineLoader } from '@graphql-tools/apollo-engine-loader';
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
import { GitLoader } from '@graphql-tools/git-loader';
import { GithubLoader } from '@graphql-tools/github-loader';
import { PrismaLoader } from '@graphql-tools/prisma-loader';
import { GraphQLConfig, GraphQLExtensionDeclaration, loadConfig } from 'graphql-config';
export const CodegenExtension: GraphQLExtensionDeclaration = (api: any) => {
// Schema
api.loaders.schema.register(
new CodeFileLoader({
pluckConfig: {
skipIndent: true,
},
})
);
api.loaders.schema.register(new GitLoader());
api.loaders.schema.register(new GithubLoader());
api.loaders.schema.register(new ApolloEngineLoader());
api.loaders.schema.register(new PrismaLoader());
// Documents
api.loaders.documents.register(
new CodeFileLoader({
pluckConfig: {
skipIndent: true,
},
})
);
api.loaders.documents.register(new GitLoader());
api.loaders.documents.register(new GithubLoader());
return {
name: 'codegen',
};
};
export async function findAndLoadGraphQLConfig(filepath?: string): Promise<GraphQLConfig | void> {
const config = await loadConfig({
filepath,
rootDir: process.cwd(),
extensions: [CodegenExtension],
throwOnEmpty: false,
throwOnMissing: false,
});
if (isGraphQLConfig(config)) {
return config;
}
}
// Kamil: user might load a config that is not GraphQL Config
// so we need to check if it's a regular config or not
function isGraphQLConfig(config: GraphQLConfig): config is GraphQLConfig {
if (!config) {
return false;
}
try {
for (const projectName in config.projects) {
if (Object.prototype.hasOwnProperty.call(config.projects, projectName)) {
const project = config.projects[projectName];
if (project.hasExtension('codegen')) {
return true;
}
}
}
} catch {}
try {
return config.getDefault().hasExtension('codegen');
} catch {}
return false;
}