Example of overriding getOne()
Is there an en example of how to Override the GetOne call to a graphql backend. I need to send additional parameters beside the ID.
I have a simple custom graphql backend. I am not using the autogenerated ones.
5 Replies
Hi @davidd4309. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Yes, you can override the
getOne
call and pass additional parameters using the meta
field. Here's an example of how to do this:
1. Use the useOne
hook and pass the meta
object with your additional parameters:
2. Get the meta
parameter from the data provider and modify the getOne
method accordingly:
In this example, replace YourResource
with the appropriate resource name in your GraphQL schema, and include the fields you want to fetch.
For more information on using meta
, refer to the Refine documentation (https://refine.dev/docs/api-reference/general-concepts/#meta).
----------------------
Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:
----------------------General Concepts | refine
- refine core is fully independent of UI. So you can use core components and hooks without any UI dependency.
<@542609011220938753> kapa.ai is still learning and improving, please let us how it did by reacting below
sharp-indigo•2y ago
also you can check this
https://refine.dev/docs/api-reference/core/providers/data-provider/#how-can-i-customize-existing-data-providers
Data Provider | refine
The data provider acts as a data layer for your app that makes the HTTP requests and encapsulates how the data is retrieved. refine consumes these methods via data hooks.
evident-indigoOP•2y ago
ok this worked. But if I don't have that default line i get an error. Any idea why or what should be in that line?
TS2322: Type '{ getOne: (resource: any, args: any, options: any) => Promise<any>; getList: <TData extends BaseRecord = BaseRecord>(params: { resource: string; pagination?: Pagination | undefined; ... 6 more ...; dataProviderName?: string | undefined; }) => Promise<...>; ... 8 more ...; custom: <TData extends BaseRecord = BaseReco...' is not assignable to type 'IDataContextProvider | IDataMultipleContextProvider'.
Property 'default' is missing in type '{ getOne: (resource: any, args: any, options: any) => Promise<any>; getList: <TData extends BaseRecord = BaseRecord>(params: { resource: string; pagination?: Pagination | undefined; ... 6 more ...; dataProviderName?: string | undefined; }) => Promise<...>; ... 8 more ...; custom: <TData extends BaseRecord = BaseReco...' but required in type 'IDataMultipleContextProvider'.
const client = new GraphQLClient(API_URL);
const gqlDataProvider = dataProvider(client);
const myDataProvider = {
...gqlDataProvider,
getOne: async (resource: any, args: any, options: any) => {
const query =
query GetOne($id: ID!, $transLang: String!) {
data: sentence(id: $id, transLang: $transLang) {
${resource.meta.fields}
}
}
;
const result = await axios.post(
API_URL,
{query, variables: {id: resource.id, ...resource.meta.variables}},
{headers: axiosHeaders},
);
return result.data.data;
},
default: gqlDataProvider, //. <<---- why I need this.
};