• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 13316763745

13 Feb 2025 08:45PM UTC coverage: 82.712% (-0.3%) from 83.023%
13316763745

push

github

web-flow
Merge pull request #3307 from wp-graphql/release/v2.0.0

release: v2.0.0

195 of 270 new or added lines in 20 files covered. (72.22%)

180 existing lines in 42 files now uncovered.

13836 of 16728 relevant lines covered (82.71%)

299.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.14
/src/Type/WPObjectType.php
1
<?php
2

3
namespace WPGraphQL\Type;
4

5
use GraphQL\Type\Definition\ObjectType;
6
use WPGraphQL\Data\DataSource;
7
use WPGraphQL\Registry\TypeRegistry;
8

9
/**
10
 * Class WPObjectType
11
 *
12
 * @phpstan-import-type ObjectConfig from \GraphQL\Type\Definition\ObjectType
13
 *
14
 * Object Types should extend this class to take advantage of the helper methods
15
 * and consistent filters.
16
 *
17
 * @package WPGraphQL\Type
18
 * @since   0.0.5
19
 */
20
class WPObjectType extends ObjectType {
21

22
        use WPInterfaceTrait;
23

24
        /**
25
         * Holds the node_interface definition allowing WPObjectTypes
26
         * to easily define themselves as a node type by implementing
27
         * self::$node_interface
28
         *
29
         * @var array<string,mixed>|\WPGraphQL\Type\InterfaceType\Node $node_interface
30
         * @since 0.0.5
31
         */
32
        private static $node_interface;
33

34
        /**
35
         * Instance of the Type Registry
36
         *
37
         * @var \WPGraphQL\Registry\TypeRegistry
38
         */
39
        public $type_registry;
40

41
        /**
42
         * @var array<string, array<string, mixed>>
43
         */
44
        public $fields;
45

46
        /**
47
         * @var array<\GraphQL\Type\Definition\InterfaceType>
48
         */
49
        public $interfaces = [];
50

51
        /**
52
         * WPObjectType constructor.
53
         *
54
         * Sets up a WPObjectType instance with the given configuration and type registry.
55
         *
56
         * @param array<string, mixed>             $config The configuration array for setting up the WPObjectType.
57
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The type registry to associate with the WPObjectType.
58
         *
59
         * @phpstan-param ObjectConfig $config
60
         *
61
         * @throws \Exception If there is an error during instantiation.
62
         * @since 0.0.5
63
         */
64
        public function __construct( array $config, TypeRegistry $type_registry ) {
604✔
65

66
                /**
67
                 * Get the Type Registry
68
                 */
69
                $this->type_registry = $type_registry;
604✔
70

71
                /**
72
                 * Filter the config of WPObjectType
73
                 *
74
                 * @param ObjectConfig          $config         Array of configuration options passed to the WPObjectType when instantiating a new type
75
                 * @param \WPGraphQL\Type\WPObjectType $wp_object_type The instance of the WPObjectType class
76
                 */
77
                $config = apply_filters( 'graphql_wp_object_type_config', $config, $this );
604✔
78

79
                $this->config = $config;
604✔
80

81
                /**
82
                 * Set the Types to start with capitals
83
                 */
84
                $name           = isset( $config['name'] ) ? ucfirst( $config['name'] ) : $this->inferName();
604✔
85
                $config['name'] = apply_filters( 'graphql_type_name', $name, $config, $this );
604✔
86

87
                /**
88
                 * Set up the fields
89
                 *
90
                 * @return array<string, array<string, mixed>> $fields
91
                 */
92
                $config['fields'] = ! empty( $this->fields ) ? $this->fields : $this->get_fields( $config, $this->type_registry );
604✔
93

94
                /**
95
                 * Set up the Interfaces
96
                 */
97
                $config['interfaces'] = $this->getInterfaces();
604✔
98

99
                /**
100
                 * Run an action when the WPObjectType is instantiating
101
                 *
102
                 * @param array<string,mixed>          $config         Array of configuration options passed to the WPObjectType when instantiating a new type
103
                 * @param \WPGraphQL\Type\WPObjectType $wp_object_type The instance of the WPObjectType class
104
                 */
105
                do_action( 'graphql_wp_object_type', $config, $this );
604✔
106

107
                parent::__construct( $config );
604✔
108
        }
109

110
        /**
111
         * Get the interfaces implemented by the ObjectType
112
         *
113
         * @return \GraphQL\Type\Definition\InterfaceType[]
114
         */
115
        public function getInterfaces(): array {
712✔
116
                if ( ! empty( $this->interfaces ) ) {
712✔
117
                        return $this->interfaces;
675✔
118
                }
119
                $this->interfaces = $this->get_implemented_interfaces();
604✔
120
                return $this->interfaces;
604✔
121
        }
122

123
        /**
124
         * Node_interface
125
         *
126
         * This returns the node_interface definition allowing
127
         * WPObjectTypes to easily implement the node_interface
128
         *
129
         * @return array<string,mixed>|\WPGraphQL\Type\InterfaceType\Node
130
         * @since 0.0.5
131
         */
UNCOV
132
        public static function node_interface() {
×
133
                if ( null === self::$node_interface ) {
×
134
                        $node_interface       = DataSource::get_node_definition();
×
NEW
135
                        self::$node_interface = $node_interface['Node'];
×
136
                }
137

138
                return self::$node_interface;
×
139
        }
140

141
        /**
142
         * This function sorts the fields and applies a filter to allow for easily
143
         * extending/modifying the shape of the Schema for the type.
144
         *
145
         * @param array<string,mixed> $fields    The array of fields for the object config
146
         * @param string              $type_name The name of the type to prepare fields for
147
         * @param array<string,mixed> $config    The config for the Object Type
148
         *
149
         * @return array<string,mixed>
150
         * @since 0.0.5
151
         */
152
        public function prepare_fields( $fields, $type_name, $config ) {
604✔
153

154
                /**
155
                 * Filter all object fields, passing the $typename as a param
156
                 *
157
                 * This is useful when several different types need to be easily filtered at once. . .for example,
158
                 * if ALL types with a field of a certain name needed to be adjusted, or something to that tune
159
                 *
160
                 * @param array<string,mixed>              $fields         The array of fields for the object config
161
                 * @param string                           $type_name      The name of the object type
162
                 * @param \WPGraphQL\Type\WPObjectType     $wp_object_type The WPObjectType Class
163
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry  The Type Registry
164
                 */
165
                $fields = apply_filters( 'graphql_object_fields', $fields, $type_name, $this, $this->type_registry );
604✔
166

167
                /**
168
                 * Filter once with lowercase, once with uppercase for Back Compat.
169
                 */
170
                $lc_type_name = lcfirst( $type_name );
604✔
171
                $uc_type_name = ucfirst( $type_name );
604✔
172

173
                /**
174
                 * Filter the fields with the typename explicitly in the filter name
175
                 *
176
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
177
                 * more specific overrides
178
                 *
179
                 * @param array<string,mixed>              $fields         The array of fields for the object config
180
                 * @param \WPGraphQL\Type\WPObjectType     $wp_object_type The WPObjectType Class
181
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry  The Type Registry
182
                 */
183
                $fields = apply_filters( "graphql_{$lc_type_name}_fields", $fields, $this, $this->type_registry );
604✔
184

185
                /**
186
                 * Filter the fields with the typename explicitly in the filter name
187
                 *
188
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
189
                 * more specific overrides
190
                 *
191
                 * @param array<string,mixed>              $fields         The array of fields for the object config
192
                 * @param \WPGraphQL\Type\WPObjectType     $wp_object_type The WPObjectType Class
193
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry  The Type Registry
194
                 */
195
                $fields = apply_filters( "graphql_{$uc_type_name}_fields", $fields, $this, $this->type_registry );
604✔
196

197
                /**
198
                 * This sorts the fields alphabetically by the key, which is super handy for making the schema readable,
199
                 * as it ensures it's not output in just random order
200
                 */
201
                ksort( $fields );
604✔
202

203
                return $fields;
604✔
204
        }
205
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc