Skip to content

Instantly share code, notes, and snippets.

@tgmarinho
Last active March 10, 2023 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgmarinho/764adb4848421ef55897b27c457511ea to your computer and use it in GitHub Desktop.
Save tgmarinho/764adb4848421ef55897b27c457511ea to your computer and use it in GitHub Desktop.
import { gql } from "graphql-request";
// import { returningValues as genericValues } from "./payload";
import { returningValues as genericValues } from "./payload";
export interface IPage {
/** How many records should be returned on the page */
limit: number;
/** Which page should be displayed (starts with 1) */
page: number;
}
export interface IQuery {
escrow_id?: number;
seller?: string;
buyer?: string;
marketplace?: string;
claimed?: boolean;
dateStart?: Date;
dateEnd?: Date;
arbitrator?: string;
status_arbitration?: string;
blockNumberNotNull?: Boolean;
sort?: string;
}
interface IBuildQuery {
query: IQuery;
returningValues?: string[];
pagination?: IPage;
}
const buildQuery = ({
query,
returningValues = genericValues,
pagination,
}: IBuildQuery) => {
let whereSentence: string | null = null;
let aggregate = "";
const conditions: string[] = [];
const sentences: string[] = [];
if (query.escrow_id) {
conditions.push(`{ escrow_id: { _eq: "${query.escrow_id}"} }`);
}
if (query.seller && query.buyer) {
conditions.push(
`{_or: [{ seller: { _ilike: "${query.seller}"}}, { buyer: { _ilike: "${query.buyer}"}} ]}`,
);
} else if (query.buyer) {
conditions.push(`{ buyer: { _ilike: "${query.buyer}"} }`);
} else if (query.seller) {
conditions.push(`{ seller: { _ilike: "${query.seller}"} }`);
}
if (query.marketplace) {
conditions.push(`{ marketplace: {_ilike: "${query.marketplace}"} }`);
}
if (query.claimed !== undefined) {
conditions.push(`{ claimed: {_eq: ${query.claimed}} }`);
}
if (query.dateStart) {
conditions.push(`{ paid_at: { _gte: "${query.dateStart.toISOString()}"} }`);
}
if (query.dateEnd) {
conditions.push(`{ paid_at: { _lte: "${query.dateEnd.toISOString()}"} }`);
}
if (query.arbitrator) {
conditions.push(`{ arbitrator: { _ilike: "${query.arbitrator}"} }`);
}
if (query.status_arbitration) {
conditions.push(
`{ status_arbitration: { _eq: "${query.status_arbitration}"} }`,
);
}
if (query.blockNumberNotNull) {
conditions.push("{ block_number: { _is_null: false } }");
}
if (conditions.length > 0) {
whereSentence = `where: {_and: [ ${conditions.join("\n")} ]}`;
sentences.push(whereSentence);
}
if (pagination) {
sentences.push(`limit: ${pagination.limit}, offset: ${pagination.page}`);
const allOrWhere = whereSentence ? `(${whereSentence})` : " ";
aggregate = `
escrow_status_view_aggregate${allOrWhere}{
aggregate {
totalCount: count
}
}
`;
} else {
sentences.push("limit: 10, offset: 0");
}
// order by
if (query.sort) {
sentences.push(`order_by: { ${query.sort}: desc } `);
} else {
sentences.push("order_by: { paid_at: desc } ");
}
console.log({aggregate})
return gql`
query getPaymentsView {
${aggregate}
escrow_status_view(
${sentences.join(",\n")}
)
{
${returningValues.join("\n\t\t")}
}
}
`;
};
export { buildQuery };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment