We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

    We can create custom api for GraphQL.

    As per the app/code/ standard, our module name is Magemonkeys/Customgraphql

    We can create schema.graphqls file in module etc folder.

    Below is the code for that file.

    type Query
    {
        CustomGraphql (
            username: String @doc(description: "Email of the customer")
            password: String @doc(description: "Password")
            fieldtype: String @doc(description: "Field Type")
        ): CustomGraphqlOutput @resolver(class: "Magemonkeys\Customgraphql\Model\Resolver\ResolverGraphql") @doc(description:"Customgraphql Datapassing")
    }
    type CustomGraphqlOutput
    {
        username: String
        password: String
        fieldtype: String
    }

    After that we need to create Model Resolver file.

    Create file ResolverGraphql.php in this location of module
    Magemonkeys/Customgraphql/Model/Resolver/

    Below is the code for that file.

    <?php
    namespace MagemonkeysCustomgraphqlModelResolver;
    
    use MagentoFrameworkGraphQlConfigElementField;
    use MagentoFrameworkGraphQlExceptionGraphQlInputException;
    use MagentoFrameworkGraphQlQueryResolverInterface;
    use MagentoFrameworkGraphQlSchemaTypeResolveInfo;
    use MagentoFrameworkExceptionNoSuchEntityException;
    use MagentoFrameworkGraphQlExceptionGraphQlNoSuchEntityException;
    
    class ResolverGraphql implements ResolverInterface
    {
        public function resolve(
            Field $field,
            $context,
            ResolveInfo $info,
            array $value = null,
            array $args = null)
        {
            if (!isset($args['username']) || !isset($args['password']) || !isset($args['fieldtype'])||
                empty($args['username']) || empty($args['password']) || empty($args['fieldtype']))
            {
                throw new GraphQlInputException(__('Invalid arguments list please check.'));
            }
            $output = [];
            $output['username'] = $args['username'];
            $output['password'] = $args['password'];
            $output['fieldtype'] = $args['fieldtype'];
          
            return $output ;
        }
    }

    After that you can check API in Postman API tool.

    That’s it.

     

    Fill the below form if you need any Magento relate help/advise/consulting.

    With Only Agency that provides a 24/7 emergency support.

      Get a Free Quote