• 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

74.29
/src/Type/WPInputObjectType.php
1
<?php
2
namespace WPGraphQL\Type;
3

4
use GraphQL\Error\InvariantViolation;
5
use GraphQL\Type\Definition\InputObjectField;
6
use GraphQL\Type\Definition\InputObjectType;
7
use GraphQL\Utils\Utils;
8
use WPGraphQL\Registry\TypeRegistry;
9

10
/**
11
 * Class WPInputObjectType
12
 *
13
 * Input types should extend this class to take advantage of the helper methods for formatting
14
 * and adding consistent filters.
15
 *
16
 * @phpstan-import-type InputObjectConfig from \GraphQL\Type\Definition\InputObjectType
17
 * @phpstan-import-type FieldConfig from \GraphQL\Type\Definition\InputObjectType
18
 *
19
 * @package WPGraphQL\Type
20
 * @since 0.0.5
21
 */
22
class WPInputObjectType extends InputObjectType {
23

24
        /**
25
         * WPInputObjectType constructor.
26
         *
27
         * @param array<string,mixed>              $config The Config to set up an Input Type
28
         *
29
         * @phpstan-param InputObjectConfig $config
30
         * @param \WPGraphQL\Registry\TypeRegistry $type_registry The TypeRegistry instance
31
         */
32
        public function __construct( array $config, TypeRegistry $type_registry ) {
425✔
33
                $name           = $config['name'] ?? $this->inferName();
425✔
34
                $config['name'] = apply_filters( 'graphql_type_name', $name, $config, $this );
425✔
35

36
                if ( array_key_exists( 'fields', $config ) && is_array( $config['fields'] ) ) {
425✔
37
                        $config['fields'] = function () use ( $config, $type_registry ) {
425✔
38
                                $fields = $this->prepare_fields( $config['fields'], $config['name'], $config, $type_registry );
425✔
39
                                $fields = $type_registry->prepare_fields( $fields, $config['name'] );
425✔
40

41
                                return $fields;
425✔
42
                        };
425✔
43
                }
44

45
                parent::__construct( $config );
425✔
46
        }
47

48
        /**
49
         * Prepare_fields
50
         *
51
         * This function sorts the fields and applies a filter to allow for easily
52
         * extending/modifying the shape of the Schema for the type.
53
         *
54
         * @param array<string,array<string,mixed>> $fields
55
         * @param string                            $type_name
56
         * @param array<string,mixed>               $config
57
         * @param \WPGraphQL\Registry\TypeRegistry  $type_registry
58
         * @return array<string,array<string,mixed>>
59
         *
60
         * @phpstan-param array<string,FieldConfig> $fields
61
         * @phpstan-return array<string,FieldConfig>
62
         * @since 0.0.5
63
         */
64
        public function prepare_fields( array $fields, string $type_name, array $config, TypeRegistry $type_registry ) {
425✔
65

66
                /**
67
                 * Filter all object fields, passing the $typename as a param
68
                 *
69
                 * This is useful when several different types need to be easily filtered at once. . .for example,
70
                 * if ALL types with a field of a certain name needed to be adjusted, or something to that tune
71
                 *
72
                 * @param array<string,FieldConfig>         $fields        The array of fields for the object config
73
                 * @param string                            $type_name     The name of the object type
74
                 * @param array<string,mixed>               $config        The type config
75
                 * @param \WPGraphQL\Registry\TypeRegistry  $type_registry The TypeRegistry instance
76
                 */
77
                $fields = apply_filters( 'graphql_input_fields', $fields, $type_name, $config, $type_registry );
425✔
78

79
                /**
80
                 * Filter once with lowercase, once with uppercase for Back Compat.
81
                 */
82
                $lc_type_name = lcfirst( $type_name );
425✔
83
                $uc_type_name = ucfirst( $type_name );
425✔
84

85
                /**
86
                 * Filter the fields with the typename explicitly in the filter name
87
                 *
88
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
89
                 * more specific overrides
90
                 *
91
                 * @param array<string,FieldConfig> $fields        The array of fields for the object config
92
                 * @param \WPGraphQL\Registry\TypeRegistry  $type_registry The TypeRegistry instance
93
                 */
94
                $fields = apply_filters( "graphql_{$lc_type_name}_fields", $fields, $type_registry );
425✔
95

96
                /**
97
                 * Filter the fields with the typename explicitly in the filter name
98
                 *
99
                 * This is useful for more targeted filtering, and is applied after the general filter, to allow for
100
                 * more specific overrides
101
                 *
102
                 * @param array<string,FieldConfig>            $fields        The array of fields for the object config
103
                 * @param \WPGraphQL\Registry\TypeRegistry $type_registry The TypeRegistry instance
104
                 */
105
                $fields = apply_filters( "graphql_{$uc_type_name}_fields", $fields, $type_registry );
425✔
106

107
                /**
108
                 * Sort the fields alphabetically by key. This makes reading through docs much easier
109
                 *
110
                 * @since 0.0.2
111
                 */
112
                ksort( $fields );
425✔
113

114
                return $fields;
425✔
115
        }
116

117
        /**
118
         * Validates type config and throws if one of type options is invalid.
119
         *
120
         * This method is overridden from the parent GraphQL\Type\Definition\InputObjectType class
121
         * to support WPGraphQL's filter system. The parent implementation only accepts iterables
122
         * for fields, but WPGraphQL's filters (like graphql_input_fields and graphql_{type}_fields)
123
         * might return a single InputObjectField instance. This override allows for both iterables
124
         * and single InputObjectField instances to be valid field values.
125
         *
126
         * @throws \GraphQL\Error\InvariantViolation
127
         */
128
        public function assertValid(): void {
9✔
129
                Utils::assertValidName( $this->name );
9✔
130

131
                $fields = $this->config['fields'] ?? null;
9✔
132
                if ( is_callable( $fields ) ) {
9✔
NEW
133
                        $fields = $fields();
×
134
                }
135

136
                // Validate that $fields is either an iterable or an InputObjectField
137
                if ( ! is_iterable( $fields ) && ! $fields instanceof InputObjectField ) {
9✔
NEW
138
                        $invalidFields = Utils::printSafe( $fields );
×
139

NEW
140
                        throw new InvariantViolation(
×
NEW
141
                                sprintf(
×
142
                                        // translators: %1$s is the name of the type and %2$s is the invalid fields
NEW
143
                                        esc_html__( '%1$s fields must be an array, an iterable, or an InputObjectField instance, got: %2$s', 'wp-graphql' ),
×
NEW
144
                                        esc_html( $this->name ),
×
NEW
145
                                        esc_html( $invalidFields )
×
NEW
146
                                )
×
NEW
147
                        );
×
148
                }
149

150
                $resolvedFields = $this->getFields();
9✔
151

152
                foreach ( $resolvedFields as $field ) {
9✔
153
                        $field->assertValid( $this );
9✔
154
                }
155
        }
156
}
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