@ocap/gql 1.31.5 → 1.31.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/http.d.mts +40 -1
- package/esm/http.mjs +89 -3
- package/esm/index.d.mts +2 -2
- package/lib/http.cjs +89 -3
- package/lib/http.d.cts +40 -1
- package/lib/index.d.cts +2 -2
- package/package.json +3 -3
package/esm/http.d.mts
CHANGED
|
@@ -34,6 +34,45 @@ interface HttpHandlerOptions {
|
|
|
34
34
|
maxQueryDepth?: number;
|
|
35
35
|
maxTotalSelections?: number;
|
|
36
36
|
maxAliases?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Requests whose parse + execute + serialize time reaches this many ms are reported to `onSlowRequest`.
|
|
39
|
+
* Falls back to `GQL_SLOW_LOG_MS`, then 2000. 0 disables the timing entirely.
|
|
40
|
+
*/
|
|
41
|
+
slowLogMs?: number;
|
|
42
|
+
/** Receives slow-request reports. Defaults to one `console.warn('[gql] slow request', json)` line. */
|
|
43
|
+
onSlowRequest?: (report: SlowRequestReport) => void;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Per root field stats inside one request. graphql-js starts every root field of a batch in the same tick and their
|
|
47
|
+
* awaits interleave, so a field's wall time also covers the other fields running alongside it: `wallMaxMs` is at
|
|
48
|
+
* most `executeMs`, and `wallSumMs` can exceed it. Use `count` to see what the batch was made of and `executeMs`
|
|
49
|
+
* for how long the event loop was busy.
|
|
50
|
+
*/
|
|
51
|
+
interface SlowRequestFieldStat {
|
|
52
|
+
count: number;
|
|
53
|
+
wallSumMs: number;
|
|
54
|
+
wallMaxMs: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Shape-only description of a slow request. Never carries argument values or variables: addresses
|
|
58
|
+
* and other inputs stay in the access log, which already records the client and time.
|
|
59
|
+
*/
|
|
60
|
+
interface SlowRequestReport {
|
|
61
|
+
totalMs: number;
|
|
62
|
+
parseMs: number;
|
|
63
|
+
executeMs: number;
|
|
64
|
+
serializeMs: number;
|
|
65
|
+
status: number;
|
|
66
|
+
responseChars: number;
|
|
67
|
+
queryChars: number;
|
|
68
|
+
operationName: string | null;
|
|
69
|
+
topLevel: number;
|
|
70
|
+
selections: number;
|
|
71
|
+
aliases: number;
|
|
72
|
+
numericAliases: number;
|
|
73
|
+
errors: number;
|
|
74
|
+
fields: Record<string, SlowRequestFieldStat>;
|
|
75
|
+
userAgent?: string;
|
|
37
76
|
}
|
|
38
77
|
/**
|
|
39
78
|
* Transport-agnostic view of an incoming GraphQL request, so the same engine serves express (`createHttpHandler`)
|
|
@@ -76,4 +115,4 @@ declare const createHttpHandler: (options: HttpHandlerOptions) => RequestHandler
|
|
|
76
115
|
*/
|
|
77
116
|
declare const createFetchHandler: (options: HttpHandlerOptions) => (request: globalThis.Request) => Promise<globalThis.Response>;
|
|
78
117
|
//#endregion
|
|
79
|
-
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
|
118
|
+
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, SlowRequestFieldStat, SlowRequestReport, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
package/esm/http.mjs
CHANGED
|
@@ -249,6 +249,31 @@ const MAX_TOTAL_SELECTIONS = 3e3;
|
|
|
249
249
|
const MAX_ALIASES = 100;
|
|
250
250
|
const MAX_NUMERIC_ALIAS_REWRITES = 2e3;
|
|
251
251
|
const BODY_SIZE_LIMIT = 100 * 1024;
|
|
252
|
+
const DEFAULT_SLOW_LOG_MS = 2e3;
|
|
253
|
+
const defaultSlowRequestHandler = (report) => {
|
|
254
|
+
console.warn("[gql] slow request", JSON.stringify(report));
|
|
255
|
+
};
|
|
256
|
+
const nowMs = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
257
|
+
/** Root field names the operation selects (fragments followed one level deep, bounded). */
|
|
258
|
+
const collectRootFieldNames = (documentAST, operationName) => {
|
|
259
|
+
const names = /* @__PURE__ */ new Set();
|
|
260
|
+
const op = getOperationAST(documentAST, operationName || void 0);
|
|
261
|
+
if (!op) return names;
|
|
262
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
263
|
+
for (const def of documentAST.definitions) if (def.kind === "FragmentDefinition") fragments.set(def.name.value, def.selectionSet.selections);
|
|
264
|
+
const seen = /* @__PURE__ */ new Set();
|
|
265
|
+
const walk = (selections, depth) => {
|
|
266
|
+
for (const sel of selections) if (sel.kind === "Field") names.add(sel.name.value);
|
|
267
|
+
else if (depth < 8 && sel.kind === "InlineFragment") walk(sel.selectionSet.selections, depth + 1);
|
|
268
|
+
else if (depth < 8 && sel.kind === "FragmentSpread" && !seen.has(sel.name.value)) {
|
|
269
|
+
seen.add(sel.name.value);
|
|
270
|
+
walk(fragments.get(sel.name.value) || [], depth + 1);
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
walk(op.selectionSet.selections, 0);
|
|
274
|
+
return names;
|
|
275
|
+
};
|
|
276
|
+
const headerValue = (v) => Array.isArray(v) ? v[0] : v;
|
|
252
277
|
/**
|
|
253
278
|
* Read the raw express request body as a utf-8 string, enforcing the size limit.
|
|
254
279
|
* Mirrors express-graphql's parseBody behavior for the subset of content
|
|
@@ -791,7 +816,7 @@ const parseEnvBoolean = (raw) => {
|
|
|
791
816
|
if (normalized === "1" || normalized === "true") return true;
|
|
792
817
|
if (normalized === "0" || normalized === "false") return false;
|
|
793
818
|
};
|
|
794
|
-
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES }) => {
|
|
819
|
+
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES, slowLogMs = parseEnvPositiveInt(process.env.GQL_SLOW_LOG_MS) ?? DEFAULT_SLOW_LOG_MS, onSlowRequest = defaultSlowRequestHandler }) => {
|
|
795
820
|
const schema = buildSchema(schemaSource);
|
|
796
821
|
const rootValue = createResolvers(resolver);
|
|
797
822
|
const formatGraphQLError = (err) => {
|
|
@@ -840,6 +865,7 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
840
865
|
if (showGraphiQL) return sendHTML(GRAPHIQL_HTML);
|
|
841
866
|
return sendJSON(400, { errors: [formatAnyError("Must provide query string.")] });
|
|
842
867
|
}
|
|
868
|
+
const startedAt = slowLogMs > 0 ? nowMs() : 0;
|
|
843
869
|
const { query: normalizedQuery, prefix: aliasPrefix, aliasMap } = normalizeNumericFieldAliases(params.query);
|
|
844
870
|
const formatError = (err) => restoreNumericAliasesInFormattedError(formatGraphQLError(err), aliasPrefix, aliasMap);
|
|
845
871
|
let documentAST;
|
|
@@ -871,12 +897,39 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
871
897
|
const operationAST = getOperationAST(documentAST, params.operationName || void 0);
|
|
872
898
|
if (operationAST && operationAST.operation !== "query") return sendJSON(405, { errors: [formatAnyError(`Can only perform a ${operationAST.operation} operation from a POST request.`)] }, { Allow: "POST" });
|
|
873
899
|
}
|
|
900
|
+
const fieldStats = /* @__PURE__ */ new Map();
|
|
901
|
+
let requestRoot = rootValue;
|
|
902
|
+
if (slowLogMs > 0) {
|
|
903
|
+
requestRoot = Object.create(rootValue);
|
|
904
|
+
for (const name of collectRootFieldNames(documentAST, params.operationName)) {
|
|
905
|
+
const fn = rootValue[name];
|
|
906
|
+
if (typeof fn !== "function") continue;
|
|
907
|
+
requestRoot[name] = async (...args) => {
|
|
908
|
+
const t = nowMs();
|
|
909
|
+
try {
|
|
910
|
+
return await fn(...args);
|
|
911
|
+
} finally {
|
|
912
|
+
const ms = nowMs() - t;
|
|
913
|
+
const s = fieldStats.get(name) || {
|
|
914
|
+
count: 0,
|
|
915
|
+
wallSumMs: 0,
|
|
916
|
+
wallMaxMs: 0
|
|
917
|
+
};
|
|
918
|
+
s.count += 1;
|
|
919
|
+
s.wallSumMs += ms;
|
|
920
|
+
if (ms > s.wallMaxMs) s.wallMaxMs = ms;
|
|
921
|
+
fieldStats.set(name, s);
|
|
922
|
+
}
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
const executeStartedAt = slowLogMs > 0 ? nowMs() : 0;
|
|
874
927
|
let result;
|
|
875
928
|
try {
|
|
876
929
|
result = await execute({
|
|
877
930
|
schema,
|
|
878
931
|
document: documentAST,
|
|
879
|
-
rootValue,
|
|
932
|
+
rootValue: requestRoot,
|
|
880
933
|
contextValue,
|
|
881
934
|
variableValues: params.variables,
|
|
882
935
|
operationName: params.operationName
|
|
@@ -886,7 +939,40 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
886
939
|
}
|
|
887
940
|
const formattedResult = { data: aliasMap && result.data != null ? restoreNumericAliasesInData(result.data, aliasMap) : result.data };
|
|
888
941
|
if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatError);
|
|
889
|
-
|
|
942
|
+
const status = result.data == null ? 500 : 200;
|
|
943
|
+
if (slowLogMs <= 0) return sendJSON(status, formattedResult);
|
|
944
|
+
const executedAt = nowMs();
|
|
945
|
+
const response = sendJSON(status, formattedResult);
|
|
946
|
+
const finishedAt = nowMs();
|
|
947
|
+
const totalMs = finishedAt - startedAt;
|
|
948
|
+
if (totalMs >= slowLogMs) try {
|
|
949
|
+
const { fields: selections, aliases } = countTotalSelectionsAndAliases(documentAST, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
950
|
+
const round = (n) => Math.round(n);
|
|
951
|
+
const fields = {};
|
|
952
|
+
for (const [name, s] of fieldStats) fields[name] = {
|
|
953
|
+
count: s.count,
|
|
954
|
+
wallSumMs: round(s.wallSumMs),
|
|
955
|
+
wallMaxMs: round(s.wallMaxMs)
|
|
956
|
+
};
|
|
957
|
+
onSlowRequest({
|
|
958
|
+
totalMs: round(totalMs),
|
|
959
|
+
parseMs: round(executeStartedAt - startedAt),
|
|
960
|
+
executeMs: round(executedAt - executeStartedAt),
|
|
961
|
+
serializeMs: round(finishedAt - executedAt),
|
|
962
|
+
status,
|
|
963
|
+
responseChars: response.body.length,
|
|
964
|
+
queryChars: params.query.length,
|
|
965
|
+
operationName: params.operationName ? params.operationName.slice(0, 100) : null,
|
|
966
|
+
topLevel: countTopLevelSelections(documentAST, Number.MAX_SAFE_INTEGER),
|
|
967
|
+
selections,
|
|
968
|
+
aliases,
|
|
969
|
+
numericAliases: aliasMap ? aliasMap.size : 0,
|
|
970
|
+
errors: result.errors ? result.errors.length : 0,
|
|
971
|
+
fields,
|
|
972
|
+
userAgent: headerValue(req.headers["user-agent"])?.slice(0, 160)
|
|
973
|
+
});
|
|
974
|
+
} catch {}
|
|
975
|
+
return response;
|
|
890
976
|
};
|
|
891
977
|
};
|
|
892
978
|
/** Express / connect adapter. Behavior is unchanged from the pre-split `createHttpHandler`. */
|
package/esm/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.mjs";
|
|
1
|
+
import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, SlowRequestFieldStat, SlowRequestReport, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.mjs";
|
|
2
2
|
import { Server } from "node:http";
|
|
3
3
|
import { TIndexedAccountState, TIndexedAssetState, TIndexedDelegationState, TIndexedFactoryState, TIndexedRollupBlock, TIndexedRollupState, TIndexedStakeState, TIndexedTokenState, TIndexedTransaction } from "@ocap/types";
|
|
4
4
|
import { Express } from "express";
|
|
@@ -39,4 +39,4 @@ declare const createSocketHandler: ({
|
|
|
39
39
|
wsOptions
|
|
40
40
|
}: SocketHandlerOptions) => Server;
|
|
41
41
|
//#endregion
|
|
42
|
-
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
|
42
|
+
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, SlowRequestFieldStat, SlowRequestReport, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
package/lib/http.cjs
CHANGED
|
@@ -252,6 +252,31 @@ const MAX_TOTAL_SELECTIONS = 3e3;
|
|
|
252
252
|
const MAX_ALIASES = 100;
|
|
253
253
|
const MAX_NUMERIC_ALIAS_REWRITES = 2e3;
|
|
254
254
|
const BODY_SIZE_LIMIT = 100 * 1024;
|
|
255
|
+
const DEFAULT_SLOW_LOG_MS = 2e3;
|
|
256
|
+
const defaultSlowRequestHandler = (report) => {
|
|
257
|
+
console.warn("[gql] slow request", JSON.stringify(report));
|
|
258
|
+
};
|
|
259
|
+
const nowMs = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
260
|
+
/** Root field names the operation selects (fragments followed one level deep, bounded). */
|
|
261
|
+
const collectRootFieldNames = (documentAST, operationName) => {
|
|
262
|
+
const names = /* @__PURE__ */ new Set();
|
|
263
|
+
const op = (0, graphql.getOperationAST)(documentAST, operationName || void 0);
|
|
264
|
+
if (!op) return names;
|
|
265
|
+
const fragments = /* @__PURE__ */ new Map();
|
|
266
|
+
for (const def of documentAST.definitions) if (def.kind === "FragmentDefinition") fragments.set(def.name.value, def.selectionSet.selections);
|
|
267
|
+
const seen = /* @__PURE__ */ new Set();
|
|
268
|
+
const walk = (selections, depth) => {
|
|
269
|
+
for (const sel of selections) if (sel.kind === "Field") names.add(sel.name.value);
|
|
270
|
+
else if (depth < 8 && sel.kind === "InlineFragment") walk(sel.selectionSet.selections, depth + 1);
|
|
271
|
+
else if (depth < 8 && sel.kind === "FragmentSpread" && !seen.has(sel.name.value)) {
|
|
272
|
+
seen.add(sel.name.value);
|
|
273
|
+
walk(fragments.get(sel.name.value) || [], depth + 1);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
walk(op.selectionSet.selections, 0);
|
|
277
|
+
return names;
|
|
278
|
+
};
|
|
279
|
+
const headerValue = (v) => Array.isArray(v) ? v[0] : v;
|
|
255
280
|
/**
|
|
256
281
|
* Read the raw express request body as a utf-8 string, enforcing the size limit.
|
|
257
282
|
* Mirrors express-graphql's parseBody behavior for the subset of content
|
|
@@ -794,7 +819,7 @@ const parseEnvBoolean = (raw) => {
|
|
|
794
819
|
if (normalized === "1" || normalized === "true") return true;
|
|
795
820
|
if (normalized === "0" || normalized === "false") return false;
|
|
796
821
|
};
|
|
797
|
-
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES }) => {
|
|
822
|
+
const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, graphiql = parseEnvBoolean(process.env.GQL_GRAPHIQL ?? process.env.GRAPHQL_GRAPHIQL) ?? process.env.NODE_ENV !== "production", introspection = parseEnvBoolean(process.env.GQL_INTROSPECTION ?? process.env.GRAPHQL_INTROSPECTION) ?? process.env.NODE_ENV !== "production", showStack = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test", maxQueryDepth = MAX_QUERY_DEPTH, maxTotalSelections = parseEnvPositiveInt(process.env.GQL_MAX_TOTAL_SELECTIONS) ?? MAX_TOTAL_SELECTIONS, maxAliases = parseEnvPositiveInt(process.env.GQL_MAX_ALIASES) ?? MAX_ALIASES, slowLogMs = parseEnvPositiveInt(process.env.GQL_SLOW_LOG_MS) ?? DEFAULT_SLOW_LOG_MS, onSlowRequest = defaultSlowRequestHandler }) => {
|
|
798
823
|
const schema = (0, graphql.buildSchema)(_ocap_schema.default);
|
|
799
824
|
const rootValue = createResolvers(resolver);
|
|
800
825
|
const formatGraphQLError = (err) => {
|
|
@@ -843,6 +868,7 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
843
868
|
if (showGraphiQL) return sendHTML(GRAPHIQL_HTML);
|
|
844
869
|
return sendJSON(400, { errors: [formatAnyError("Must provide query string.")] });
|
|
845
870
|
}
|
|
871
|
+
const startedAt = slowLogMs > 0 ? nowMs() : 0;
|
|
846
872
|
const { query: normalizedQuery, prefix: aliasPrefix, aliasMap } = normalizeNumericFieldAliases(params.query);
|
|
847
873
|
const formatError = (err) => restoreNumericAliasesInFormattedError(formatGraphQLError(err), aliasPrefix, aliasMap);
|
|
848
874
|
let documentAST;
|
|
@@ -874,12 +900,39 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
874
900
|
const operationAST = (0, graphql.getOperationAST)(documentAST, params.operationName || void 0);
|
|
875
901
|
if (operationAST && operationAST.operation !== "query") return sendJSON(405, { errors: [formatAnyError(`Can only perform a ${operationAST.operation} operation from a POST request.`)] }, { Allow: "POST" });
|
|
876
902
|
}
|
|
903
|
+
const fieldStats = /* @__PURE__ */ new Map();
|
|
904
|
+
let requestRoot = rootValue;
|
|
905
|
+
if (slowLogMs > 0) {
|
|
906
|
+
requestRoot = Object.create(rootValue);
|
|
907
|
+
for (const name of collectRootFieldNames(documentAST, params.operationName)) {
|
|
908
|
+
const fn = rootValue[name];
|
|
909
|
+
if (typeof fn !== "function") continue;
|
|
910
|
+
requestRoot[name] = async (...args) => {
|
|
911
|
+
const t = nowMs();
|
|
912
|
+
try {
|
|
913
|
+
return await fn(...args);
|
|
914
|
+
} finally {
|
|
915
|
+
const ms = nowMs() - t;
|
|
916
|
+
const s = fieldStats.get(name) || {
|
|
917
|
+
count: 0,
|
|
918
|
+
wallSumMs: 0,
|
|
919
|
+
wallMaxMs: 0
|
|
920
|
+
};
|
|
921
|
+
s.count += 1;
|
|
922
|
+
s.wallSumMs += ms;
|
|
923
|
+
if (ms > s.wallMaxMs) s.wallMaxMs = ms;
|
|
924
|
+
fieldStats.set(name, s);
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
const executeStartedAt = slowLogMs > 0 ? nowMs() : 0;
|
|
877
930
|
let result;
|
|
878
931
|
try {
|
|
879
932
|
result = await (0, graphql.execute)({
|
|
880
933
|
schema,
|
|
881
934
|
document: documentAST,
|
|
882
|
-
rootValue,
|
|
935
|
+
rootValue: requestRoot,
|
|
883
936
|
contextValue,
|
|
884
937
|
variableValues: params.variables,
|
|
885
938
|
operationName: params.operationName
|
|
@@ -889,7 +942,40 @@ const createGraphQLEngine = ({ resolver = {}, onError = defaultErrorHandler, gra
|
|
|
889
942
|
}
|
|
890
943
|
const formattedResult = { data: aliasMap && result.data != null ? restoreNumericAliasesInData(result.data, aliasMap) : result.data };
|
|
891
944
|
if (result.errors && result.errors.length > 0) formattedResult.errors = result.errors.map(formatError);
|
|
892
|
-
|
|
945
|
+
const status = result.data == null ? 500 : 200;
|
|
946
|
+
if (slowLogMs <= 0) return sendJSON(status, formattedResult);
|
|
947
|
+
const executedAt = nowMs();
|
|
948
|
+
const response = sendJSON(status, formattedResult);
|
|
949
|
+
const finishedAt = nowMs();
|
|
950
|
+
const totalMs = finishedAt - startedAt;
|
|
951
|
+
if (totalMs >= slowLogMs) try {
|
|
952
|
+
const { fields: selections, aliases } = countTotalSelectionsAndAliases(documentAST, Number.MAX_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
|
|
953
|
+
const round = (n) => Math.round(n);
|
|
954
|
+
const fields = {};
|
|
955
|
+
for (const [name, s] of fieldStats) fields[name] = {
|
|
956
|
+
count: s.count,
|
|
957
|
+
wallSumMs: round(s.wallSumMs),
|
|
958
|
+
wallMaxMs: round(s.wallMaxMs)
|
|
959
|
+
};
|
|
960
|
+
onSlowRequest({
|
|
961
|
+
totalMs: round(totalMs),
|
|
962
|
+
parseMs: round(executeStartedAt - startedAt),
|
|
963
|
+
executeMs: round(executedAt - executeStartedAt),
|
|
964
|
+
serializeMs: round(finishedAt - executedAt),
|
|
965
|
+
status,
|
|
966
|
+
responseChars: response.body.length,
|
|
967
|
+
queryChars: params.query.length,
|
|
968
|
+
operationName: params.operationName ? params.operationName.slice(0, 100) : null,
|
|
969
|
+
topLevel: countTopLevelSelections(documentAST, Number.MAX_SAFE_INTEGER),
|
|
970
|
+
selections,
|
|
971
|
+
aliases,
|
|
972
|
+
numericAliases: aliasMap ? aliasMap.size : 0,
|
|
973
|
+
errors: result.errors ? result.errors.length : 0,
|
|
974
|
+
fields,
|
|
975
|
+
userAgent: headerValue(req.headers["user-agent"])?.slice(0, 160)
|
|
976
|
+
});
|
|
977
|
+
} catch {}
|
|
978
|
+
return response;
|
|
893
979
|
};
|
|
894
980
|
};
|
|
895
981
|
/** Express / connect adapter. Behavior is unchanged from the pre-split `createHttpHandler`. */
|
package/lib/http.d.cts
CHANGED
|
@@ -34,6 +34,45 @@ interface HttpHandlerOptions {
|
|
|
34
34
|
maxQueryDepth?: number;
|
|
35
35
|
maxTotalSelections?: number;
|
|
36
36
|
maxAliases?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Requests whose parse + execute + serialize time reaches this many ms are reported to `onSlowRequest`.
|
|
39
|
+
* Falls back to `GQL_SLOW_LOG_MS`, then 2000. 0 disables the timing entirely.
|
|
40
|
+
*/
|
|
41
|
+
slowLogMs?: number;
|
|
42
|
+
/** Receives slow-request reports. Defaults to one `console.warn('[gql] slow request', json)` line. */
|
|
43
|
+
onSlowRequest?: (report: SlowRequestReport) => void;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Per root field stats inside one request. graphql-js starts every root field of a batch in the same tick and their
|
|
47
|
+
* awaits interleave, so a field's wall time also covers the other fields running alongside it: `wallMaxMs` is at
|
|
48
|
+
* most `executeMs`, and `wallSumMs` can exceed it. Use `count` to see what the batch was made of and `executeMs`
|
|
49
|
+
* for how long the event loop was busy.
|
|
50
|
+
*/
|
|
51
|
+
interface SlowRequestFieldStat {
|
|
52
|
+
count: number;
|
|
53
|
+
wallSumMs: number;
|
|
54
|
+
wallMaxMs: number;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Shape-only description of a slow request. Never carries argument values or variables: addresses
|
|
58
|
+
* and other inputs stay in the access log, which already records the client and time.
|
|
59
|
+
*/
|
|
60
|
+
interface SlowRequestReport {
|
|
61
|
+
totalMs: number;
|
|
62
|
+
parseMs: number;
|
|
63
|
+
executeMs: number;
|
|
64
|
+
serializeMs: number;
|
|
65
|
+
status: number;
|
|
66
|
+
responseChars: number;
|
|
67
|
+
queryChars: number;
|
|
68
|
+
operationName: string | null;
|
|
69
|
+
topLevel: number;
|
|
70
|
+
selections: number;
|
|
71
|
+
aliases: number;
|
|
72
|
+
numericAliases: number;
|
|
73
|
+
errors: number;
|
|
74
|
+
fields: Record<string, SlowRequestFieldStat>;
|
|
75
|
+
userAgent?: string;
|
|
37
76
|
}
|
|
38
77
|
/**
|
|
39
78
|
* Transport-agnostic view of an incoming GraphQL request, so the same engine serves express (`createHttpHandler`)
|
|
@@ -76,4 +115,4 @@ declare const createHttpHandler: (options: HttpHandlerOptions) => RequestHandler
|
|
|
76
115
|
*/
|
|
77
116
|
declare const createFetchHandler: (options: HttpHandlerOptions) => (request: globalThis.Request) => Promise<globalThis.Response>;
|
|
78
117
|
//#endregion
|
|
79
|
-
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
|
118
|
+
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, type Resolver, SlowRequestFieldStat, SlowRequestReport, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
package/lib/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.cjs";
|
|
1
|
+
import { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, SlowRequestFieldStat, SlowRequestReport, createFetchHandler, createHttpHandler, createResolvers, formatContext, normalizeNumericFieldAliases, wrapResolver } from "./http.cjs";
|
|
2
2
|
import { TIndexedAccountState, TIndexedAssetState, TIndexedDelegationState, TIndexedFactoryState, TIndexedRollupBlock, TIndexedRollupState, TIndexedStakeState, TIndexedTokenState, TIndexedTransaction } from "@ocap/types";
|
|
3
3
|
import { Express } from "express";
|
|
4
4
|
import { Server } from "node:http";
|
|
@@ -39,4 +39,4 @@ declare const createSocketHandler: ({
|
|
|
39
39
|
wsOptions
|
|
40
40
|
}: SocketHandlerOptions) => Server;
|
|
41
41
|
//#endregion
|
|
42
|
-
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
|
42
|
+
export { GqlRequestLike, GqlResponseLike, HttpHandlerOptions, Resolver, SlowRequestFieldStat, SlowRequestReport, createFetchHandler, createHttpHandler, createResolvers, createSocketHandler, formatContext, normalizeNumericFieldAliases, wrapResolver };
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.31.
|
|
6
|
+
"version": "1.31.6",
|
|
7
7
|
"description": "Resolver middleware for ocap adapters",
|
|
8
8
|
"type": "module",
|
|
9
9
|
"main": "./lib/index.cjs",
|
|
@@ -51,8 +51,8 @@
|
|
|
51
51
|
],
|
|
52
52
|
"license": "MIT",
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@arcblock/ws": "1.31.
|
|
55
|
-
"@ocap/schema": "1.31.
|
|
54
|
+
"@arcblock/ws": "1.31.6",
|
|
55
|
+
"@ocap/schema": "1.31.6",
|
|
56
56
|
"graphql": "^16.13.2",
|
|
57
57
|
"lodash": "^4.17.23"
|
|
58
58
|
},
|