Skip to content

Commit

Permalink
Merge pull request #122 from TomHAnderson/feature/complete-connection
Browse files Browse the repository at this point in the history
Feature/complete connection
  • Loading branch information
TomHAnderson authored Aug 1, 2024
2 parents 6f3c06b + 7b60bbc commit 216705d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 20 deletions.
33 changes: 13 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@ $schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'artists' => [
'type' => $driver->connection(Artist::class),
'args' => [
'filter' => $driver->filter(Artist::class),
'pagination' => $driver->pagination(),
],
'resolve' => $driver->resolve(Artist::class),
],
'artists' => $driver->completeConnection(Artist::class),
],
]),
'mutation' => new ObjectType([
Expand Down Expand Up @@ -218,23 +211,23 @@ Example

```gql
{
artists (
filter: {
name: {
contains: "Dead"
}
}
artists (
filter: {
name: {
contains: "Dead"
}
}
) {
edges {
node {
id
name
performances (
filter: {
venue: {
eq: "The Fillmore"
}
}
performances (
filter: {
venue: {
eq: "The Fillmore"
}
}
) {
edges {
node {
Expand Down
25 changes: 25 additions & 0 deletions docs/driver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ It has many options and top-level functions are detailed here.
Functions
=========

completeConnection()
--------------------

This is a short cut to using connection(), pagination(), resolve(), and filter().
There are two parameters:

1. Doctrine entity class name, required,
2. queryBuilderEvent name, optional.

.. code-block:: php
use ApiSkeletons\Doctrine\ORM\GraphQL\Driver;
$driver = new Driver($this->getEntityManager());
$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'artists' => $driver->completeConnection(Artist::class),
],
]),
]);
connection(), pagination(), and resolve()
-----------------------------------------

Expand Down
17 changes: 17 additions & 0 deletions src/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,21 @@ public function input(string $entityClass, array $requiredFields = [], array $op
{
return $this->get(Input\InputFactory::class)->get($entityClass, $requiredFields, $optionalFields);
}

/**
* Return an array defining a GraphQL endpoint.
*
* @return mixed[]
*/
public function completeConnection(string $id, string|null $eventName = null): array
{
return [
'type' => $this->connection($id),
'args' => [
'filter' => $this->filter($id),
'pagination' => $this->pagination(),
],
'resolve' => $this->resolve($id, $eventName),
];
}
}
24 changes: 24 additions & 0 deletions test/Feature/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,28 @@ public function testTypeMethodInvalidType(): void

$this->assertInstanceOf(BooleanType::class, $driver->type('custom'));
}

public function testCompleteConnection(): void
{
$driver = new Driver($this->getEntityManager());

$schema = new Schema([
'query' => new ObjectType([
'name' => 'query',
'fields' => [
'artist' => $driver->completeConnection(Artist::class),
],
]),
]);

$query = '{
artist (filter: { name: { contains: "dead" } })
{ edges { node { id name performances { edges { node { venue recordings { edges { node { source } } } } } } } } }
}';

$result = GraphQL::executeQuery($schema, $query);
$output = $result->toArray();

$this->assertEquals('Grateful Dead', $output['data']['artist']['edges'][0]['node']['name']);
}
}

0 comments on commit 216705d

Please sign in to comment.