-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
53 lines (44 loc) · 1.25 KB
/
index.ts
File metadata and controls
53 lines (44 loc) · 1.25 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
import { buildSchema, graphql } from "graphql";
import gqlSchema from "../assets/schema.gql";
import { rootResolver } from "./resolvers";
import { consola } from "consola";
import pc from "picocolors";
import { dependencies } from "../dependencies";
export function createGraphql() {
const schema = buildSchema(gqlSchema);
let increment = 0;
const evaluateQuery = async (method: string, body: GraphQLParams) => {
const id = ++increment;
const { operationName = "Unknown", query, variables } = body;
const start = performance.now();
consola.debug(
`${pc.dim(`<-- [${id}]`)} ${pc.green(method)} ${pc.cyan(
pc.bold(operationName),
)}`,
);
const response = await graphql({
schema,
source: query,
contextValue: dependencies.resolveAll(),
variableValues: variables,
rootValue: rootResolver,
});
const end = performance.now();
consola.debug(
`${pc.dim(`--> [${id}]`)} ${pc.green(method)} ${pc.cyan(
pc.bold(operationName),
)} ${(end - start).toFixed(3)}ms`,
);
return response;
};
return {
evaluateQuery,
};
}
export interface GraphQLParams {
query: string;
variables?: {
readonly [name: string]: unknown;
};
operationName?: string;
}