Skip to content

Commit

Permalink
found the culprit: maybe
Browse files Browse the repository at this point in the history
TODO: return to old age of gql!
  • Loading branch information
wtfsayo committed Apr 2, 2024
1 parent 941ddc4 commit 75f97fb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 129 deletions.
22 changes: 5 additions & 17 deletions packages/graphql/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import {
ApolloClient,
InMemoryCache,
NormalizedCacheObject,
} from '@apollo/client';
import { SUPPORTED_NETWORKS } from '@smart-invoice/constants';
import { getGraphUrl } from '@smart-invoice/shared';
import { GraphQLClient } from 'graphql-request';

const cache = new InMemoryCache();

export const clients = SUPPORTED_NETWORKS.reduce(
(o, chainId) => ({
...o,
[chainId]: new ApolloClient({
uri: getGraphUrl(chainId),
cache,
}),
}),
{} as Record<number, ApolloClient<NormalizedCacheObject>>,
);
export const clients = SUPPORTED_NETWORKS.map((network: number) => ({
network,
client: new GraphQLClient(getGraphUrl(network)),
}));
16 changes: 6 additions & 10 deletions packages/graphql/src/invoice.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase */
import { logDebug } from '@smart-invoice/shared';
import { Address, Hex, isAddress } from 'viem';

import _ from 'lodash';
import { clients } from './client';
import { scalars } from './scalars';
import {
Expand Down Expand Up @@ -140,18 +140,14 @@ export const fetchInvoice = async (chainId: number, queryAddress: Address) => {
if (!address) return null;

const query = invoiceQuery(address);
const { data, error } = await clients[chainId].query({ query });

logDebug({ data, error, address });
const { client } = _.find(clients, { network: chainId })!;

const result = await client.request(query);

if (!data) {
if (error) {
throw error;
}
return null;
}
logDebug({ result, address });

return data.invoice;
return result.invoice;
};

export type TokenMetadata = {
Expand Down
18 changes: 7 additions & 11 deletions packages/graphql/src/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { logDebug } from '@smart-invoice/shared';
import { Address, isAddress } from 'viem';

import _ from 'lodash';

import { clients } from './client';
import { scalars } from './scalars';
import { _SubgraphErrorPolicy_, Invoice_orderBy, OrderDirection } from './zeus';
Expand Down Expand Up @@ -80,11 +82,12 @@ export const fetchInvoices = async (
sortDirection,
where,
);
const { data, error } = await clients[chainId].query({ query });

const { client } = _.find(clients, { network: chainId })!;
const result = await client.request(query);

logDebug({
data,
error,
result,
chainId,
searchInput,
pageIndex,
Expand All @@ -93,14 +96,7 @@ export const fetchInvoices = async (
sortDesc,
});

if (!data) {
if (error) {
throw error;
}
return null;
}

return data.invoices;
return result.invoices;
};

// type GetElementType<T extends any[] | undefined | null> = T extends (infer U)[]
Expand Down
181 changes: 90 additions & 91 deletions packages/graphql/src/zeus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,71 +799,69 @@ type IsScalar<S, SCLR extends ScalarDefinition> = S extends 'scalar' & {
: unknown
: unknown
: S;
type IsArray<T, U, SCLR extends ScalarDefinition> = T extends Array<infer R>
? InputType<R, U, SCLR>[]
: InputType<T, U, SCLR>;
type IsArray<T, U, SCLR extends ScalarDefinition> =
T extends Array<infer R> ? InputType<R, U, SCLR>[] : InputType<T, U, SCLR>;
type FlattenArray<T> = T extends Array<infer R> ? R : T;
type BaseZeusResolver = boolean | 1 | string | Variable<any, string>;

type IsInterfaced<
SRC extends DeepAnify<DST>,
DST,
SCLR extends ScalarDefinition,
> = FlattenArray<SRC> extends ZEUS_INTERFACES | ZEUS_UNIONS
? {
[P in keyof SRC]: SRC[P] extends '__union' & infer R
? P extends keyof DST
? IsArray<
R,
'__typename' extends keyof DST
? DST[P] & { __typename: true }
: DST[P],
SCLR
>
: IsArray<
R,
'__typename' extends keyof DST
? { __typename: true }
: Record<string, never>,
SCLR
>
: never;
}[keyof SRC] & {
[P in keyof Omit<
Pick<
SRC,
{
[P in keyof DST]: SRC[P] extends '__union' & infer R ? never : P;
}[keyof DST]
>,
'__typename'
>]: IsPayLoad<DST[P]> extends BaseZeusResolver
? IsScalar<SRC[P], SCLR>
: IsArray<SRC[P], DST[P], SCLR>;
}
: {
[P in keyof Pick<SRC, keyof DST>]: IsPayLoad<
DST[P]
> extends BaseZeusResolver
? IsScalar<SRC[P], SCLR>
: IsArray<SRC[P], DST[P], SCLR>;
};
> =
FlattenArray<SRC> extends ZEUS_INTERFACES | ZEUS_UNIONS
? {
[P in keyof SRC]: SRC[P] extends '__union' & infer R
? P extends keyof DST
? IsArray<
R,
'__typename' extends keyof DST
? DST[P] & { __typename: true }
: DST[P],
SCLR
>
: IsArray<
R,
'__typename' extends keyof DST
? { __typename: true }
: Record<string, never>,
SCLR
>
: never;
}[keyof SRC] & {
[P in keyof Omit<
Pick<
SRC,
{
[P in keyof DST]: SRC[P] extends '__union' & infer R ? never : P;
}[keyof DST]
>,
'__typename'
>]: IsPayLoad<DST[P]> extends BaseZeusResolver
? IsScalar<SRC[P], SCLR>
: IsArray<SRC[P], DST[P], SCLR>;
}
: {
[P in keyof Pick<SRC, keyof DST>]: IsPayLoad<
DST[P]
> extends BaseZeusResolver
? IsScalar<SRC[P], SCLR>
: IsArray<SRC[P], DST[P], SCLR>;
};

export type MapType<
SRC,
DST,
SCLR extends ScalarDefinition,
> = SRC extends DeepAnify<DST> ? IsInterfaced<SRC, DST, SCLR> : never;
export type MapType<SRC, DST, SCLR extends ScalarDefinition> =
SRC extends DeepAnify<DST> ? IsInterfaced<SRC, DST, SCLR> : never;
// eslint-disable-next-line @typescript-eslint/ban-types
export type InputType<
SRC,
DST,
SCLR extends ScalarDefinition = {},
> = IsPayLoad<DST> extends { __alias: infer R }
? {
[P in keyof R]: MapType<SRC, R[P], SCLR>[keyof MapType<SRC, R[P], SCLR>];
} & MapType<SRC, Omit<IsPayLoad<DST>, '__alias'>, SCLR>
: MapType<SRC, IsPayLoad<DST>, SCLR>;
export type InputType<SRC, DST, SCLR extends ScalarDefinition = {}> =
IsPayLoad<DST> extends { __alias: infer R }
? {
[P in keyof R]: MapType<SRC, R[P], SCLR>[keyof MapType<
SRC,
R[P],
SCLR
>];
} & MapType<SRC, Omit<IsPayLoad<DST>, '__alias'>, SCLR>
: MapType<SRC, IsPayLoad<DST>, SCLR>;
export type SubscriptionToGraphQL<Z, T, SCLR extends ScalarDefinition> = {
ws: WebSocket;
on: (fn: (args: InputType<T, Z, SCLR>) => void) => void;
Expand Down Expand Up @@ -914,17 +912,18 @@ type VR<T extends string> = VariableRequired<VariableRequired<T>>;

export type GraphQLVariableType = VR<AllVariableTypes>;

type ExtractVariableTypeString<T extends string> = T extends VR<infer R1>
? R1 extends VR<infer R2>
? R2 extends VR<infer R3>
? R3 extends VR<infer R4>
? R4 extends VR<infer R5>
? R5
: R4
: R3
: R2
: R1
: T;
type ExtractVariableTypeString<T extends string> =
T extends VR<infer R1>
? R1 extends VR<infer R2>
? R2 extends VR<infer R3>
? R3 extends VR<infer R4>
? R4 extends VR<infer R5>
? R5
: R4
: R3
: R2
: R1
: T;

type DecomposeType<T, Type> = T extends `[${infer R}]`
? Array<DecomposeType<R, Type>> | undefined
Expand Down Expand Up @@ -963,40 +962,40 @@ export type Variable<T extends GraphQLVariableType, Name extends string> = {
' __zeus_type': T;
};

export type ExtractVariablesDeep<Query> = Query extends Variable<
infer VType,
infer VName
>
? { [key in VName]: GetVariableType<VType> }
: Query extends string | number | boolean | Array<string | number | boolean>
? // eslint-disable-next-line @typescript-eslint/ban-types
{}
: UnionToIntersection<
{
[K in keyof Query]: WithOptionalNullables<
ExtractVariablesDeep<Query[K]>
>;
}[keyof Query]
>;

export type ExtractVariables<Query> = Query extends Variable<
infer VType,
infer VName
>
? { [key in VName]: GetVariableType<VType> }
: Query extends [infer Inputs, infer Outputs]
? ExtractVariablesDeep<Inputs> & ExtractVariables<Outputs>
export type ExtractVariablesDeep<Query> =
Query extends Variable<infer VType, infer VName>
? { [key in VName]: GetVariableType<VType> }
: Query extends string | number | boolean | Array<string | number | boolean>
? // eslint-disable-next-line @typescript-eslint/ban-types
{}
: UnionToIntersection<
{
[K in keyof Query]: WithOptionalNullables<
ExtractVariables<Query[K]>
ExtractVariablesDeep<Query[K]>
>;
}[keyof Query]
>;

export type ExtractVariables<Query> =
Query extends Variable<infer VType, infer VName>
? { [key in VName]: GetVariableType<VType> }
: Query extends [infer Inputs, infer Outputs]
? ExtractVariablesDeep<Inputs> & ExtractVariables<Outputs>
: Query extends
| string
| number
| boolean
| Array<string | number | boolean>
? // eslint-disable-next-line @typescript-eslint/ban-types
{}
: UnionToIntersection<
{
[K in keyof Query]: WithOptionalNullables<
ExtractVariables<Query[K]>
>;
}[keyof Query]
>;

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I,
) => void
Expand Down

0 comments on commit 75f97fb

Please sign in to comment.