Skip to content

Commit

Permalink
chore: codegen query template
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 committed Jan 24, 2025
1 parent df6d2ee commit d960dee
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 118 deletions.
31 changes: 2 additions & 29 deletions apps/lana-chat/app/api/chat/tools/get-credit-facilities.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { z } from "zod";
import { tool } from "ai";
import { gql } from "@apollo/client";
import { getClient } from "../client";
import {
CreditFacilitiesDocument,
CreditFacilitiesQuery,
CreditFacilitiesQueryVariables,
CreditFacilitiesFilterBy,
CreditFacilitiesSortBy,
CollateralizationState,
CreditFacilityStatus,
SortDirection,
creditFacilities,
} from "@/lib/graphql/generated";

const CollateralizationStateSchema = z
Expand Down Expand Up @@ -106,31 +104,6 @@ gql`
}
`;

export const getCreditFacilities = async (
variables: CreditFacilitiesQueryVariables
) => {
try {
const response = await getClient().query<
CreditFacilitiesQuery,
CreditFacilitiesQueryVariables
>({
query: CreditFacilitiesDocument,
variables,
});

if (!response.data) {
return { error: "Data not found" };
}

return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
};

export const getCreditFacilitiesTool = tool({
type: "function",
description:
Expand All @@ -149,7 +122,7 @@ export const getCreditFacilitiesTool = tool({
filter: CreditFacilitiesFilterSchema.optional(),
}),
execute: async ({ first, after, sort, filter }) => {
return getCreditFacilities({
return creditFacilities({
first,
after,
sort,
Expand Down
31 changes: 2 additions & 29 deletions apps/lana-chat/app/api/chat/tools/get-credit-facility-details.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { z } from "zod";
import { tool } from "ai";
import { gql } from "@apollo/client";
import { getClient } from "../client";
import {
GetCreditFacilityDetailsDocument,
GetCreditFacilityDetailsQuery,
getCreditFacilityDetails,
GetCreditFacilityDetailsQueryVariables,
} from "@/lib/graphql/generated";

Expand Down Expand Up @@ -122,31 +120,6 @@ gql`
}
`;

export const getCreditFacilityById = async (
variables: GetCreditFacilityDetailsQueryVariables
) => {
try {
const response = await getClient().query<
GetCreditFacilityDetailsQuery,
GetCreditFacilityDetailsQueryVariables
>({
query: GetCreditFacilityDetailsDocument,
variables,
});

if (!response.data) {
return { error: "Data not found" };
}

return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
};

export const getCreditFacilityDetailsTool = tool({
type: "function",
description: `Retrieve comprehensive details for a single credit facility. USE ONLY: when complete facility details are asked.`,
Expand All @@ -159,6 +132,6 @@ export const getCreditFacilityDetailsTool = tool({
),
}),
execute: async ({ id }) => {
return getCreditFacilityById({ id });
return getCreditFacilityDetails({ id });
},
});
31 changes: 2 additions & 29 deletions apps/lana-chat/app/api/chat/tools/get-customer-credit-facility.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { z } from "zod";
import { tool } from "ai";
import { gql } from "@apollo/client";
import { getClient } from "../client";
import {
GetCustomerCreditFacilityByEmailDocument,
GetCustomerCreditFacilityByEmailQuery,
getCustomerCreditFacilityByEmail,
GetCustomerCreditFacilityByEmailQueryVariables,
} from "@/lib/graphql/generated";

Expand Down Expand Up @@ -123,31 +121,6 @@ gql`
}
`;

export const getCustomerCreditFacility = async (
variables: GetCustomerCreditFacilityByEmailQueryVariables
) => {
try {
const response = await getClient().query<
GetCustomerCreditFacilityByEmailQuery,
GetCustomerCreditFacilityByEmailQueryVariables
>({
query: GetCustomerCreditFacilityByEmailDocument,
variables,
});

if (!response.data?.customerByEmail) {
return { error: "Customer not found" };
}

return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
};

export const getCustomerCreditFacilitiesTool = tool({
type: "function",
description:
Expand All @@ -160,6 +133,6 @@ export const getCustomerCreditFacilitiesTool = tool({
),
}),
execute: async ({ email }) => {
return getCustomerCreditFacility({ email });
return getCustomerCreditFacilityByEmail({ email });
},
});
31 changes: 2 additions & 29 deletions apps/lana-chat/app/api/chat/tools/get-customer-details.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { z } from "zod";
import { tool } from "ai";
import { gql } from "@apollo/client";
import { getClient } from "../client";
import {
GetCustomerByEmailDocument,
GetCustomerByEmailQuery,
GetCustomerByEmailQueryVariables,
getCustomerByEmail,
} from "@/lib/graphql/generated";

gql`
Expand Down Expand Up @@ -47,31 +45,6 @@ gql`
}
`;

export const getCustomerDetails = async (
variables: GetCustomerByEmailQueryVariables
) => {
try {
const response = await getClient().query<
GetCustomerByEmailQuery,
GetCustomerByEmailQueryVariables
>({
query: GetCustomerByEmailDocument,
variables,
});

if (!response.data?.customerByEmail) {
return { error: "Customer not found" };
}

return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
};

export const getCustomerDetailsTool = tool({
type: "function",
description:
Expand All @@ -84,6 +57,6 @@ export const getCustomerDetailsTool = tool({
),
}),
execute: async ({ email }) => {
return getCustomerDetails({ email });
return getCustomerByEmail({ email });
},
});
2 changes: 1 addition & 1 deletion apps/lana-chat/codegen.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
overwrite: true
schema:
- "../../lana/admin-server/src/graphql/schema.graphql"
- "./lib/graphql/local-schema.gql"
documents:
- "app/**/*.{ts,tsx}"
- "lib/**/*.{ts,tsx}"
Expand All @@ -15,6 +14,7 @@ generates:
- typescript
- typescript-operations
- typescript-react-apollo
- "./lib/graphql/generated/codegen-query-template.js"
- add:
content: "// this file is autogenerated by codegen"
- add:
Expand Down
File renamed without changes.
42 changes: 42 additions & 0 deletions apps/lana-chat/lib/graphql/generated/codegen-query-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module.exports = {
plugin: (_, documents) => {
const imports = `import { getClient } from "../../client";`;

const functions = documents
.filter((doc) => doc.document?.definitions?.length > 0)
.map((doc) => {
const operation = doc.document.definitions.find(
(def) => def.kind === "OperationDefinition" && def.name
);

if (operation?.name?.value) {
const name = operation.name.value;
const camelCaseName = name.charAt(0).toLowerCase() + name.slice(1);
return functionTemplate(camelCaseName, name);
}
return "";
})
.filter(Boolean)
.join("\n");

return [imports, functions].join("\n\n");
},
};

const functionTemplate = (functionName, operationName) => {
return `
export async function ${functionName}(variables: ${operationName}QueryVariables) {
try {
const response = await getClient().query<${operationName}Query, ${operationName}QueryVariables>({
query: ${operationName}Document,
variables,
});
return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
}`;
};
64 changes: 63 additions & 1 deletion apps/lana-chat/lib/graphql/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2038,4 +2038,66 @@ export function useGetCustomerByEmailLazyQuery(baseOptions?: Apollo.LazyQueryHoo
}
export type GetCustomerByEmailQueryHookResult = ReturnType<typeof useGetCustomerByEmailQuery>;
export type GetCustomerByEmailLazyQueryHookResult = ReturnType<typeof useGetCustomerByEmailLazyQuery>;
export type GetCustomerByEmailQueryResult = Apollo.QueryResult<GetCustomerByEmailQuery, GetCustomerByEmailQueryVariables>;
export type GetCustomerByEmailQueryResult = Apollo.QueryResult<GetCustomerByEmailQuery, GetCustomerByEmailQueryVariables>;
import { getClient } from "../../client";


export async function creditFacilities(variables: CreditFacilitiesQueryVariables) {
try {
const response = await getClient().query<CreditFacilitiesQuery, CreditFacilitiesQueryVariables>({
query: CreditFacilitiesDocument,
variables,
});
return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
}

export async function getCreditFacilityDetails(variables: GetCreditFacilityDetailsQueryVariables) {
try {
const response = await getClient().query<GetCreditFacilityDetailsQuery, GetCreditFacilityDetailsQueryVariables>({
query: GetCreditFacilityDetailsDocument,
variables,
});
return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
}

export async function getCustomerCreditFacilityByEmail(variables: GetCustomerCreditFacilityByEmailQueryVariables) {
try {
const response = await getClient().query<GetCustomerCreditFacilityByEmailQuery, GetCustomerCreditFacilityByEmailQueryVariables>({
query: GetCustomerCreditFacilityByEmailDocument,
variables,
});
return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
}

export async function getCustomerByEmail(variables: GetCustomerByEmailQueryVariables) {
try {
const response = await getClient().query<GetCustomerByEmailQuery, GetCustomerByEmailQueryVariables>({
query: GetCustomerByEmailDocument,
variables,
});
return response;
} catch (error) {
if (error instanceof Error) {
return { error: error.message };
}
return { error: "An unknown error occurred" };
}
}

0 comments on commit d960dee

Please sign in to comment.