forked from pimcore/data-hub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOCS] sample for adding custom queries
1 parent
30754b0
commit a43a702
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.