Skip to content

Commit

Permalink
fix: test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Sep 8, 2024
1 parent 1abad53 commit fde673b
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 35 deletions.
17 changes: 15 additions & 2 deletions internal/test-utils/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import assert from 'assert';
import {
Cache,
createClient,
GQtyError,
type BaseGeneratedSchema,
type GQtyClient,
type QueryPayload,
Expand Down Expand Up @@ -295,13 +296,25 @@ export const createInMemoryClient = async <TSchema extends BaseGeneratedSchema>(
fetcher: async ({ query, variables, operationName }) => {
await options.onFetch?.({ query, variables, operationName });

const res = await executor({
const result = await executor({
document: parse(query),
variables,
operationName,
}).then((result) => {
if (Symbol.asyncIterator in result) {
return result[Symbol.asyncIterator]()
.next()
.then((res) => res.value as ExecutionResult<any, any>);
} else {
return result;
}
});

return res as never;
if (result.errors?.length) {
throw GQtyError.fromGraphQLErrors(result.errors);
}

return result;
},
subscriber: new MockWsClient(executor),
},
Expand Down
20 changes: 9 additions & 11 deletions packages/gqty/src/Accessor/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ const objectProxyHandler: ProxyHandler<GeneratedSchemaObject> = {
const { __args, __type } = targetType;
if (__args) {
return (args?: Record<string, unknown>) => {
const alias = meta.context.aliasGenerator?.(
meta.selection.ancestry.map((s) => s.key.toString()).concat(key),
args
);
const keys = meta.selection.ancestry
.map((s) => s.key.toString())
.concat(key);
const alias = meta.context.aliasGenerator?.(keys, args);
const input: SelectionInput = {};

if (args) {
Expand All @@ -270,14 +270,12 @@ const objectProxyHandler: ProxyHandler<GeneratedSchemaObject> = {
}
}

return resolve(
proxy,
meta.selection.getChild(
key,
args ? { alias: alias?.field, input } : {}
),
__type
const child = meta.selection.getChild(
key,
args ? { alias: alias?.field, input } : {}
);

return resolve(proxy, child, __type);
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/gqty/src/Helpers/prepass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function prepass<T extends object | null | undefined>(
s.input
? {
field: `${s.key}`,
variables: s.input.values,
variables: s.inputValues,
}
: `${s.key}`
);
Expand Down
14 changes: 13 additions & 1 deletion packages/gqty/src/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ export class Selection {
return this.options.input;
}

/**
* Stripping alias and type information from inputs, returning a bare mapping
* of variable names and values.
*/
get inputValues() {
if (!this.input) return;

return Object.fromEntries(
Object.entries(this.input).map(([key, { value }]) => [key, value])
);
}

/** Indicates current selection being a inteface/union key. */
get isUnion() {
return this.options.isUnion ?? false;
Expand Down Expand Up @@ -142,7 +154,7 @@ export class Selection {

toString() {
return `Selection(${this.cacheKeys.join('.')}) ${JSON.stringify(
this.input?.values ?? {}
this.inputValues ?? {}
)}`;
}
}
4 changes: 2 additions & 2 deletions packages/gqty/src/Utils/pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export const pick = (

for (const { ancestry } of selections) {
let srcNode = schema;
for (const { key, input } of ancestry) {
for (const { key, inputValues } of ancestry) {
if (srcNode == null) break;

if (typeof srcNode[key] === 'function') {
srcNode = srcNode[key](input?.values);
srcNode = srcNode[key](inputValues);
} else {
srcNode = srcNode[key];
}
Expand Down
3 changes: 1 addition & 2 deletions packages/gqty/test/selection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ describe('selection creation', () => {
expect(selectionA.alias).toBe(undefined);
expect(selectionA.root.key).toBe('mutation');

expect(selectionA.input?.values).toBe(undefined);
expect(selectionA.input?.types).toBe(undefined);
expect(selectionA.input).toBe(undefined);
expect(selectionA.ancestry).toEqual([mutationRoot, selectionA]);

expect(selectionA.cacheKeys).toEqual(['mutation', 'a']);
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,14 @@ export function createLogger(
console.groupCollapsed(...format(['Selections', headerStyles]));
for (const [
,
{ key, cacheKeys, alias, input, isUnion },
{ key, cacheKeys, alias, inputValues, isUnion },
] of uniqueSelections) {
console.log(
stringifyJSONIfEnabled({
key,
cacheKeys: cacheKeys.join('.'),
alias,
input,
input: inputValues,
isUnion,
})
);
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/query/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ export const createUseQuery = <TSchema extends BaseGeneratedSchema>(
fetchInBackground,
operationName,
selections,
state,
]
);

Expand Down
22 changes: 14 additions & 8 deletions packages/react/test/useQuery.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { renderHook, waitFor } from '@testing-library/react';
import { Cache, type QueryPayload } from 'gqty';
import { act } from 'react';
Expand All @@ -17,7 +18,7 @@ describe('useQuery', () => {

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

query.time;
Reflect.get(query, 'time');

return query.$state.isLoading;
});
Expand Down Expand Up @@ -146,16 +147,16 @@ describe('useQuery', () => {
[
{
"operationName": undefined,
"query": "query($a00425:String){a2c936:human(name:$a00425){__typename id name}}",
"query": "query($a69281:String){a7b317:human(name:$a69281){__typename id name}}",
"variables": {
"a00425": "1",
"a69281": "1",
},
},
{
"operationName": undefined,
"query": "query($dd0895:String){a657eb:human(name:$dd0895){__typename id name}}",
"query": "query($a7c6a0:String){dc91d3:human(name:$a7c6a0){__typename id name}}",
"variables": {
"dd0895": "2",
"a7c6a0": "2",
},
},
]
Expand Down Expand Up @@ -281,6 +282,7 @@ describe('useQuery', () => {
const { result } = renderHook(() => {
const query = useQuery({
initialLoadingState: true,
suspense: true,
});

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

expect(result.current.$state.error).toBeUndefined();

expect(queries).toMatchInlineSnapshot(`
[
"query($a2a039:ID!){eb2884:pet(id:$a2a039){__typename id owner{__typename id name}}now peoples{__typename id name}}",
"query($a1a2bc:ID!){a1648a:pet(id:$a1a2bc){__typename id owner{__typename id name}}now peoples{__typename id name}}",
]
`);

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

expect(result.current.$state.error).toBeUndefined();

expect(queries).toMatchInlineSnapshot(`
[
"query($a2a039:ID!){eb2884:pet(id:$a2a039){__typename id owner{__typename id name}}now peoples{__typename id name}}",
"query($a2a039:ID!){eb2884:pet(id:$a2a039){__typename id owner{__typename id name}}now peoples{__typename id name}}",
"query($a1a2bc:ID!){a1648a:pet(id:$a1a2bc){__typename id owner{__typename id name}}now peoples{__typename id name}}",
"query($a1a2bc:ID!){a1648a:pet(id:$a1a2bc){__typename id owner{__typename id name}}now peoples{__typename id name}}",
]
`);
});
Expand Down
12 changes: 6 additions & 6 deletions packages/solid/src/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,23 @@ describe('createQuery', () => {
[
{
"operationName": undefined,
"query": "query($e61a8e:ID!){a02d2c:people(id:$e61a8e){__typename id name}}",
"query": "query($a176f8:ID!){a66965:people(id:$a176f8){__typename id name}}",
"variables": {
"e61a8e": "1",
"a176f8": "1",
},
},
{
"operationName": undefined,
"query": "query($d6d931:ID!){e084c7:people(id:$d6d931){__typename id name}}",
"query": "query($a482a1:ID!){eebe6f:people(id:$a482a1){__typename id name}}",
"variables": {
"d6d931": "2",
"a482a1": "2",
},
},
{
"operationName": undefined,
"query": "query($a60dd8:ID!){d2f785:people(id:$a60dd8){__typename id name}}",
"query": "query($b8233c:ID!){c0c0aa:people(id:$b8233c){__typename id name}}",
"variables": {
"a60dd8": "3",
"b8233c": "3",
},
},
]
Expand Down

0 comments on commit fde673b

Please sign in to comment.