@livestore/graphql 0.4.0-dev.9 → 0.5.0-dev.0
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/dist/.tsbuildinfo +1 -1
- package/dist/graphql.d.ts +1 -1
- package/dist/graphql.d.ts.map +1 -1
- package/dist/graphql.js +17 -16
- package/dist/graphql.js.map +1 -1
- package/package.json +44 -13
- package/src/graphql.ts +27 -21
package/dist/graphql.d.ts
CHANGED
|
@@ -23,7 +23,7 @@ export type GraphQLOptions<TContext> = {
|
|
|
23
23
|
schema: GraphQLSchema;
|
|
24
24
|
makeContext: (db: SqliteDbWrapper, tracer: otel.Tracer, sessionId: string) => TContext;
|
|
25
25
|
};
|
|
26
|
-
export type MapResult<To, From> = ((res: From, get: LiveQueries.GetAtomResult) => To) | Schema.
|
|
26
|
+
export type MapResult<To, From> = ((res: From, get: LiveQueries.GetAtomResult) => To) | Schema.Codec<To, From>;
|
|
27
27
|
export declare const queryGraphQL: <TResult extends Record<string, any>, TVariableValues extends Record<string, any>, TResultMapped extends Record<string, any> = TResult>(document: DocumentNode<TResult, TVariableValues>, genVariableValues: TVariableValues | ((get: LiveQueries.GetAtomResult) => TVariableValues), options?: {
|
|
28
28
|
label?: string;
|
|
29
29
|
map?: MapResult<TResultMapped, TResult>;
|
package/dist/graphql.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,mCAAmC,CAAA;
|
|
1
|
+
{"version":3,"file":"graphql.d.ts","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,IAAI,YAAY,EAAE,MAAM,mCAAmC,CAAA;AAC1F,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,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;QACD,QAAQ,EAAE,YAAY,CAAC,OAAO,EAAE,eAAe,CAAC,CAAA;QAChD,iBAAiB,EAAE,eAAe,GAAG,CAAC,CAAC,GAAG,EAAE,WAAW,CAAC,aAAa,KAAK,eAAe,CAAC,CAAA;QAC1F,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,eAAe,EAAE,WAAW,CAAC,eAAe,CAAA;QAC5C,GAAG,CAAC,EAAE,SAAS,CAAC,aAAa,EAAE,OAAO,CAAC,CAAA;QACvC,GAAG,EAAE,WAAW,CAAC,YAAY,CAAC,aAAa,CAAC,CAAA;KAC7C;IAkFD,SAAS,GAAI,oEAOV;QACD,QAAQ,EAAE,OAAO,CAAC,YAAY,CAAA;QAC9B,WAAW,EAAE,IAAI,CAAC,OAAO,CAAA;QACzB,UAAU,EAAE,IAAI,CAAC,MAAM,CAAA;QACvB,cAAc,EAAE,eAAe,CAAA;QAC/B,GAAG,EAAE,WAAW,CAAC,aAAa,CAAA;QAC9B,KAAK,EAAE,KAAK,CAAA;KACb;;;;MAkDA;IAED,OAAO,aAUN;CACF"}
|
package/dist/graphql.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StoreInternalsSymbol } from '@livestore/livestore';
|
|
2
2
|
import { LiveQueries, ReactiveGraph } from '@livestore/livestore/internal';
|
|
3
|
-
import { omitUndefineds, shouldNeverHappen } from '@livestore/utils';
|
|
4
|
-
import { Equal, Hash, Predicate, Schema,
|
|
3
|
+
import { objectToString, omitUndefineds, shouldNeverHappen } from '@livestore/utils';
|
|
4
|
+
import { Equal, Hash, Predicate, Result, Schema, SchemaIssue } from '@livestore/utils/effect';
|
|
5
5
|
import * as otel from '@opentelemetry/api';
|
|
6
6
|
import * as graphql from 'graphql';
|
|
7
7
|
export const queryGraphQL = (document, genVariableValues, options = {}) => {
|
|
8
8
|
const documentName = graphql.getOperationAST(document)?.name?.value;
|
|
9
|
-
const hash = options.deps
|
|
9
|
+
const hash = options.deps !== undefined
|
|
10
10
|
? LiveQueries.depsToString(options.deps)
|
|
11
11
|
: (documentName ?? shouldNeverHappen('No document name found and no deps provided'));
|
|
12
12
|
const label = options.label ?? documentName ?? 'graphql';
|
|
@@ -55,20 +55,20 @@ export class LiveStoreGraphQLQuery extends LiveQueries.LiveStoreQueryBase {
|
|
|
55
55
|
this.mapResult =
|
|
56
56
|
map === undefined
|
|
57
57
|
? (res) => res
|
|
58
|
-
: Schema.isSchema(map)
|
|
58
|
+
: Schema.isSchema(map) === true
|
|
59
59
|
? (res) => {
|
|
60
|
-
const parseResult = Schema.
|
|
61
|
-
if (parseResult
|
|
62
|
-
console.error(`Error parsing GraphQL query result: ${
|
|
63
|
-
return shouldNeverHappen(`Error parsing SQL query result: ${parseResult.
|
|
60
|
+
const parseResult = Schema.decodeResult(map)(res);
|
|
61
|
+
if (Result.isFailure(parseResult) === true) {
|
|
62
|
+
console.error(`Error parsing GraphQL query result: ${SchemaIssue.makeFormatterDefault()(parseResult.failure.issue)}`);
|
|
63
|
+
return shouldNeverHappen(`Error parsing SQL query result: ${String(parseResult.failure)}`);
|
|
64
64
|
}
|
|
65
65
|
else {
|
|
66
|
-
return parseResult.
|
|
66
|
+
return parseResult.success;
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
: typeof map === 'function'
|
|
70
70
|
? map
|
|
71
|
-
: shouldNeverHappen(`Invalid map function ${map}`);
|
|
71
|
+
: shouldNeverHappen(`Invalid map function ${objectToString(map)}`);
|
|
72
72
|
// TODO don't even create a thunk if variables are static
|
|
73
73
|
let variableValues$OrvariableValues;
|
|
74
74
|
if (typeof genVariableValues === 'function') {
|
|
@@ -83,7 +83,7 @@ export class LiveStoreGraphQLQuery extends LiveQueries.LiveStoreQueryBase {
|
|
|
83
83
|
const resultsLabel = `${labelWithDefault}:results`;
|
|
84
84
|
this.results$ = this.reactivityGraph.makeThunk((get, setDebugInfo, ctx, otelContext, debugRefreshReason) => {
|
|
85
85
|
const { store, otelTracer, rootOtelContext } = ctx;
|
|
86
|
-
const variableValues = ReactiveGraph.isThunk(variableValues$OrvariableValues)
|
|
86
|
+
const variableValues = ReactiveGraph.isThunk(variableValues$OrvariableValues) === true
|
|
87
87
|
? get(variableValues$OrvariableValues, otelContext, debugRefreshReason)
|
|
88
88
|
: variableValues$OrvariableValues;
|
|
89
89
|
const { result, queriedTables, durationMs } = this.queryOnce({
|
|
@@ -96,7 +96,7 @@ export class LiveStoreGraphQLQuery extends LiveQueries.LiveStoreQueryBase {
|
|
|
96
96
|
});
|
|
97
97
|
// Add dependencies on any tables that were used
|
|
98
98
|
for (const tableName of queriedTables) {
|
|
99
|
-
const tableRef = store.tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`);
|
|
99
|
+
const tableRef = store[StoreInternalsSymbol].tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`);
|
|
100
100
|
get(tableRef);
|
|
101
101
|
}
|
|
102
102
|
setDebugInfo({ _tag: 'graphql', label: resultsLabel, query: graphql.print(document), durationMs });
|
|
@@ -107,6 +107,7 @@ export class LiveStoreGraphQLQuery extends LiveQueries.LiveStoreQueryBase {
|
|
|
107
107
|
const { schema, context } = unpackStoreContext(store);
|
|
108
108
|
const operationName = graphql.getOperationAST(document)?.name?.value;
|
|
109
109
|
return otelTracer.startActiveSpan(`executeGraphQLQuery: ${operationName}`, {}, otelContext, (span) => {
|
|
110
|
+
const startTimePerfNow = performance.now();
|
|
110
111
|
span.setAttribute('graphql.variables', JSON.stringify(variableValues));
|
|
111
112
|
span.setAttribute('graphql.query', graphql.print(document));
|
|
112
113
|
context.queriedTables.clear();
|
|
@@ -118,7 +119,7 @@ export class LiveStoreGraphQLQuery extends LiveQueries.LiveStoreQueryBase {
|
|
|
118
119
|
variableValues,
|
|
119
120
|
});
|
|
120
121
|
// TODO track number of nested SQL queries via Otel + debug info
|
|
121
|
-
if (res.errors) {
|
|
122
|
+
if (res.errors !== undefined) {
|
|
122
123
|
span.setStatus({ code: otel.SpanStatusCode.ERROR, message: 'GraphQL error' });
|
|
123
124
|
span.setAttribute('graphql.error', res.errors.join('\n'));
|
|
124
125
|
span.setAttribute('graphql.error-detail', JSON.stringify(res.errors));
|
|
@@ -126,13 +127,13 @@ export class LiveStoreGraphQLQuery extends LiveQueries.LiveStoreQueryBase {
|
|
|
126
127
|
for (const error of res.errors) {
|
|
127
128
|
console.error(error);
|
|
128
129
|
}
|
|
129
|
-
//
|
|
130
|
+
// oxlint-disable-next-line eslint(no-debugger) -- intentional breakpoint for GraphQL errors
|
|
130
131
|
debugger;
|
|
131
132
|
shouldNeverHappen(`GraphQL error: ${res.errors.join('\n')}`);
|
|
132
133
|
}
|
|
133
134
|
span.end();
|
|
134
135
|
const result = this.mapResult(res.data, get);
|
|
135
|
-
const durationMs =
|
|
136
|
+
const durationMs = performance.now() - startTimePerfNow;
|
|
136
137
|
this.executionTimes.push(durationMs);
|
|
137
138
|
return {
|
|
138
139
|
result,
|
package/dist/graphql.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"graphql.js","sourceRoot":"","sources":["../src/graphql.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAA;AAC3D,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAC1E,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpF,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AAC7F,OAAO,KAAK,IAAI,MAAM,oBAAoB,CAAA;AAE1C,OAAO,KAAK,OAAO,MAAM,SAAS,CAAA;AA2BlC,MAAM,CAAC,MAAM,YAAY,GAAG,CAK1B,QAAgD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,OAAO,iBAAiB,CAAC,+CAA+C,CAAC,CAAA;IAC3E,CAAC;IACD,IAAI,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,SAAS,CAAC,KAAK,KAAK,EAAE,CAAC;QACtE,OAAO,iBAAiB,CAAC,gDAAgD,CAAC,CAAA;IAC5E,CAAC;IACD,MAAM,MAAM,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,MAA+B,CAAA;IACpE,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,OAAgC,CAAA;IACtE,IAAI,OAAO,CAAC,OAAO,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QACvC,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACzC,OAAO,CAAC,OAAO,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAA;IAC7C,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;AACnD,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,31 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@livestore/graphql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0-dev.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/livestorejs/livestore-contrib.git"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist",
|
|
11
|
+
"package.json",
|
|
12
|
+
"src"
|
|
13
|
+
],
|
|
4
14
|
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
5
16
|
"exports": {
|
|
6
17
|
".": "./dist/index.js"
|
|
7
18
|
},
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
8
22
|
"dependencies": {
|
|
9
23
|
"@graphql-typed-document-node/core": "3.2.0",
|
|
10
|
-
"@
|
|
11
|
-
"@livestore/
|
|
12
|
-
"@livestore/utils": "0.
|
|
13
|
-
"@
|
|
24
|
+
"@livestore/common": "0.5.0-dev.0",
|
|
25
|
+
"@livestore/livestore": "0.5.0-dev.0",
|
|
26
|
+
"@livestore/utils": "0.5.0-dev.0",
|
|
27
|
+
"@opentelemetry/api": "1.9.0"
|
|
14
28
|
},
|
|
15
29
|
"devDependencies": {
|
|
16
30
|
"graphql": "16.11.0"
|
|
17
31
|
},
|
|
18
|
-
"files": [
|
|
19
|
-
"package.json",
|
|
20
|
-
"src",
|
|
21
|
-
"dist"
|
|
22
|
-
],
|
|
23
|
-
"license": "Apache-2.0",
|
|
24
32
|
"peerDependencies": {
|
|
33
|
+
"@effect/opentelemetry": "^4.0.0-rc.111",
|
|
34
|
+
"@effect/platform-browser": "^4.0.0-rc.111",
|
|
35
|
+
"@effect/platform-bun": "^4.0.0-rc.111",
|
|
36
|
+
"@effect/platform-node": "^4.0.0-rc.111",
|
|
37
|
+
"@effect/platform-node-shared": "^4.0.0-rc.111",
|
|
38
|
+
"@effect/vitest": "^4.0.0-rc.111",
|
|
39
|
+
"@opentelemetry/api": "^1.9.0",
|
|
40
|
+
"@opentelemetry/resources": "^2.2.0",
|
|
41
|
+
"@standard-schema/spec": "^1.1.0",
|
|
42
|
+
"effect": "^4.0.0-rc.111",
|
|
25
43
|
"graphql": "^16.11.0"
|
|
26
44
|
},
|
|
27
|
-
"
|
|
28
|
-
"
|
|
45
|
+
"$genie": {
|
|
46
|
+
"source": "package.json.genie.ts",
|
|
47
|
+
"warning": "DO NOT EDIT - changes will be overwritten",
|
|
48
|
+
"workspaceClosureDirs": [
|
|
49
|
+
"packages/@livestore/graphql",
|
|
50
|
+
"repos/livestore/packages/@livestore/adapter-web",
|
|
51
|
+
"repos/livestore/packages/@livestore/common",
|
|
52
|
+
"repos/livestore/packages/@livestore/common-cf",
|
|
53
|
+
"repos/livestore/packages/@livestore/livestore",
|
|
54
|
+
"repos/livestore/packages/@livestore/sqlite-wasm",
|
|
55
|
+
"repos/livestore/packages/@livestore/utils",
|
|
56
|
+
"repos/livestore/packages/@livestore/utils-dev",
|
|
57
|
+
"repos/livestore/packages/@livestore/wa-sqlite",
|
|
58
|
+
"repos/livestore/packages/@livestore/webmesh"
|
|
59
|
+
]
|
|
29
60
|
},
|
|
30
61
|
"scripts": {
|
|
31
62
|
"test": "echo 'No tests'"
|
package/src/graphql.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'
|
|
2
|
-
import { getDurationMsFromSpan } from '@livestore/common'
|
|
3
2
|
import type { RefreshReason, SqliteDbWrapper, Store } from '@livestore/livestore'
|
|
3
|
+
import { StoreInternalsSymbol } from '@livestore/livestore'
|
|
4
4
|
import { LiveQueries, ReactiveGraph } from '@livestore/livestore/internal'
|
|
5
|
-
import { omitUndefineds, shouldNeverHappen } from '@livestore/utils'
|
|
6
|
-
import { Equal, Hash, Predicate, Schema,
|
|
5
|
+
import { objectToString, omitUndefineds, shouldNeverHappen } from '@livestore/utils'
|
|
6
|
+
import { Equal, Hash, Predicate, Result, Schema, SchemaIssue } from '@livestore/utils/effect'
|
|
7
7
|
import * as otel from '@opentelemetry/api'
|
|
8
8
|
import type { GraphQLSchema } from 'graphql'
|
|
9
9
|
import * as graphql from 'graphql'
|
|
@@ -31,7 +31,7 @@ export type GraphQLOptions<TContext> = {
|
|
|
31
31
|
makeContext: (db: SqliteDbWrapper, tracer: otel.Tracer, sessionId: string) => TContext
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
export type MapResult<To, From> = ((res: From, get: LiveQueries.GetAtomResult) => To) | Schema.
|
|
34
|
+
export type MapResult<To, From> = ((res: From, get: LiveQueries.GetAtomResult) => To) | Schema.Codec<To, From>
|
|
35
35
|
|
|
36
36
|
export const queryGraphQL = <
|
|
37
37
|
TResult extends Record<string, any>,
|
|
@@ -48,9 +48,10 @@ export const queryGraphQL = <
|
|
|
48
48
|
} = {},
|
|
49
49
|
): LiveQueries.LiveQueryDef<TResultMapped> => {
|
|
50
50
|
const documentName = graphql.getOperationAST(document)?.name?.value
|
|
51
|
-
const hash =
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
const hash =
|
|
52
|
+
options.deps !== undefined
|
|
53
|
+
? LiveQueries.depsToString(options.deps)
|
|
54
|
+
: (documentName ?? shouldNeverHappen('No document name found and no deps provided'))
|
|
54
55
|
const label = options.label ?? documentName ?? 'graphql'
|
|
55
56
|
const map = options.map
|
|
56
57
|
|
|
@@ -130,19 +131,21 @@ export class LiveStoreGraphQLQuery<
|
|
|
130
131
|
this.mapResult =
|
|
131
132
|
map === undefined
|
|
132
133
|
? (res: TResult) => res as any as TResultMapped
|
|
133
|
-
: Schema.isSchema(map)
|
|
134
|
+
: Schema.isSchema(map) === true
|
|
134
135
|
? (res: TResult) => {
|
|
135
|
-
const parseResult = Schema.
|
|
136
|
-
if (parseResult
|
|
137
|
-
console.error(
|
|
138
|
-
|
|
136
|
+
const parseResult = Schema.decodeResult(map as Schema.Codec<TResultMapped, TResult>)(res)
|
|
137
|
+
if (Result.isFailure(parseResult) === true) {
|
|
138
|
+
console.error(
|
|
139
|
+
`Error parsing GraphQL query result: ${SchemaIssue.makeFormatterDefault()(parseResult.failure.issue)}`,
|
|
140
|
+
)
|
|
141
|
+
return shouldNeverHappen(`Error parsing SQL query result: ${String(parseResult.failure)}`)
|
|
139
142
|
} else {
|
|
140
|
-
return parseResult.
|
|
143
|
+
return parseResult.success
|
|
141
144
|
}
|
|
142
145
|
}
|
|
143
146
|
: typeof map === 'function'
|
|
144
147
|
? map
|
|
145
|
-
: shouldNeverHappen(`Invalid map function ${map}`)
|
|
148
|
+
: shouldNeverHappen(`Invalid map function ${objectToString(map)}`)
|
|
146
149
|
|
|
147
150
|
// TODO don't even create a thunk if variables are static
|
|
148
151
|
let variableValues$OrvariableValues:
|
|
@@ -167,9 +170,10 @@ export class LiveStoreGraphQLQuery<
|
|
|
167
170
|
this.results$ = this.reactivityGraph.makeThunk<TResultMapped>(
|
|
168
171
|
(get, setDebugInfo, ctx, otelContext, debugRefreshReason) => {
|
|
169
172
|
const { store, otelTracer, rootOtelContext } = ctx
|
|
170
|
-
const variableValues =
|
|
171
|
-
|
|
172
|
-
|
|
173
|
+
const variableValues =
|
|
174
|
+
ReactiveGraph.isThunk(variableValues$OrvariableValues) === true
|
|
175
|
+
? (get(variableValues$OrvariableValues, otelContext, debugRefreshReason) as TVariableValues)
|
|
176
|
+
: (variableValues$OrvariableValues as TVariableValues)
|
|
173
177
|
const { result, queriedTables, durationMs } = this.queryOnce({
|
|
174
178
|
document,
|
|
175
179
|
variableValues,
|
|
@@ -181,7 +185,8 @@ export class LiveStoreGraphQLQuery<
|
|
|
181
185
|
|
|
182
186
|
// Add dependencies on any tables that were used
|
|
183
187
|
for (const tableName of queriedTables) {
|
|
184
|
-
const tableRef =
|
|
188
|
+
const tableRef =
|
|
189
|
+
store[StoreInternalsSymbol].tableRefs[tableName] ?? shouldNeverHappen(`No table ref found for ${tableName}`)
|
|
185
190
|
get(tableRef)
|
|
186
191
|
}
|
|
187
192
|
|
|
@@ -214,6 +219,7 @@ export class LiveStoreGraphQLQuery<
|
|
|
214
219
|
const operationName = graphql.getOperationAST(document)?.name?.value
|
|
215
220
|
|
|
216
221
|
return otelTracer.startActiveSpan(`executeGraphQLQuery: ${operationName}`, {}, otelContext, (span) => {
|
|
222
|
+
const startTimePerfNow = performance.now()
|
|
217
223
|
span.setAttribute('graphql.variables', JSON.stringify(variableValues))
|
|
218
224
|
span.setAttribute('graphql.query', graphql.print(document))
|
|
219
225
|
|
|
@@ -230,7 +236,7 @@ export class LiveStoreGraphQLQuery<
|
|
|
230
236
|
|
|
231
237
|
// TODO track number of nested SQL queries via Otel + debug info
|
|
232
238
|
|
|
233
|
-
if (res.errors) {
|
|
239
|
+
if (res.errors !== undefined) {
|
|
234
240
|
span.setStatus({ code: otel.SpanStatusCode.ERROR, message: 'GraphQL error' })
|
|
235
241
|
span.setAttribute('graphql.error', res.errors.join('\n'))
|
|
236
242
|
span.setAttribute('graphql.error-detail', JSON.stringify(res.errors))
|
|
@@ -238,7 +244,7 @@ export class LiveStoreGraphQLQuery<
|
|
|
238
244
|
for (const error of res.errors) {
|
|
239
245
|
console.error(error)
|
|
240
246
|
}
|
|
241
|
-
//
|
|
247
|
+
// oxlint-disable-next-line eslint(no-debugger) -- intentional breakpoint for GraphQL errors
|
|
242
248
|
debugger
|
|
243
249
|
shouldNeverHappen(`GraphQL error: ${res.errors.join('\n')}`)
|
|
244
250
|
}
|
|
@@ -247,7 +253,7 @@ export class LiveStoreGraphQLQuery<
|
|
|
247
253
|
|
|
248
254
|
const result = this.mapResult(res.data as unknown as TResult, get)
|
|
249
255
|
|
|
250
|
-
const durationMs =
|
|
256
|
+
const durationMs = performance.now() - startTimePerfNow
|
|
251
257
|
|
|
252
258
|
this.executionTimes.push(durationMs)
|
|
253
259
|
|