-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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(graphql): add getData and getCount options #6357
base: releases/october
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@refinedev/graphql": minor | ||
--- | ||
|
||
feat: add default fetcher function. TBC Later. | ||
|
||
[Resolves #5943](https://github.com/refinedev/refine/issues/5943) | ||
[Resolves #5942](https://github.com/refinedev/refine/issues/5942) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import type { | ||
BaseRecord, | ||
CreateParams, | ||
DeleteOneParams, | ||
GetListParams, | ||
GetOneParams, | ||
UpdateParams, | ||
} from "@refinedev/core"; | ||
import camelCase from "camelcase"; | ||
import pluralize from "pluralize"; | ||
|
||
"asd".toUpperCase; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. leftover code here |
||
|
||
type GraphQLGetDataFunctionParams = { response: Record<string, any> } & ( | ||
| { method: "getList"; params: GetListParams } | ||
| { method: "create"; params: CreateParams } | ||
| { method: "update"; params: UpdateParams } | ||
| { method: "getOne"; params: GetOneParams } | ||
| { method: "deleteOne"; params: DeleteOneParams } | ||
); | ||
|
||
type GraphQLGetDataFunction = (params: GraphQLGetDataFunctionParams) => any; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return type can be inferred from type ResponseMap = {
getList: GetListResponse<any>;
getOne: GetOneResponse<any>;
/* ... */
};
type InferResponse<T extends GraphQLGetDataFunctionParams> = T extends { method: infer M }
? M extends keyof ResponseMap
? ResponseMap[M]
: never
: never;
type GraphQLGetDataFunction = (params: GraphQLGetDataFunctionParams) => InferResponse<typeof params>; |
||
|
||
type GraphQLGetCountFunctionParams = { | ||
response: Record<string, any>; | ||
params: GetListParams; | ||
}; | ||
|
||
type GraphQLGetCountFunction = ( | ||
params: GraphQLGetCountFunctionParams, | ||
) => number; | ||
|
||
export type GraphQLDataProviderOptions = { | ||
getData: GraphQLGetDataFunction; | ||
getCount: GraphQLGetCountFunction; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type is also used in the |
||
|
||
export const defaultGetDataFunc: GraphQLGetDataFunction = ({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
method, | ||
params, | ||
response, | ||
}) => { | ||
const singularResource = pluralize.singular(params.resource); | ||
|
||
switch (method) { | ||
case "create": { | ||
const camelCreateName = camelCase(`create-${singularResource}`); | ||
const operation = params.meta?.operation ?? camelCreateName; | ||
|
||
// console.log(response, operation, singularResource); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover here too |
||
|
||
return response[operation][singularResource]; | ||
} | ||
case "deleteOne": { | ||
const camelDeleteName = camelCase(`delete-${singularResource}`); | ||
|
||
const operation = params.meta?.operation ?? camelDeleteName; | ||
|
||
return response[operation][singularResource]; | ||
} | ||
case "getList": { | ||
const camelResource = camelCase(params.resource); | ||
const operation = params.meta?.operation ?? camelResource; | ||
|
||
return response[operation] ?? []; | ||
} | ||
case "getOne": { | ||
const camelResource = camelCase(singularResource); | ||
|
||
const operation = params.meta?.operation ?? camelResource; | ||
|
||
return response[operation]; | ||
} | ||
case "update": { | ||
const camelUpdateName = camelCase(`update-${singularResource}`); | ||
const operation = params.meta?.operation ?? camelUpdateName; | ||
|
||
return response[operation][singularResource]; | ||
} | ||
} | ||
}; | ||
|
||
export const defaultGetCountFunc: GraphQLGetCountFunction = ({ | ||
params, | ||
response, | ||
}): number => { | ||
const camelResource = camelCase(params.resource); | ||
|
||
const operation = params.meta?.operation ?? camelResource; | ||
|
||
return response[operation]?.totalCount ?? 0; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This marks the
options
as optional but requires both methods to be defined when passing it.getCount
andgetData
should have default values individually.