After a lot of searching, I found my mistake.
My error was capturing the id in the resolver file.
private function getId(array $args): int
{
if (!isset($args['id'])) {
throw new GraphQlInputException(__('"id should be specified'));
}
return (int)$args['id'];
}
$args['id'] here as per my graphQl schema I want to pass id, but I passed something else.
And also my passed query also wrong.
I should pass like below to get the result as per my graphql schema.
query wordoftheday {
wordoftheday( id : 5){
word
verb
meaning
details
}
}
I correct all my mistakes and got it up and run, to get a clear understanding please read How to create a Graphql schema by passing an argument for custom Magento 2 module with the custom table?.
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!
Best Answer
According to your Shema.graphqls your mutation should look like below.
As per your error in the post, you don't need to include PracticeOutput in your Mutation.Just pass the return attributes defined in the output. I wrote an article about How to write grapgQl mutation to create and integrate the contact us page functionality in magento 2.3.2?.
I used graphql Mutation to implement contact us page.Please check that. Hope this answer will help you.