Magento 2: GraphQl

Posted Almost 2 years ago. Visible to the public.

How to create a Mutation with single input and output

The class file

class CreateSplitOrders implements ResolverInterface {


    /**
     * @param Field $field
     * @param $context
     * @param ResolveInfo $info
     * @param array|null $value
     * @param array|null $args
     * @return \Magento\Framework\GraphQl\Query\Resolver\Value|mixed
     * @throws GraphQlAuthorizationException
     * @throws GraphQlInputException
     */
    public function resolve (Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
    {

        /** @var \Magento\GraphQl\Model\Query\ContextInterface $context */
        if (false === $context->getExtensionAttributes()->getIsCustomer()) {
            throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
        }

        if (empty($args['parent_increment_id'])) {
            throw new GraphQlInputException(__('Specify the "parent_increment_id" value.'));
        }


        return $args['parent_increment_id'];
    }
}

The schema.graphqls

type Mutation {
    createSplitOrders(parent_increment_id: String): String @resolver(class: "Vasan\\SplitOrderGraphQl\\Model\\Resolver\\CreateSplitOrders") @doc(description: "Create Split Orders")
}

The Altair testing

mutation {
  createSplitOrders(parent_increment_id: "000000001") 
}
vasan
Last edit
Almost 2 years ago
vasan
Keywords
Mutation
Posted by vasan to vasan's deck (2022-05-11 17:09)