Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 1.61 KB

27_Add_Custom_Mutations.md

File metadata and controls

44 lines (33 loc) · 1.61 KB

Add Custom Mutations

You can extend the mutation schema and add your custom mutations in the following way.

See Events and Event Listeners if you need more information on Pimcore's event mechanism.

\Pimcore::getEventDispatcher()->addListener(\Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents::PRE_BUILD,
    function (\Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent $event) {
        $config = $event->getConfig();

        $opName = "performCustomMutation";

        $inputType = new \GraphQL\Type\Definition\InputObjectType([
            'name' => "MyCustomInputTypeName",
            'fields' => [
                'myCustomFieldName' => [
                    'type' => \GraphQL\Type\Definition\Type::string()
                ]
            ]
        ]);

        $operation = [
            'type' => Type::string(),           // the result type
            'args' => [
                'id' => ['type' => Type::nonNull(\GraphQL\Type\Definition\Type::int())],
                'input' => ['type' => $inputType],
            ], 'resolve' => function ($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) {
                // do something here
                $id = $args['id'];
                return "claiming that item " . $id . " has been updated to `" . $args['input']['myCustomFieldName'] . "`";
            }

        ];

        $config['fields'][$opName] = $operation;
        $event->setConfig($config);
    });

iExplorer