If I am not wrong you meant to ask What if we’re adding a custom attribute, e.g., an attribute we’ve added to the database? And how we gonna deal with making that attribute available to GraphQL. So let's proceed with an example:
Let’s assume you want to add the parent_item_id as an attribute available through the GraphQL Quote Endpoint.Now, you may already be aware, parent_item_id is an existing field (it’s not a new database attribute). But currently, it’s not supported through the native GraphQL Quote Endpoint – so it’s not accessible via GraphQL responses. We’ll walk through a simple example to add it:
First, We’ll create a module:
// File: app/code/GraphQlHandson/Example/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'GraphQlHandson_Example',
__DIR__
);
Next, We’ll add our etc/schema.graphqls file:
// File: app/code/GraphQlHandson/Example/etc/schema.graphqls
interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemTypeResolver") {
parent_item_id: Int
}
Now, refresh your database / cache:
php bin/magento setup:upgrade
…That’s it! On the client side, We can send a GraphQL query that looks like this:
query {
cart {
items {
parent_item_id
}
}
}
(Obviously in a real-world scenario, you would request additional information).
Now, the only point that confusing at first glance is the interface definition in the schema.graphqls file. …Where did we get that? Well, if you look in the native Quote graphql module:
// File>: vendor/magento/module-quote-graph-ql/etc/schema.graphqls
...
type SimpleCartItem implements CartItemInterface @doc(description: "Simple Cart Item") {
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
}
type VirtualCartItem implements CartItemInterface @doc(description: "Virtual Cart Item") {
customizable_options: [SelectedCustomizableOption] @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CustomizableOptions")
}
interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemTypeResolver") {
id: String!
quantity: Float!
product: ProductInterface!
}
...
You can see above, that by default Magento 2 does several things:
- Defines an interface CartItemInterface which is used to represent
cart items.
- Defines concrete implementations of that interface for
different product types.
Defines the fields available through GraphQL
for each of them.
So, by default, the only available fields for an
item are id, quantity, product — the last of which is a complex
ProductInterface type
In our custom module, all we did was copy the interface definition and start adding new properties to it.
That’s an important point to remember – the GraphQL definitions
function more like layout xml in Magento 2, and less like typical
extension_attributes. The beauty of this is that we can freely add new
types, modify queries, etc.
Hope this provide a clear glimpse on how you can modify the GraphQL queries by adding custom or new attributes. If yes, then kindly upvote it and mark it as an answer. Thanks!