-
-
Notifications
You must be signed in to change notification settings - Fork 804
/
index.ts
74 lines (69 loc) · 2.46 KB
/
index.ts
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
import { promises as fsPromises } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import { cwd } from 'process';
import { LoadFromUrlOptions, UrlLoader } from '@graphql-tools/url-loader';
import { Environment, PrismaDefinitionClass } from './prisma-yml/index.js';
const { access } = fsPromises;
/**
* additional options for loading from a `prisma.yml` file
*/
export interface PrismaLoaderOptions extends LoadFromUrlOptions {
envVars?: { [key: string]: string };
graceful?: boolean;
cwd?: string;
}
/**
* This loader loads a schema from a `prisma.yml` file
*/
export class PrismaLoader extends UrlLoader {
canLoadSync() {
return false;
}
async canLoad(prismaConfigFilePath: string, options: PrismaLoaderOptions): Promise<boolean> {
if (typeof prismaConfigFilePath === 'string' && prismaConfigFilePath.endsWith('prisma.yml')) {
const joinedYmlPath = join(options.cwd || cwd(), prismaConfigFilePath);
try {
await access(joinedYmlPath);
return true;
} catch {
return false;
}
}
return false;
}
async load(prismaConfigFilePath: string, options: PrismaLoaderOptions) {
if (!(await this.canLoad(prismaConfigFilePath, options))) {
return [];
}
const { graceful, envVars } = options;
const home = homedir();
const env = new Environment(home);
await env.load();
const joinedYmlPath = join(options.cwd || cwd(), prismaConfigFilePath);
const definition = new PrismaDefinitionClass(env, joinedYmlPath, envVars);
await definition.load({}, undefined, graceful);
const serviceName = definition.service!;
const stage = definition.stage!;
const clusterName = definition.cluster;
if (!clusterName) {
throw new Error(`No cluster set. Please set the "cluster" property in your prisma.yml`);
}
const cluster = await definition.getCluster();
if (!cluster) {
throw new Error(
`Cluster ${clusterName} provided in prisma.yml could not be found in global ~/.prisma/config.yml.
Please check in ~/.prisma/config.yml, if the cluster exists.
You can use \`docker-compose up -d\` to start a new cluster.`,
);
}
const token = await definition.getToken(serviceName, stage);
const url = cluster.getApiEndpoint(serviceName, stage, definition.getWorkspace() || undefined);
const headers = token
? {
Authorization: `Bearer ${token}`,
}
: undefined;
return super.load(url, { headers });
}
}