This repository has been archived by the owner on Jun 29, 2021. It is now read-only.
/
index.ts
143 lines (124 loc) · 3.21 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import {resolve} from 'path';
import {readFile, readJSON} from 'fs-extra';
import {Source, parse, concatAST, GraphQLSchema} from 'graphql';
import {getGraphQLConfig, GraphQLProjectConfig} from 'graphql-config';
import {
getGraphQLProjectIncludedFilePaths,
getGraphQLProjects,
} from 'graphql-config-utilities';
import {compile} from 'graphql-tool-utilities';
import {
Fixture,
Validation,
validateFixture,
getOperationForFixture,
MissingOperationError,
AmbiguousOperationNameError,
GraphQLProjectAST,
} from './validate';
export interface Options {
cwd: string;
}
export interface Evaluation extends Validation {
scriptError?: Error;
}
export async function evaluateFixtures(
fixturePaths: string[],
{cwd}: Options,
): Promise<Evaluation[]> {
const config = getGraphQLConfig(resolve(cwd));
const projectASTCollection = await Promise.all(
getGraphQLProjects(config).map(getOperationsForProject),
);
return runForEachFixture(fixturePaths, (fixture) =>
evaluateFixture(fixture, projectASTCollection),
);
}
async function getOperationsForProject(
projectConfig: GraphQLProjectConfig,
): Promise<GraphQLProjectAST> {
const operationPaths = await getGraphQLProjectIncludedFilePaths(
projectConfig,
);
const operationSources = await Promise.all(
operationPaths.map(loadOperationSource),
);
const document = concatAST(
operationSources.map((source) => {
try {
return parse(source);
} catch (error) {
throw new Error(
`Error parsing '${source.name}':\n\n${error.message.replace(
/Syntax Error.*?\(.*?\) /,
'',
)}`,
);
}
}),
);
let schema: GraphQLSchema;
try {
schema = projectConfig.getSchema();
} catch (error) {
throw new Error(
`Error parsing '${projectConfig.schemaPath}':\n\n${error.message.replace(
/Syntax Error.*?\(.*?\) /,
'',
)}`,
);
}
return {
ast: compile(schema, document),
config: projectConfig,
};
}
async function loadOperationSource(filePath: string) {
const body = await readFile(filePath, 'utf8');
return new Source(body, filePath);
}
function runForEachFixture<T extends Partial<Evaluation>>(
fixturePaths: string[],
runner: (fixture: Fixture) => T,
): Promise<Evaluation[]> {
return Promise.all(
fixturePaths.map(async (fixturePath) => {
try {
const fixture = await readJSON(fixturePath);
return {
fixturePath,
...(runner({path: fixturePath, content: fixture}) as any),
};
} catch (error) {
return {
fixturePath,
scriptError: error,
validationErrors: [],
};
}
}),
);
}
function evaluateFixture(
fixture: Fixture,
projectASTCollection: GraphQLProjectAST[],
): Evaluation {
try {
const {
operation,
projectAST: {ast},
} = getOperationForFixture(fixture, projectASTCollection);
return validateFixture(fixture, ast, operation);
} catch (error) {
if (
error instanceof MissingOperationError ||
error instanceof AmbiguousOperationNameError
) {
return {
fixturePath: fixture.path,
validationErrors: [error],
};
}
throw error;
}
}