Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Mar 10, 2023
1 parent 5c1471a commit 1eb6377
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 175 deletions.
23 changes: 8 additions & 15 deletions integration/graphql-17/src/gqty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,25 @@
import type { QueryFetcher } from 'gqty';
import { createClient } from 'gqty';
import { TestClient } from './api';
import type {
GeneratedSchema,
SchemaObjectTypes,
SchemaObjectTypesNames,
} from './schema.generated';
import type { GeneratedSchema } from './schema.generated';
import { generatedSchema, scalarsEnumsHash } from './schema.generated';

const queryFetcher: QueryFetcher = async function (query, variables) {
const queryFetcher: QueryFetcher = async function ({ query, variables }) {
return (await TestClient).query(query, {
variables,
});
};

export const client = createClient<
GeneratedSchema,
SchemaObjectTypesNames,
SchemaObjectTypes
>({
export const client = createClient<GeneratedSchema>({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher,
scalars: scalarsEnumsHash,
fetchOptions: {
fetcher: queryFetcher,
},
});

const { query, mutation, mutate, subscription, resolved, refetch, track } =
client;

export { query, mutation, mutate, subscription, resolved, refetch, track };

export * from './schema.generated';
export { query, mutation, mutate, subscription, resolved, refetch, track };
40 changes: 16 additions & 24 deletions packages/cli/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,18 +738,6 @@ export async function generate(
(v) => v[0]
);

typescriptTypes += `
export interface SchemaObjectTypes {
${objectTypesEntries.reduce((acum, [typeName]) => {
acum += `${typeName}:${typeName};`;
return acum;
}, '')}
}
export type SchemaObjectTypesNames = ${objectTypesEntries
.map(([key]) => `"${key}"`)
.join(' | ')};
`;

if (unionsAndInterfacesObjectTypesMap.size) {
typescriptTypes += `
${deps
Expand Down Expand Up @@ -798,8 +786,8 @@ export async function generate(
${
isJavascriptOutput
? typeDoc('import("gqty").QueryFetcher') + 'const queryFetcher'
: 'const queryFetcher : QueryFetcher'
} = async function (query, variables, fetchOptions) {
: 'const queryFetcher: QueryFetcher'
} = async function ({ query, variables, operationName }, fetchOptions) {
// Modify "${endpoint}" if needed
const response = await fetch("${endpoint}", {
method: "POST",
Expand All @@ -809,6 +797,7 @@ export async function generate(
body: JSON.stringify({
query,
variables,
operationName,
}),
mode: "cors",
...fetchOptions
Expand Down Expand Up @@ -977,15 +966,15 @@ export const generatedSchema = {${generatedSchemaCodeString}};
${react ? `import { createReactClient } from "@gqty/react"` : ''}
${
subscriptions
? `import { createSubscriptionsClient } from "@gqty/subscriptions"`
? `import { createClient as createSubscriptionsClient } from "graphql-ws"`
: ''
}
${isJavascriptOutput ? '' : 'import type { QueryFetcher } from "gqty";'}
import { createClient } from "gqty";
${
isJavascriptOutput
? ''
: 'import type { GeneratedSchema, SchemaObjectTypes, SchemaObjectTypesNames } from "./schema.generated";'
: 'import type { GeneratedSchema } from "./schema.generated";'
}
import { generatedSchema, scalarsEnumsHash } from "./schema.generated";
Expand Down Expand Up @@ -1015,19 +1004,22 @@ export const generatedSchema = {${generatedSchemaCodeString}};
'import("gqty").GQtyClient<import("./schema.generated").GeneratedSchema>'
)}export const client = createClient({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher
${subscriptions ? ', subscriptionsClient' : ''}
scalars: scalarsEnumsHash,
fetchOptions: {
fetcher: queryFetcher,
${subscriptions ? 'subscriber: subscriptionsClient' : ''}
},
});`
: `export const client = createClient<GeneratedSchema, SchemaObjectTypesNames, SchemaObjectTypes>({
: `export const client = createClient<GeneratedSchema>({
schema: generatedSchema,
scalarsEnumsHash,
queryFetcher
${subscriptions ? ', subscriptionsClient' : ''}
scalars: scalarsEnumsHash,
fetchOptions:{
fetcher: queryFetcher,
${subscriptions ? 'subscriber: subscriptionsClient' : ''}
},
});`
}
const { query, mutation, mutate, subscription, resolved, refetch, track } = client;
export { query, mutation, mutate, subscription, resolved, refetch, track };
Expand Down
28 changes: 0 additions & 28 deletions packages/gqty/src/Accessor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,6 @@ export const assignSelections = <TData extends GeneratedSchemaObject>(
}
};

/* TODO: Selection - null
*
* Cache accessor and selections such that subsequent selections are
* retained when null types are returned from the cache, where new selections
* are prevented from happening.
*
* Make sure such cache is cleared when new selections can be made again.
*
* Triggering onSelect() for all scalar selections inside would suffice, no
* need to cache the whole selection tree.
*
* Cache by value, nullObjectKey? Every single fetch should cache selections
* from last time, cached selections are only used as long as we got nulls.
*
* Caching accessors may prevent accessors from showing new values, so we only
* cache selections by null values and empty arrays.
*/

/* TODO: Selection - Conditional Rendering
*
* Handles conditional rendering that triggers query update on itself
* which results in infinite circular renderings.
*
* When a cache is still fresh, subsequent fetches should merge with objects
* instead of replacing them. Except on refetches, i.e. no-cache and no-store,
* which should instead invalidate reachable cache roots during selection.
*/

/* TODO: Selection - use()
*
* Replace `assignSelection` with `Selection.use(Selection)`,
Expand Down
4 changes: 2 additions & 2 deletions packages/gqty/src/Client/compat/inlineResolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const createLegacyInlineResolved = <
TSchema extends BaseGeneratedSchema = BaseGeneratedSchema
>({
resolvers: { createResolver },
subscribeLegacySelections: subscribeSelections,
subscribeLegacySelections,
}: CreateLegacyMethodOptions<TSchema>): LegacyInlineResolved => {
return (
fn,
Expand All @@ -46,7 +46,7 @@ export const createLegacyInlineResolved = <
fetchPolicy: refetch ? 'no-cache' : 'default',
operationName,
});
const unsubscribe = subscribeSelections((selection, cache) => {
const unsubscribe = subscribeLegacySelections((selection, cache) => {
context.onSelect?.(selection, cache);
onSelection?.(convertSelection(selection));
});
Expand Down
16 changes: 9 additions & 7 deletions packages/gqty/src/Client/compat/prefetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ import type { BaseGeneratedSchema } from '../..';
import type { CreateLegacyMethodOptions } from './client';

export type LegacyPrefetch<TSchema extends BaseGeneratedSchema> = {
<TData>(fn: (query: TSchema['query']) => TData): Promise<TData>;
<TData>(fn: (query: TSchema['query']) => TData): TData | Promise<TData>;
};

export const createLegacyPrefetch =
<TSchema extends BaseGeneratedSchema = BaseGeneratedSchema>({
resolvers: { createResolver },
subscribeLegacySelections,
}: CreateLegacyMethodOptions<TSchema>): LegacyPrefetch<TSchema> =>
async (fn, { operationName }: { operationName?: string } = {}) => {
const { accessor, context, resolve } = createResolver({ operationName });
(fn, { operationName }: { operationName?: string } = {}) => {
const {
accessor: { query },
context,
resolve,
} = createResolver({ operationName });
const unsubscribe = subscribeLegacySelections((selection, cache) => {
context.onSelect?.(selection, cache);
});
const data = fn(accessor.query);
const data = fn(query);

unsubscribe();

if (!context.shouldFetch) {
return data;
}

await resolve();

return fn(accessor.query);
return resolve().then(() => fn(query));
};
4 changes: 2 additions & 2 deletions packages/gqty/src/Client/compat/resolved.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const createLegacyResolved = <
debugger: debug,
fetchOptions: { fetcher, subscriber, retryPolicy },
resolvers: { createResolver },
subscribeLegacySelections: subscribeSelections,
subscribeLegacySelections,
}: CreateLegacyMethodOptions<TSchema>): LegacyResolved => {
const resolved: LegacyResolved = async <TData = unknown>(
fn: () => TData,
Expand All @@ -134,7 +134,7 @@ export const createLegacyResolved = <
fetchPolicy: noCache ? 'no-store' : refetch ? 'no-cache' : 'default',
operationName,
});
const unsubscribe = subscribeSelections((selection, cache) => {
const unsubscribe = subscribeLegacySelections((selection, cache) => {
context.onSelect?.(selection, cache);
onSelection?.(convertSelection(selection));
});
Expand Down
76 changes: 33 additions & 43 deletions packages/gqty/src/Client/resolveSelections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,8 @@ export const fetchSelections = <
operationName,
extensions: { type, hash } = {},
}) => {
if (!type) {
throw new GQtyError(`Invalid query type: ${type}`);
}

if (!hash) {
throw new GQtyError(`Expected query hash.`);
}
if (!type) throw new GQtyError(`Invalid query type: ${type}`);
if (!hash) throw new GQtyError(`Expected query hash.`);

const queryPayload: QueryPayload<QueryExtensions> = {
query,
Expand All @@ -68,26 +63,23 @@ export const fetchSelections = <
doFetch<TData>(queryPayload, { ...fetchOptions, selections })
);

const error = errors?.length
? GQtyError.fromGraphQLErrors(errors)
: undefined;
const result: FetchResult<TData> = {
data,
extensions: { ...extensions, ...queryPayload.extensions },
};

if (errors?.length) {
result.error = GQtyError.fromGraphQLErrors(errors);
}

debug?.dispatch({
cache,
request: { query, variables, operationName },
result: {
data,
error,
extensions: { ...extensions, ...queryPayload.extensions },
},
result,
selections,
});

return {
data,
error,
extensions: { ...extensions, ...queryPayload.extensions },
};
return result;
}
)
);
Expand Down Expand Up @@ -175,27 +167,24 @@ export const subscribeSelections = <
const next = ({ data, errors, extensions }: ExecutionResult<TData>) => {
if (data === undefined) return;

const error = errors?.length
? GQtyError.fromGraphQLErrors(errors)
: undefined;
const result: FetchResult<TData> = {
data,
extensions: { ...extensions, ...queryPayload.extensions },
};

if (errors?.length) {
result.error = GQtyError.fromGraphQLErrors(errors);
}

debug?.dispatch({
cache,
label: subscriptionId ? `[id=${subscriptionId}] [data]` : undefined,
request: queryPayload,
result: {
data,
error,
extensions: { ...extensions, ...queryPayload.extensions },
},
result,
selections,
});

fn({
data,
error,
extensions: { ...extensions, ...queryPayload.extensions },
});
fn(result);
};

const error = (error: GQtyError) => {
Expand Down Expand Up @@ -313,19 +302,20 @@ const doFetch = async <
return new Promise((resolve, reject) => {
// Selections are attached solely for useMetaState()
doRetry(retryPolicy!, {
onLastTry: () => {
const promise = doDoFetch().then(resolve, reject);

notifyRetry(promise, selections);

return promise;
onLastTry: async () => {
try {
const promise = doDoFetch();
notifyRetry(promise, selections);
resolve(await promise);
} catch (e) {
reject(e);
}
},
onRetry: () => {
const promise = doDoFetch().then(resolve);

onRetry: async () => {
const promise = doDoFetch();
notifyRetry(promise, selections);

return promise;
resolve(await promise);
},
});
});
Expand Down
9 changes: 6 additions & 3 deletions packages/gqty/src/Client/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,12 @@ export const createResolvers = <TSchema extends BaseGeneratedSchema>({
depthLimit,
fetchPolicy,
onSelect(selection, cache) {
selections.add(selection);
onSelect?.(selection, cache);
parentContext?.onSelect(selection, cache);
// Prevents infinite loop created by legacy functions
if (!selections.has(selection)) {
selections.add(selection);
onSelect?.(selection, cache);
parentContext?.onSelect(selection, cache);
}
},
scalars,
schema,
Expand Down
Loading

0 comments on commit 1eb6377

Please sign in to comment.