Skip to content
This repository was archived by the owner on Mar 29, 2020. It is now read-only.

Commit 1acc506

Browse files
committed
allow types to be serialized
1 parent 65f9e57 commit 1acc506

File tree

8 files changed

+140
-114
lines changed

8 files changed

+140
-114
lines changed

src/Commands/CacheCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CacheCommand extends Command
3737
public function __construct(FileStore $cache)
3838
{
3939
parent::__construct();
40-
40+
4141
$this->cache = $cache;
4242
}
4343

src/Schema/GraphQL.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GraphQL\Type\Definition\InterfaceType;
1212
use Illuminate\Support\Collection;
1313
use Illuminate\Database\Eloquent\Model;
14+
use SuperClosure\SerializableClosure;
1415

1516
use Nuwave\Relay\Support\ValidationError;
1617
use Nuwave\Relay\Support\Definition\RelayType;
@@ -263,10 +264,12 @@ public function connection($name, $resolve = null, $fresh = false)
263264
{
264265
$this->checkType($name);
265266

266-
if ($resolve && !$resolve instanceof Closure) {
267-
$resolve = function ($root, array $args, ResolveInfo $info) use ($resolve) {
268-
return $this->resolveConnection($root, $args, $info, $resolve);
269-
};
267+
if (!is_null($resolve) && !$resolve instanceof Closure) {
268+
$connectionResolver = $this->getConnectionResolver();
269+
270+
$resolve = new SerializableClosure(function ($root, array $args, ResolveInfo $info) use ($resolve, $connectionResolver) {
271+
return $connectionResolver->resolve($root, $args, $info, $resolve);
272+
});
270273
}
271274

272275
if (!$fresh && $this->connectionInstances->has($name)) {

src/Support/Definition/EdgeType.php

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,14 @@ public function fields()
4646
{
4747
return [
4848
'node' => [
49-
'type' => function () {
50-
if (is_object($this->type)) {
51-
return $this->type;
52-
}
53-
54-
return $this->getNodeType($this->type);
55-
},
49+
'type' => array($this, 'nodeType'),
5650
'description' => 'The item at the end of the edge.',
57-
'resolve' => function ($edge) {
58-
return $edge;
59-
},
51+
'resolve' => array($this, 'edge'),
6052
],
6153
'cursor' => [
6254
'type' => Type::nonNull(Type::string()),
6355
'description' => 'A cursor for use in pagination.',
64-
'resolve' => function ($edge) {
65-
if (is_array($edge) && isset($edge['relayCursor'])) {
66-
return $edge['relayCursor'];
67-
} elseif (is_array($edge->attributes)) {
68-
return $edge->attributes['relayCursor'];
69-
}
70-
71-
return $edge->relayCursor;
72-
},
56+
'resolve' => array($this, 'resolveCursor'),
7357
]
7458
];
7559
}
@@ -98,6 +82,48 @@ public function toType()
9882
return new ObjectType($this->toArray());
9983
}
10084

85+
/**
86+
* Type of node.
87+
*
88+
* @return mixed
89+
*/
90+
public function nodeType()
91+
{
92+
if (is_object($this->type)) {
93+
return $this->type;
94+
}
95+
96+
return $this->getNodeType($this->type);
97+
}
98+
99+
/**
100+
* Resolve node at end of edge.
101+
*
102+
* @param mixed $edge
103+
* @return mixed
104+
*/
105+
public function resolveNode($edge)
106+
{
107+
return $edge;
108+
}
109+
110+
/**
111+
* Resolve cursor for edge.
112+
*
113+
* @param mixed $edge
114+
* @return string
115+
*/
116+
public function resolveCursor($edge)
117+
{
118+
if (is_array($edge) && isset($edge['relayCursor'])) {
119+
return $edge['relayCursor'];
120+
} elseif (is_array($edge->attributes)) {
121+
return $edge->attributes['relayCursor'];
122+
}
123+
124+
return $edge->relayCursor;
125+
}
126+
101127
/**
102128
* Get node at the end of the edge.
103129
*

src/Support/Definition/GraphQLMutation.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,6 @@
77

88
class GraphQLMutation extends GraphQLField
99
{
10-
/**
11-
* The validator instance.
12-
*
13-
* @var \Illuminate\Validation\Factory
14-
*/
15-
protected $validator;
16-
17-
/**
18-
* The container instance of GraphQL.
19-
*
20-
* @var \Laravel\Lumen\Application|mixed
21-
*/
22-
protected $graphQL;
23-
24-
/**
25-
* GraphQLMutation constructor.
26-
*
27-
*/
28-
public function __construct()
29-
{
30-
parent::__construct();
31-
32-
$this->validator = app('validator');
33-
34-
$this->graphQL = app('graphql');
35-
}
36-
3710
/**
3811
* Get the validation rules.
3912
*
@@ -105,7 +78,7 @@ protected function validate(array $args)
10578
$rules = $this->getRules(...$args);
10679

10780
if (sizeof($rules)) {
108-
$validator = $this->validator->make($args['input'], $rules);
81+
$validator = app('validator')->make($args['input'], $rules);
10982

11083
if ($validator->fails()) {
11184
throw with(new ValidationError('Validation failed', $validator));

src/Support/Definition/GraphQLType.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@
99

1010
class GraphQLType extends Fluent
1111
{
12-
/**
13-
* The container instance of GraphQL.
14-
*
15-
* @var \Laravel\Lumen\Application|mixed
16-
*/
17-
protected $graphQL;
18-
19-
/**
20-
* GraphQLType constructor.
21-
*
22-
*/
23-
public function __construct()
24-
{
25-
parent::__construct();
26-
27-
$this->graphQL = app('graphql');
28-
}
29-
3012
/**
3113
* Type fields.
3214
*

src/Support/Definition/PageInfoType.php

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,89 @@ public function fields()
3333
'hasNextPage' => [
3434
'type' => Type::nonNull(Type::boolean()),
3535
'description' => 'When paginating forwards, are there more items?',
36-
'resolve' => function ($collection) {
37-
if ($collection instanceof LengthAwarePaginator) {
38-
return $collection->hasMorePages();
39-
}
40-
41-
return false;
42-
}
36+
'resolve' => array($this, 'resolveHasNextPage')
4337
],
4438
'hasPreviousPage' => [
4539
'type' => Type::nonNull(Type::boolean()),
4640
'description' => 'When paginating backwards, are there more items?',
47-
'resolve' => function ($collection) {
48-
if ($collection instanceof LengthAwarePaginator) {
49-
return $collection->currentPage() > 1;
50-
}
51-
52-
return false;
53-
}
41+
'resolve' => array($this, 'resolveHasPreviousPage')
5442
],
5543
'startCursor' => [
5644
'type' => Type::string(),
5745
'description' => 'When paginating backwards, the cursor to continue.',
58-
'resolve' => function ($collection) {
59-
if ($collection instanceof LengthAwarePaginator) {
60-
return $this->encodeGlobalId(
61-
'arrayconnection',
62-
$collection->firstItem() * $collection->currentPage()
63-
);
64-
}
65-
66-
return null;
67-
}
46+
'resolve' => array($this, 'resolveStartCursor')
6847
],
6948
'endCursor' => [
7049
'type' => Type::string(),
7150
'description' => 'When paginating forwards, the cursor to continue.',
72-
'resolve' => function ($collection) {
73-
if ($collection instanceof LengthAwarePaginator) {
74-
return $this->encodeGlobalId(
75-
'arrayconnection',
76-
$collection->lastItem() * $collection->currentPage()
77-
);
78-
}
79-
80-
return null;
81-
}
51+
'resolve' => array($this, 'resolveEndCursor')
8252
]
8353
];
8454
}
55+
56+
/**
57+
* Determine if collection has a next page.
58+
*
59+
* @param mixed $collection
60+
* @return boolean
61+
*/
62+
public function resolveHasNextPage($collection)
63+
{
64+
if ($collection instanceof LengthAwarePaginator) {
65+
return $collection->hasMorePages();
66+
}
67+
68+
return false;
69+
}
70+
71+
/**
72+
* Determine if collection has previous page.
73+
*
74+
* @param mixed $collection
75+
* @return boolean
76+
*/
77+
public function resolveHasPreviousPage($collection)
78+
{
79+
if ($collection instanceof LengthAwarePaginator) {
80+
return $collection->currentPage() > 1;
81+
}
82+
83+
return false;
84+
}
85+
86+
/**
87+
* Resolve start cursor for edge.
88+
*
89+
* @param mixed $collection
90+
* @return string|null
91+
*/
92+
public function startCursor($collection)
93+
{
94+
if ($collection instanceof LengthAwarePaginator) {
95+
return $this->encodeGlobalId(
96+
'arrayconnection',
97+
$collection->firstItem() * $collection->currentPage()
98+
);
99+
}
100+
101+
return null;
102+
}
103+
104+
/**
105+
* Resolve end cursor for edge.
106+
*
107+
* @param mixed $collection
108+
* @return string|null
109+
*/
110+
public function resolveEndCursor($collection)
111+
{
112+
if ($collection instanceof LengthAwarePaginator) {
113+
return $this->encodeGlobalId(
114+
'arrayconnection',
115+
$collection->lastItem() * $collection->currentPage()
116+
);
117+
}
118+
119+
return null;
120+
}
85121
}

src/Support/Definition/RelayConnectionType.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ protected function baseFields()
6767
'pageInfo' => [
6868
'type' => Type::nonNull(GraphQL::type('pageInfo')),
6969
'description' => 'Information to aid in pagination.',
70-
'resolve' => function ($collection) {
71-
return $collection;
72-
},
70+
'resolve' => array($this, 'resolvePageInfo'),
7371
],
7472
'edges' => [
7573
'type' => Type::listOf($this->buildEdgeType($this->name, $type)),
7674
'description' => 'Information to aid in pagination.',
77-
'resolve' => function ($collection) {
78-
return $this->injectCursor($collection);
79-
},
75+
'resolve' => array($this, 'resolveInjectCursor'),
8076
]
8177
];
8278
}
@@ -175,9 +171,7 @@ public function toArray()
175171
'name' => ucfirst($this->name),
176172
'description' => 'A connection to a list of items.',
177173
'fields' => $fields,
178-
'resolve' => function ($root, $args, ResolveInfo $info) {
179-
return $this->resolve($root, $args, $info, $this->name);
180-
}
174+
'resolve' => array($this, 'resolve')
181175
];
182176
}
183177

@@ -250,4 +244,14 @@ public function type()
250244
{
251245
return null;
252246
}
247+
248+
public function resolvePageInfo($collection)
249+
{
250+
return $collection;
251+
}
252+
253+
public function resolveInjectCursor($collection)
254+
{
255+
return $this->injectCursor($collection);
256+
}
253257
}

src/Support/Definition/RelayType.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ protected function connections()
5151
*/
5252
public function getConnections()
5353
{
54-
return collect($this->connections())->transform(function ($edge, $name) {
54+
$graphql = app('graphql');
55+
56+
return collect($this->connections())->transform(function ($edge, $name) use ($graphql) {
5557
if (!isset($edge['resolve'])) {
5658
$edge['resolve'] = function ($root, array $args, ResolveInfo $info) use ($name) {
5759
return GraphQL::resolveConnection($root, $args, $info, $name);

0 commit comments

Comments
 (0)