Skip to content

Commit

Permalink
[DOCS] sample for adding custom queries
Browse files Browse the repository at this point in the history
weisswurstkanone committed Mar 13, 2020
1 parent 30754b0 commit a43a702
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/GraphQL.md
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@ admin_pimcoredatahub_webservice:
* Documents: see [Document Query Documentation](./graphl/DocumentQueries.md)
* DataObjects: see [DataObject Query Documentation](./graphl/DataObjectQueries.md)
* [Adding custom queries](graphl/AddCustomQuery.md)
## Mutations
49 changes: 49 additions & 0 deletions doc/graphl/AddCustomQuery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Add Custom Query

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

See [Events and Event Listeners](https://pimcore.com/docs/6.x/Development_Documentation/Extending_Pimcore/Event_API_and_Event_Manager.html)
if you need more information on Pimcore's event mechanism.

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

$outputType = new \GraphQL\Type\Definition\ObjectType([
'name' => "CustomQueryResultType",
'fields' => [
'outputFieldA' => [
'type' => \GraphQL\Type\Definition\Type::string(),
'resolve' => function ($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) {
return "A-value for item " . $source['resolvedId'] . " is " . uniqid();
}
],
'outputFieldB' => [
'type' => \GraphQL\Type\Definition\Type::string(),
'resolve' => function ($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) {
return "B-value for item " . $source['resolvedId'] . " is " . uniqid();
}
]
]
]
);

$operation = [
'type' => $outputType,
'args' => ['itemId' => ['type' => Type::nonNull(Type::int())]],
'resolve' => function ($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) {
// resolve the item using the input parameters. Result will be passed
// to the field-level resolvers
return ['resolvedId' => $args['itemId']];
}
];

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

![iExplorer](../img/graphql/add_query.png)


Binary file added doc/img/graphql/add_query.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a43a702

Please sign in to comment.