Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Provide types and hook to set the data of a specific operation safely #235

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions plugins/typescript/src/generators/generateReactQueryFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const generateReactQueryFunctions = async (
const contextTypeName = `${c.pascal(filenamePrefix)}Context`;
const nodes: ts.Node[] = [];
const keyManagerItems: ts.TypeLiteralNode[] = [];
const queryOperationDataTypeItems: ts.TypeElement[] = [];

const fetcherFilename = formatFilename(filenamePrefix + "-fetcher");
const contextFilename = formatFilename(filenamePrefix + "-context");
Expand Down Expand Up @@ -143,8 +144,6 @@ export const generateReactQueryFunctions = async (
),
});



const operationFetcherFnName = `fetch${c.pascal(operationId)}`;
const operationQueryFnName = `${c.pascal(operationId)}Query`;
const component: "useQuery" | "useMutate" =
Expand All @@ -157,7 +156,6 @@ export const generateReactQueryFunctions = async (
}

if (component === "useQuery") {

nodes.push(...declarationNodes);

keyManagerItems.push(
Expand Down Expand Up @@ -185,6 +183,15 @@ export const generateReactQueryFunctions = async (
])
);

queryOperationDataTypeItems.push(
f.createPropertySignature(
undefined,
f.createIdentifier(operationId),
undefined,
dataType
)
);

nodes.push(
...createOperationFetcherFnNodes({
dataType,
Expand Down Expand Up @@ -252,6 +259,13 @@ export const generateReactQueryFunctions = async (
])
);

const queryOperationDataTypes = f.createTypeAliasDeclaration(
[f.createModifier(ts.SyntaxKind.ExportKeyword)],
"QueryDataTypes",
undefined,
f.createTypeLiteralNode(queryOperationDataTypeItems)
);

const { nodes: usedImportsNodes, keys: usedImportsKeys } = getUsedImports(
nodes,
{
Expand All @@ -278,6 +292,7 @@ export const generateReactQueryFunctions = async (
...usedImportsNodes,
...nodes,
queryKeyManager,
queryOperationDataTypes,
])
);
};
Expand Down
19 changes: 19 additions & 0 deletions plugins/typescript/src/templates/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,23 @@ export const getContext = (prefix: string, componentsFile: string) =>
} => {
return Boolean((operation.variables as any).queryParams);
};

/**
* Set the data of a query operation
*
* @example
* const setQueryOperationData = useSetQueryOperationData();
* setQueryOperationData({
* operationId: "getUser",
* path: "/users/{id}",
* variables: { pathParams: { id: "1" } }
* }, data /* data must have the type expected by that operation * /);
*/
export function useSetQueryOperationData() {
const { queryKeyFn } = useAcApiContext();
const queryClient = useQueryClient();
return <Op extends QueryOperation>(operation: Op, data: QueryDataTypes[Op["operationId"]]) => {
queryClient.setQueryData(queryKeyFn(operation), data);
};
}
`;