How to create graphql schema by passing argument for custom magento 2 module with custom table?

How to create graphql schema by passing argument for custom magento 2 module with custom table? – Good Day for every one. Today’s article is going to cover the GraphQl schema for custom table and retrieve the data by passing an argument.
For this i am going to use a custom magento 2 module. This module help the admin to add the Word of the Day to show in the home page. Here we are going to pass the Current day as argument. And retrieve the Word of the day collection to the home page.

The word of the day module contain 5 columns in its database table. Word, Word Class, Meaning, Example Sentence and the Date. As our previous article we have few steps to achieve this task.Following are the main steps to consider apart from creating the custom module.All other stuffs related to custom module i am not going to explain here.

Step 1 – How to write schema.graphqls.
Step 2 – Writing Resolver file.
Step 3 – Create Data Provider file.

Step 1 is one of the main part where we want to pass our argument to GraphQl query. schema.graphqls file should go under app/etc/schema.graphqls

In schema.graphqls we are passing date_to_show as our argument.We want to pass the argument to our resolver and get the data accordingly.Here i am using wordOfTheDayByDate.when we going to get the data we want to use this wordOfTheDayByDate in the query.Another main stuff we want to consider is the resolver file path.Path is [ class: “Ayakil\Wordoftheday\Model\Resolver\Wordoftheday”].
type WodCollection{} is our return data array. We want to pass the word, verb,meaning,details in wordOfTheDayByDate query along with date_to_show as argument.

Step 2 – Creating resolver file under Model directory. File path should be app/code/Ayakil/Wordoftheday/Model/Resolver/Wordoftheday.php as mentioned in the schema.grapqls file.

Important things to capture here in this resolver file is function getDates(), In this function we are passing our arguments and return the argument.To capture the exact argument we want to pass the exact argument name which we given in the schema.graphqls Query. Here we passed date_to_show, so we want to get it as $args[‘date_to_show’]. Why i am mentioning this here was i had to spend more time to realize this as i did this very first time.

Step 3 – Creating Data provider file.File path should be app/code/Ayakil/Wordoftheday/Model/Resolver/DataProvider/Wordoftheday.php

This data provider file simply return the data collection. All logic set in the collection and the function getWordofthedayByDate() return all the word of the day data to relevant day.While we setup the data array ( $wordofthedayData = [];) pass the id to the array as mention in the code to avoid null value return.
Ex: $wod_id$wordofthedayData[$wod_id][‘word’] = $wod->getWord();
After run upgrade command to install the module we can check the graphQl query response.

To check the response you can use some extensions like ChromeiQL or Altair GraphQL addon. To get clear understand about GraphQl and how to access it follow What is GraphQl and how to access it?. I am using Altair GraphQl Addon.

To check our query we want to set up the endpoint.It is usually coming like <magento_root_url>/graphql. Ex :- http://ayakil.local/graphql.
Here is the query and result.

word of the day sample query and result
GraphQl query & Result

Sample GraphQl Query

Create graphql schema for custom module explained.That’s it. Have a nice day.Enjoy coding , Learn , Experience , Teach and Help.

1 thought on “How to create graphql schema by passing argument for custom magento 2 module with custom table?”

Leave a Reply

Your email address will not be published. Required fields are marked *