-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-config.php
127 lines (111 loc) · 3 KB
/
class-config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
use GraphQL\Error\InvariantViolation;
use WPGraphQL\Data\Connection\PostObjectConnectionResolver;
use WPGraphQL\Data\Connection\TermObjectConnectionResolver;
use WPGraphQL\Data\Connection\UserConnectionResolver;
use WPGraphQL\Data\DataSource;
final class WPGraphQL_MB_Relationships_Config
{
/**
* Type name
*/
public $type_name;
/**
* Type object
*/
public $type_object;
/**
* GraphQL type name
*/
public $graphql_type_name;
/**
* GraphQL connection name
*/
public $connection_name;
/**
* GraphQL connection type name
*/
public $connection_type;
/**
* GraphQL connection arguments
*/
public $connection_args;
/**
* GraphQL connection resolve function
*/
public $resolve = null;
/**
* GraphQL connection resolve node function
*/
public $resolve_node = null;
/**
* WordPress object type
* post | user | term
*/
public $object_type_name;
function __construct($settings)
{
$this->object_type_name = $settings['object_type'];
switch ($this->object_type_name) {
default:
case 'post':
$this->type_name = $settings['field']['post_type'];
$this->type_object = get_post_type_object($this->type_name);
$this->graphql_type_name = $this->type_object->graphql_single_name;
break;
// case 'term':
// $this->type_name = $settings['field']['taxonomy'];
// $this->type_object = get_taxonomy($this->type_name);
// break;
case 'user':
$this->type_name = 'user';
$this->type_object = null;
$this->graphql_type_name = 'User';
break;
}
$this->connection_name = $settings['graphql_name'];
$this->connection_args = isset( $settings['graphql_args'] ) ? $settings['graphql_args'] : [];
$this->type_object = get_post_type_object($this->type_name);
if (array_key_exists('resolve', $settings)) {
$this->resolve = $settings['resolve'];
}
if (array_key_exists('resolve_node', $settings)) {
$this->resolve_node = $settings['resolve_node'];
}
}
/**
* Register WPGraphQL MB Relationships config.
*
* @access public
* @since 0.3.0
* @return void
*/
public function should_register()
{
return $this->type_name === 'user' ||
($this->type_object !== null &&
$this->type_object->show_in_graphql);
}
/**
* Get applicable connection resolver
*
* @access public
* @since 0.4.0
* @return void
*/
public function get_resolver(...$args)
{
switch ($this->object_type_name) {
case 'post':
$resolver = new PostObjectConnectionResolver(...$args);
// Meta Box does not want post_parent set
$resolver->setQueryArg('post_parent', null);
return $resolver;
// case 'term':
// return new TermObjectConnectionResolver(...$args);
case 'user':
return new UserConnectionResolver(...$args);
}
throw new InvariantViolation('Unsupported object_type_name for wpgraphql-mb-relationships');
}
}