Get entire collection data from relation widget


I’m trying to get all fields from a collection referenced in a relation widget field, not just the single value declared in “valueField”. Is there any way to do this?


This is my current configuration:

    - label: Child
      name: child
      folder: child
           - label: Name
            name: title
            widget: string

           - label: Age
            name: age
            widget: number

    - label: Father
      name: father
      folder: father
          - label: Name
            name: title
            widget: string

          - label: Children
            name: children
            widget: list
                - label: Child
                  name: child
                  widget: relation
                  collection: child
                  valueField: title

Not at this time. The relation widget currently only stores the value of the one named field.

Thanks. Is there any known workaround?

Can you give some details of what you are trying to do with the relation value and why you want all the relation fields to exist in the data rather than just resolve the data from the value at the time you access the data?

Like my example, I would like to get Name and Age from the children when I query a Father. I was looking for a more elegant to get this info than querying all Child and run it in a loop to compare strings and “attach” to the Father collection.

Generally you store a unique value, which may need to be an actual id string to ensure uniqueness, and then fetch related data at build time. Otherwise you would be storing duplicate data.

Got it. Thanks.

If it’s useful for anyone with the same question, what I did was create a datetime widget with format: x and set this field as identifier_field. This way I could fetch which Child was stored in Father.

1 Like

Is there really no way to get multiple items from a collection with the relation widget? For example to pull an Author from a collection it would really be nice to have both the name and the thumbnail.

Hi @byebyers, can you share your use case?
Do you want to access the author information during build time?
In that case you could do what @erquhart mentioned here:

Iterate over the collection with the relation widget, get the author id (e.g. name/email) and use that value to extract name and thumbnail from the authors collection.

Hey @erez! Yes I want to access author information during build time :slightly_smiling_face:

I ended up opening a new question in the forum with more details. Is that ok going forward? If not I’ll make sure to not do that.

I am not sure what you mean to iterate over the collection with the relation widget? Mainly because it seems in only takes one value to be saved for the field. I am currently looking at possibly getting this info with gatsby-node.js but I am still searching.

A new question is even better, let’s continue the discussion there.

1 Like