Setup Relationships Between Resources for Inferencer
So my app has an organization resource and a deployment resource; deployments belong to an organization, and an organization can have many deployments.
Now, when a user is creating a deployment, there needs to be a select dropdown where the user can select what organization this deployment belongs to.
How can I establish this kind of relationship between resources so the inferencer can properly generate the create/edit forms for the deployment to allow this relationship?
12 Replies
Hi @nick_montoya. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In refine, you can establish a relationship between resources using the
useSelect
hook. This hook fetches the data by passing the params to the dataProvider
's getList
method and then returns the options
to be used in the select component.
In your case, when creating a deployment, you can use the useSelect
hook to fetch the organizations and use them as options in the select dropdown. Here's an example of how you can do this:
This will fetch the organizations and provide them as options for the select dropdown. The user can then select an organization when creating a deployment.
The Inferencer in refine can automatically generate the create/edit forms for you. If it doesn't work as expected, you can manually modify the inferred fields using the fieldTransformer
function. More information about this can be found in the Modifying the inferred fields (https://refine.dev/docs/packages/documentation/inferencer#modifying-the-inferred-fields) section of the documentation.
For more information on how to use the useSelect
hook, you can refer to the useSelect
documentation (https://refine.dev/docs/api-reference/core/hooks/useSelect/).
Please note that the useSelect
hook returns 10 records by default. If the organization of the deployment may not be in the first 10 records, you can use the defaultValue
prop to set the default value of the useSelect
hook:
This will ensure that the correct organization is selected by default when editing a deployment.
Sources:
- Adding Create Page (https://refine.dev/docs/tutorial/adding-crud-pages/headless/add-create-page)
- Adding Edit Page (https://refine.dev/docs/tutorial/adding-crud-pages/headless/add-edit-page)
- Inferencer (https://refine.dev/docs/packages/documentation/inferencer)
----------------------
Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:
----------------------<@263527875506995202> kapa.ai is still learning and improving, please let us know how it did by reacting below
afraid-scarletOP•2y ago
@kapa.ai So can the inferencer use the resource interfaces I have defined?
Let me check the knowledge base for you. One moment! :books:
----------------------
The Inferencer in refine generates views and codes based on the data structure of the resource by fetching it using the
dataProvider
of the <Refine/>
component. It doesn't directly use the resource interfaces you have defined. Instead, it infers the data structure from the data returned by the dataProvider
.
However, you can customize the inferred fields using the fieldTransformer
function. This function allows you to modify the inferred fields based on your needs. You can find more information about this in the Modifying the inferred fields (https://refine.dev/docs/packages/documentation/inferencer#modifying-the-inferred-fields) section of the documentation.
For GraphQL backends, refine handles them by using the meta
properties in its data hooks. Inferencer lets you define meta values for your resources and methods in a single prop and uses it when generating the code and inferring the fields. Here's the syntax for defining the meta
values in Inferencer components:
default
is the default meta
value for all the methods. In the absence of a specific meta
value for a method for a resource, the default
value will be used.
Sources:
- Inferencer (https://refine.dev/docs/packages/documentation/inferencer)
- Usage with GraphQL backends and meta
values (https://refine.dev/docs/packages/documentation/inferencer)
----------------------
Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:
----------------------<@263527875506995202> kapa.ai is still learning and improving, please let us know how it did by reacting below
afraid-scarletOP•2y ago
@kapa.ai well if the inferencer uses dataProvider to query my backend API, if I don't already have data for those resources yet, assuming I am building out my application, then I would have to manually create my resources and establish the proper relationships before running the inferencer? Sounds like more leg work for the developer and the tool, wouldn't it be easier to have the interfaces to act as a "schema" for the database model? This inferencer assumes I already have data fully established to inference off of
Let me check the knowledge base for you. One moment! :books:
----------------------
Yes, you're correct. The Inferencer in refine does rely on the data returned by the
dataProvider
to generate the CRUD pages. If you don't have data for those resources yet, the Inferencer might not be able to generate the pages as expected.
The Inferencer is designed to help speed up the development process by automatically generating the CRUD pages based on the existing data. However, it does assume that you have some data already established.
If you're building out your application and don't have data yet, you might need to manually create your resources and establish the proper relationships before running the Inferencer. This could involve more work initially, but once the data is set up, the Inferencer can save you a lot of time by automatically generating the CRUD pages.
As for using interfaces as a "schema" for the database model, that's an interesting idea. However, the current design of the Inferencer doesn't support this. It's designed to work with the data returned by the dataProvider
, not with predefined interfaces or schemas.
If you find that the Inferencer doesn't meet your needs, you might consider manually creating your CRUD pages. This would give you more control over the design and functionality of the pages.
Sources:
- Inferencer (https://refine.dev/docs/packages/documentation/inferencer)
- Generating CRUD pages automatically with Inferencer (https://refine.dev/docs/tutorial/getting-started/headless/generate-crud-pages)
----------------------
Note: you can ask me a follow up question by @ mentioning me again
:speech_balloon:
----------------------<@263527875506995202> kapa.ai is still learning and improving, please let us know how it did by reacting below
other-emerald•2y ago
Hey @nick_montoya, yeah currently the inferencer package uses your API to determine the fields and the relations. We're planning to do some refactor and allow users to define schemas for the inferencer to use without doing any api calls (such as your use case when you don't have any data but know the expected response from the API)
But for now, you need to have something to be returned from your data provider. As a workaround, you can setup a mock data provider that returns some sample data that fits your schema and let inferencer generate the UI according to your sample data.
afraid-scarletOP•2y ago
Oh ok that sounds good! After thinking over it somemore, it's actually best to use the API to do it, because or else you will need to have your API in sync with the inference schema at all times, which could introduce bugs and make it harder to maintain for more complex apps.
I think solving the API first and then the UI presentation layer is probably the way to do it, this ensures the complexities of the backend are already figured out and you don't have to change your infrastructure around the frontend.
But perhaps there is a solution to allow both, but I can see why going the API route is good
other-emerald•2y ago
Our aim with the refactor is actually to have the UI generation and the inference part separated. This way you can either choose to generate using a schema or with your API and let the UI generation part handle the rest
Thank you for the feedback! 🚀
extended-yellow•2y ago
Guys hello!
Lil question: what can I do with inferencer If I want to draw my fields as it is, if this suspicious relation analysis is't needed?