Many-to-many-fields work in Pimcore GraphQL (data-hub), though it might take you some time to figure out how it exactly works.

If you haven’t added data-hub, read this post first about data-hub.

Many-to-one field

Say we have object Fridge and object Brand. Object Fridge has a many-to-one field with Object Brand.

In order for this to work you have to add both object Fridge and Brand in the Schema Query.

Select the fields you want to be available in GraphQL for both objects (Fridge and Brand).
Select also the folders of the objects that should be available (in tab Security Definition under "objects"). They need to include the folders from both objects .

Click save.

Click "Open in Tab", this opens the editor ‘GraphiQL’ in a new browser tab.

Enter a variant of this query:

{
    getFridge (id: 141) {
        id,
        name,
        description,
        brand {
            ... on object_Brand {
                name
            }
        }
    }
}

The Many To One field requires:

  • the field name as it’s defined in the Fridge class (brand in this example)
  • 3 dots (…) followed by the fixed word object_ followed by the class name of the linked object (... on object_brand in this example)
  • the field names that you want to return from the brand.

Many-to-many fields

If you work with a many-to-many fields, you work exactly as in many-to-one fields. The only difference is an extra line in the query.

  • the fixed word element
{
    getFridge (id: 141) {
        id,
        name,
        description,
        brand {
            element {
                ... on object_Brand {
                    name
                }
            }
        }
    }
}

I discovered in some cases it also works without the "… on object_" line.

Possible errors:

Cannot query field "name" on type "object_Brand".

This happens when you didn’t add the object in the Schema Definition of the Datahub configuration.
The linked objects must also be defined.

Error "type definition … not found"

This was a bug on "Advanced Many-To-Many Object Relation" types. It’s fixed in the dev-mater branch of data-hub. See: https://github.com/pimcore/data-hub/issues/39

Execute the following command to fix this:

composer update pimcore/data-hub

Rss Comments

Comments

No comments yet.

Leave a comment






Wordpress.org clearPaper by CreativeBits.it Copyright © 2012-2019 Robin Brackez. All rights reserved. By visiting this site you agree to accept cookies that are purely used to check how many visitors I have. Theme by: creativebits. Custom adaptations by Robin Brackez.