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

ReferenceManyField implementation #29

Open
baskin opened this issue Apr 10, 2020 · 10 comments
Open

ReferenceManyField implementation #29

baskin opened this issue Apr 10, 2020 · 10 comments

Comments

@baskin
Copy link

baskin commented Apr 10, 2020

Hi @Steams
Just got started with this project. Looks great till now. Many thanks :).

I came to a point where I used a ReferenceManyField in list view. This component fetches a list of referenced records by reverse lookup of the current record.id in other resource (dataProvider.getManyReference()).

Looking at the n/w calls, it seems like the implementation is making 1 call for each (current) record in the list. I think it can be collapsed to a single call by using the 'in' operator.
Can you kindly take a look?

Thanks

@lapidus
Copy link

lapidus commented Apr 16, 2020

Hi there! Is this to support many-to-many relationships?
If you have a working example (even if slow), great if you could share some code. I am having trouble with this particular step :) Thx!

@webdeb
Copy link
Contributor

webdeb commented Apr 16, 2020

@lapidus for a simple and slow example you could

      <ReferenceManyField
        label="Labels"
        reference="locals_labels"
        target="local_id"
        source="id"
      >
        <SingleFieldList>
          <ReferenceField source="label_id" reference="labels" target="id">
            <TextField source="label" />
          </ReferenceField>
        </SingleFieldList>
      </ReferenceManyField>

make sure to also export the many_to_many Resource, btw. you can export an array

export default [
  <Resource key="locals" name="locals" />,
  <Resource key="locals_labels" name="locals_labels" />
]

@lapidus
Copy link

lapidus commented Apr 16, 2020

Thanks, great! Will try when I get past the initial hurdle of "buildHasuraProvider". It's giving me a bit of a hard time ... 😅

@lapidus
Copy link

lapidus commented Apr 16, 2020

Hm, can't get it to work exactly. If I am trying to display topics for blog posts and the "bridge table" looks like this, what should I do? Thx!

blog_post_topic
blog_post_id | topic_id

Also, why do I need to export those Resources and where? I basically just want to add the many-to-many items in my listing view and edit view.

@webdeb
Copy link
Contributor

webdeb commented Apr 16, 2020

In my example above I am rendering a query which would look like

locals {
  locals_labels {
     label {
       label
    }
  }
}

the dataProvider will load the glueing part, and then the corresponding label / topic..
You have to export blog_post_topic Resource otherwise the dataprovider will complain.

@lapidus
Copy link

lapidus commented Apr 17, 2020

Thanks for all the help, now it works with the display!

Do you by any chance have experience with the AutoComplete?

From a topic edit view, I can get it to display my articles. But I can't:

  1. Get it to show the selected ones from the complete list
  2. Save the changes

My current implementation looks something like this:

<ReferenceManyField reference="article_topic" target="topic_id">
  <ReferenceArrayInput
    source="article_id"
    reference="article"
   >
      <AutocompleteArrayInput
          optionText="name"
      /> 
  </ReferenceArrayInput>
</ReferenceManyField>

@gengue
Copy link

gengue commented Apr 23, 2020

I am looking a fix for this because but we shouldn't use it the ReferenceManyField + RefenceField on that way, it makes a tons of request for a data grid.
if we have defined a many-to-many relationship in our schema, it should build only one query for this.

something like this:
e.g: users -> users_roles -> roles

query MyQuery {
  users(limit: 10) {
    id
    name
    user_roles  {
      role {
        name
      }
    }
  }
}

@lapidus
Copy link

lapidus commented Apr 23, 2020

Great! Thanks! Might also simplify this?
#33

@gengue
Copy link

gengue commented Apr 27, 2020

The solution is described in the documentation: https://github.com/Steams/ra-data-hasura-graphql#example-query-related-entities

But those functions aren't exported in the library :(
should create a issue about it?

@jasper95
Copy link
Contributor

Hi @baskin!

Please check my comment here on how I worked on this use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants