Skip to content

Commit fde673b

Browse files
committed
fix: test errors
1 parent 1abad53 commit fde673b

File tree

10 files changed

+64
-35
lines changed

10 files changed

+64
-35
lines changed

internal/test-utils/src/client.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assert from 'assert';
33
import {
44
Cache,
55
createClient,
6+
GQtyError,
67
type BaseGeneratedSchema,
78
type GQtyClient,
89
type QueryPayload,
@@ -295,13 +296,25 @@ export const createInMemoryClient = async <TSchema extends BaseGeneratedSchema>(
295296
fetcher: async ({ query, variables, operationName }) => {
296297
await options.onFetch?.({ query, variables, operationName });
297298

298-
const res = await executor({
299+
const result = await executor({
299300
document: parse(query),
300301
variables,
301302
operationName,
303+
}).then((result) => {
304+
if (Symbol.asyncIterator in result) {
305+
return result[Symbol.asyncIterator]()
306+
.next()
307+
.then((res) => res.value as ExecutionResult<any, any>);
308+
} else {
309+
return result;
310+
}
302311
});
303312

304-
return res as never;
313+
if (result.errors?.length) {
314+
throw GQtyError.fromGraphQLErrors(result.errors);
315+
}
316+
317+
return result;
305318
},
306319
subscriber: new MockWsClient(executor),
307320
},

packages/gqty/src/Accessor/resolve.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ const objectProxyHandler: ProxyHandler<GeneratedSchemaObject> = {
254254
const { __args, __type } = targetType;
255255
if (__args) {
256256
return (args?: Record<string, unknown>) => {
257-
const alias = meta.context.aliasGenerator?.(
258-
meta.selection.ancestry.map((s) => s.key.toString()).concat(key),
259-
args
260-
);
257+
const keys = meta.selection.ancestry
258+
.map((s) => s.key.toString())
259+
.concat(key);
260+
const alias = meta.context.aliasGenerator?.(keys, args);
261261
const input: SelectionInput = {};
262262

263263
if (args) {
@@ -270,14 +270,12 @@ const objectProxyHandler: ProxyHandler<GeneratedSchemaObject> = {
270270
}
271271
}
272272

273-
return resolve(
274-
proxy,
275-
meta.selection.getChild(
276-
key,
277-
args ? { alias: alias?.field, input } : {}
278-
),
279-
__type
273+
const child = meta.selection.getChild(
274+
key,
275+
args ? { alias: alias?.field, input } : {}
280276
);
277+
278+
return resolve(proxy, child, __type);
281279
};
282280
}
283281

packages/gqty/src/Helpers/prepass.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function prepass<T extends object | null | undefined>(
3131
s.input
3232
? {
3333
field: `${s.key}`,
34-
variables: s.input.values,
34+
variables: s.inputValues,
3535
}
3636
: `${s.key}`
3737
);

packages/gqty/src/Selection.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ export class Selection {
4343
return this.options.input;
4444
}
4545

46+
/**
47+
* Stripping alias and type information from inputs, returning a bare mapping
48+
* of variable names and values.
49+
*/
50+
get inputValues() {
51+
if (!this.input) return;
52+
53+
return Object.fromEntries(
54+
Object.entries(this.input).map(([key, { value }]) => [key, value])
55+
);
56+
}
57+
4658
/** Indicates current selection being a inteface/union key. */
4759
get isUnion() {
4860
return this.options.isUnion ?? false;
@@ -142,7 +154,7 @@ export class Selection {
142154

143155
toString() {
144156
return `Selection(${this.cacheKeys.join('.')}) ${JSON.stringify(
145-
this.input?.values ?? {}
157+
this.inputValues ?? {}
146158
)}`;
147159
}
148160
}

packages/gqty/src/Utils/pick.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export const pick = (
99

1010
for (const { ancestry } of selections) {
1111
let srcNode = schema;
12-
for (const { key, input } of ancestry) {
12+
for (const { key, inputValues } of ancestry) {
1313
if (srcNode == null) break;
1414

1515
if (typeof srcNode[key] === 'function') {
16-
srcNode = srcNode[key](input?.values);
16+
srcNode = srcNode[key](inputValues);
1717
} else {
1818
srcNode = srcNode[key];
1919
}

packages/gqty/test/selection.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ describe('selection creation', () => {
1010
expect(selectionA.alias).toBe(undefined);
1111
expect(selectionA.root.key).toBe('mutation');
1212

13-
expect(selectionA.input?.values).toBe(undefined);
14-
expect(selectionA.input?.types).toBe(undefined);
13+
expect(selectionA.input).toBe(undefined);
1514
expect(selectionA.ancestry).toEqual([mutationRoot, selectionA]);
1615

1716
expect(selectionA.cacheKeys).toEqual(['mutation', 'a']);

packages/logger/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ export function createLogger(
162162
console.groupCollapsed(...format(['Selections', headerStyles]));
163163
for (const [
164164
,
165-
{ key, cacheKeys, alias, input, isUnion },
165+
{ key, cacheKeys, alias, inputValues, isUnion },
166166
] of uniqueSelections) {
167167
console.log(
168168
stringifyJSONIfEnabled({
169169
key,
170170
cacheKeys: cacheKeys.join('.'),
171171
alias,
172-
input,
172+
input: inputValues,
173173
isUnion,
174174
})
175175
);

packages/react/src/query/useQuery.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ export const createUseQuery = <TSchema extends BaseGeneratedSchema>(
461461
fetchInBackground,
462462
operationName,
463463
selections,
464+
state,
464465
]
465466
);
466467

packages/react/test/useQuery.test.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-unused-expressions */
12
import { renderHook, waitFor } from '@testing-library/react';
23
import { Cache, type QueryPayload } from 'gqty';
34
import { act } from 'react';
@@ -17,7 +18,7 @@ describe('useQuery', () => {
1718

1819
results.push(query.$state.isLoading);
1920

20-
query.time;
21+
Reflect.get(query, 'time');
2122

2223
return query.$state.isLoading;
2324
});
@@ -146,16 +147,16 @@ describe('useQuery', () => {
146147
[
147148
{
148149
"operationName": undefined,
149-
"query": "query($a00425:String){a2c936:human(name:$a00425){__typename id name}}",
150+
"query": "query($a69281:String){a7b317:human(name:$a69281){__typename id name}}",
150151
"variables": {
151-
"a00425": "1",
152+
"a69281": "1",
152153
},
153154
},
154155
{
155156
"operationName": undefined,
156-
"query": "query($dd0895:String){a657eb:human(name:$dd0895){__typename id name}}",
157+
"query": "query($a7c6a0:String){dc91d3:human(name:$a7c6a0){__typename id name}}",
157158
"variables": {
158-
"dd0895": "2",
159+
"a7c6a0": "2",
159160
},
160161
},
161162
]
@@ -281,6 +282,7 @@ describe('useQuery', () => {
281282
const { result } = renderHook(() => {
282283
const query = useQuery({
283284
initialLoadingState: true,
285+
suspense: true,
284286
});
285287

286288
// Empty array
@@ -299,9 +301,11 @@ describe('useQuery', () => {
299301
// This should NOT trigger a SWR refetch
300302
await act(() => result.current.$refetch(false));
301303

304+
expect(result.current.$state.error).toBeUndefined();
305+
302306
expect(queries).toMatchInlineSnapshot(`
303307
[
304-
"query($a2a039:ID!){eb2884:pet(id:$a2a039){__typename id owner{__typename id name}}now peoples{__typename id name}}",
308+
"query($a1a2bc:ID!){a1648a:pet(id:$a1a2bc){__typename id owner{__typename id name}}now peoples{__typename id name}}",
305309
]
306310
`);
307311

@@ -311,10 +315,12 @@ describe('useQuery', () => {
311315
// This should trigger a SWR refetch
312316
await act(() => result.current.$refetch(false));
313317

318+
expect(result.current.$state.error).toBeUndefined();
319+
314320
expect(queries).toMatchInlineSnapshot(`
315321
[
316-
"query($a2a039:ID!){eb2884:pet(id:$a2a039){__typename id owner{__typename id name}}now peoples{__typename id name}}",
317-
"query($a2a039:ID!){eb2884:pet(id:$a2a039){__typename id owner{__typename id name}}now peoples{__typename id name}}",
322+
"query($a1a2bc:ID!){a1648a:pet(id:$a1a2bc){__typename id owner{__typename id name}}now peoples{__typename id name}}",
323+
"query($a1a2bc:ID!){a1648a:pet(id:$a1a2bc){__typename id owner{__typename id name}}now peoples{__typename id name}}",
318324
]
319325
`);
320326
});

packages/solid/src/query.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,23 @@ describe('createQuery', () => {
179179
[
180180
{
181181
"operationName": undefined,
182-
"query": "query($e61a8e:ID!){a02d2c:people(id:$e61a8e){__typename id name}}",
182+
"query": "query($a176f8:ID!){a66965:people(id:$a176f8){__typename id name}}",
183183
"variables": {
184-
"e61a8e": "1",
184+
"a176f8": "1",
185185
},
186186
},
187187
{
188188
"operationName": undefined,
189-
"query": "query($d6d931:ID!){e084c7:people(id:$d6d931){__typename id name}}",
189+
"query": "query($a482a1:ID!){eebe6f:people(id:$a482a1){__typename id name}}",
190190
"variables": {
191-
"d6d931": "2",
191+
"a482a1": "2",
192192
},
193193
},
194194
{
195195
"operationName": undefined,
196-
"query": "query($a60dd8:ID!){d2f785:people(id:$a60dd8){__typename id name}}",
196+
"query": "query($b8233c:ID!){c0c0aa:people(id:$b8233c){__typename id name}}",
197197
"variables": {
198-
"a60dd8": "3",
198+
"b8233c": "3",
199199
},
200200
},
201201
]

0 commit comments

Comments
 (0)