@@ -23,40 +23,40 @@ |
||
23 | 23 | { |
24 | 24 | $finalFields = $config['fields'] ?? []; |
25 | 25 | |
26 | - if (!isset($config['type'])) { |
|
27 | - throw new \Exception('You must specify a type for your field'); |
|
28 | - } |
|
26 | + if (!isset($config['type'])) { |
|
27 | + throw new \Exception('You must specify a type for your field'); |
|
28 | + } |
|
29 | 29 | |
30 | 30 | GraphQL\Utils\Utils::invariant($config['type'] instanceof Type, 'Must provide type.'); |
31 | 31 | |
32 | 32 | if (isset($config['errorCodes'])) { |
33 | - if(isset($config['validate'])) { |
|
34 | - /** code property */ |
|
35 | - $finalFields['code'] = [ |
|
36 | - 'type' => $this->_set(new EnumType([ |
|
37 | - 'name' => $this->_nameFromPath(array_merge($path)) . 'ErrorCode', |
|
38 | - 'description' => 'Error code', |
|
39 | - 'values' => $config['errorCodes'], |
|
40 | - ]), $config), |
|
41 | - 'description' => 'An error code', |
|
42 | - 'resolve' => static function ($value) { |
|
43 | - return $value['error'][0] ?? null; |
|
44 | - }, |
|
45 | - ]; |
|
46 | - |
|
47 | - /** |
|
48 | - * msg property |
|
49 | - */ |
|
50 | - $finalFields['msg'] = [ |
|
51 | - 'type' => Type::string(), |
|
52 | - 'description' => 'A natural language description of the issue', |
|
53 | - 'resolve' => static function ($value) { |
|
54 | - return $value['error'][1] ?? null; |
|
55 | - }, |
|
56 | - ]; |
|
57 | - } else { |
|
58 | - throw new \Exception("If you specify errorCodes, you must also provide a validate callback"); |
|
59 | - } |
|
33 | + if(isset($config['validate'])) { |
|
34 | + /** code property */ |
|
35 | + $finalFields['code'] = [ |
|
36 | + 'type' => $this->_set(new EnumType([ |
|
37 | + 'name' => $this->_nameFromPath(array_merge($path)) . 'ErrorCode', |
|
38 | + 'description' => 'Error code', |
|
39 | + 'values' => $config['errorCodes'], |
|
40 | + ]), $config), |
|
41 | + 'description' => 'An error code', |
|
42 | + 'resolve' => static function ($value) { |
|
43 | + return $value['error'][0] ?? null; |
|
44 | + }, |
|
45 | + ]; |
|
46 | + |
|
47 | + /** |
|
48 | + * msg property |
|
49 | + */ |
|
50 | + $finalFields['msg'] = [ |
|
51 | + 'type' => Type::string(), |
|
52 | + 'description' => 'A natural language description of the issue', |
|
53 | + 'resolve' => static function ($value) { |
|
54 | + return $value['error'][1] ?? null; |
|
55 | + }, |
|
56 | + ]; |
|
57 | + } else { |
|
58 | + throw new \Exception("If you specify errorCodes, you must also provide a validate callback"); |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | $type = $config['type']; |
@@ -30,7 +30,7 @@ discard block |
||
30 | 30 | GraphQL\Utils\Utils::invariant($config['type'] instanceof Type, 'Must provide type.'); |
31 | 31 | |
32 | 32 | if (isset($config['errorCodes'])) { |
33 | - if(isset($config['validate'])) { |
|
33 | + if (isset($config['validate'])) { |
|
34 | 34 | /** code property */ |
35 | 35 | $finalFields['code'] = [ |
36 | 36 | 'type' => $this->_set(new EnumType([ |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | 'values' => $config['errorCodes'], |
40 | 40 | ]), $config), |
41 | 41 | 'description' => 'An error code', |
42 | - 'resolve' => static function ($value) { |
|
42 | + 'resolve' => static function($value) { |
|
43 | 43 | return $value['error'][0] ?? null; |
44 | 44 | }, |
45 | 45 | ]; |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | $finalFields['msg'] = [ |
51 | 51 | 'type' => Type::string(), |
52 | 52 | 'description' => 'A natural language description of the issue', |
53 | - 'resolve' => static function ($value) { |
|
53 | + 'resolve' => static function($value) { |
|
54 | 54 | return $value['error'][1] ?? null; |
55 | 55 | }, |
56 | 56 | ]; |
@@ -64,18 +64,18 @@ discard block |
||
64 | 64 | $fields = []; |
65 | 65 | foreach ($type->getFields() as $key => $field) { |
66 | 66 | $newType = static::create( |
67 | - $field->config + ['typeSetter' => $config['typeSetter'] ?? null], |
|
67 | + $field->config+['typeSetter' => $config['typeSetter'] ?? null], |
|
68 | 68 | array_merge($path, [$key]) |
69 | 69 | ); |
70 | 70 | |
71 | - if (! $newType) { |
|
71 | + if (!$newType) { |
|
72 | 72 | continue; |
73 | 73 | } |
74 | 74 | |
75 | 75 | $fields[$key] = [ |
76 | 76 | 'description' => 'Error for ' . $key, |
77 | 77 | 'type' => $newType, |
78 | - 'resolve' => static function ($value) use ($key) { |
|
78 | + 'resolve' => static function($value) use ($key) { |
|
79 | 79 | return $value[$key] ?? null; |
80 | 80 | }, |
81 | 81 | ]; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | 'fields' => $fields, |
93 | 93 | ]), $config), |
94 | 94 | 'description' => 'Suberrors for ' . ucfirst($path[count($path)-1]), |
95 | - 'resolve' => static function (array $value) { |
|
95 | + 'resolve' => static function(array $value) { |
|
96 | 96 | return $value['suberrors'] ?? null; |
97 | 97 | }, |
98 | 98 | ]; |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | $finalFields['suberrors'] = [ |
117 | 117 | 'description' => 'Suberrors for the list of ' . $type->ofType . ' items', |
118 | 118 | 'type' => $newType, |
119 | - 'resolve' => static function ($value) { |
|
119 | + 'resolve' => static function($value) { |
|
120 | 120 | return $value['suberrors'] ?? null; |
121 | 121 | }, |
122 | 122 | ]; |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | $finalFields['path'] = [ |
131 | 131 | 'type' => Type::listOf(Type::int()), |
132 | 132 | 'description' => 'A path describing this items\'s location in the nested array', |
133 | - 'resolve' => static function ($value) { |
|
133 | + 'resolve' => static function($value) { |
|
134 | 134 | return $value['path']; |
135 | 135 | }, |
136 | 136 | ]; |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $config['fields']['code'] = $config['fields']['code'] ?? [ |
168 | 168 | 'type' => Type::int(), |
169 | 169 | 'description' => 'A numeric error code. 0 on success, non-zero on failure.', |
170 | - 'resolve' => static function ($value) { |
|
170 | + 'resolve' => static function($value) { |
|
171 | 171 | $error = $value['error'] ?? null; |
172 | 172 | switch (gettype($error)) { |
173 | 173 | case 'integer': |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | $config['fields']['msg'] = $config['fields']['msg'] ?? [ |
181 | 181 | 'type' => Type::string(), |
182 | 182 | 'description' => 'An error message.', |
183 | - 'resolve' => static function ($value) { |
|
183 | + 'resolve' => static function($value) { |
|
184 | 184 | $error = $value['error'] ?? null; |
185 | 185 | switch (gettype($error)) { |
186 | 186 | case 'integer': |
@@ -37,7 +37,7 @@ |
||
37 | 37 | { |
38 | 38 | $type = new UserErrorsType([ |
39 | 39 | 'errorCodes' => ['invalidColor'], |
40 | - 'validate' => static function ($value) { |
|
40 | + 'validate' => static function($value) { |
|
41 | 41 | return $value ? 0 : 1; |
42 | 42 | }, |
43 | 43 | 'type' => new IDType(['name' => 'Color']), |
@@ -45,9 +45,9 @@ discard block |
||
45 | 45 | |
46 | 46 | public function testFieldsWithErrorCodesButNoValidate() |
47 | 47 | { |
48 | - $this->expectExceptionMessage("If you specify errorCodes, you must also provide a validate callback"); |
|
48 | + $this->expectExceptionMessage("If you specify errorCodes, you must also provide a validate callback"); |
|
49 | 49 | |
50 | - new UserErrorsType([ |
|
50 | + new UserErrorsType([ |
|
51 | 51 | 'type' => new InputObjectType([ |
52 | 52 | 'name' => 'bookInput', |
53 | 53 | 'fields' => [ |
@@ -61,25 +61,25 @@ discard block |
||
61 | 61 | ], ['updateBook']); |
62 | 62 | } |
63 | 63 | |
64 | - public function testFieldsWithValidate() |
|
65 | - { |
|
66 | - $type = new UserErrorsType([ |
|
67 | - 'type' => new InputObjectType([ |
|
68 | - 'name' => 'bookInput', |
|
69 | - 'fields' => [ |
|
70 | - 'authorId' => [ |
|
71 | - 'errorCodes' => ['unknownAuthor'], |
|
72 | - 'validate' => static function(int $authorId) { |
|
73 | - return $authorId ? 0 : 1; |
|
74 | - }, |
|
75 | - 'type' => Type::id(), |
|
76 | - 'description' => 'An author Id', |
|
77 | - ], |
|
78 | - ], |
|
79 | - ]) |
|
80 | - ], ['updateBook']); |
|
64 | + public function testFieldsWithValidate() |
|
65 | + { |
|
66 | + $type = new UserErrorsType([ |
|
67 | + 'type' => new InputObjectType([ |
|
68 | + 'name' => 'bookInput', |
|
69 | + 'fields' => [ |
|
70 | + 'authorId' => [ |
|
71 | + 'errorCodes' => ['unknownAuthor'], |
|
72 | + 'validate' => static function(int $authorId) { |
|
73 | + return $authorId ? 0 : 1; |
|
74 | + }, |
|
75 | + 'type' => Type::id(), |
|
76 | + 'description' => 'An author Id', |
|
77 | + ], |
|
78 | + ], |
|
79 | + ]) |
|
80 | + ], ['updateBook']); |
|
81 | 81 | |
82 | - self::assertEquals(Utils::nowdoc(' |
|
82 | + self::assertEquals(Utils::nowdoc(' |
|
83 | 83 | schema { |
84 | 84 | query: UpdateBookError |
85 | 85 | } |
@@ -111,5 +111,5 @@ discard block |
||
111 | 111 | } |
112 | 112 | |
113 | 113 | '), SchemaPrinter::doPrint(new Schema(['query' => $type]))); |
114 | - } |
|
114 | + } |
|
115 | 115 | } |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | { |
18 | 18 | public function testFieldsWithNoErrorCodes() |
19 | 19 | { |
20 | - $type = new UserErrorsType([ |
|
20 | + $type = new UserErrorsType([ |
|
21 | 21 | 'type' => new InputObjectType([ |
22 | 22 | 'name' => 'bookInput', |
23 | 23 | 'fields' => [ |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | |
64 | 64 | public function testFieldsWithValidate() |
65 | 65 | { |
66 | - $type = new UserErrorsType([ |
|
66 | + $type = new UserErrorsType([ |
|
67 | 67 | 'type' => new InputObjectType([ |
68 | 68 | 'name' => 'bookInput', |
69 | 69 | 'fields' => [ |
@@ -100,13 +100,13 @@ discard block |
||
100 | 100 | 'args' => [ |
101 | 101 | 'bookAttributes' => [ |
102 | 102 | 'type' => Type::listOf($this->bookAttributesInputType), |
103 | - 'validate' => static function($var) { |
|
104 | - return $var ? 0: 1; |
|
105 | - }, |
|
106 | - 'validateItem' => static function($book) { |
|
107 | - $res = isset($book['author']) || isset($book['title']) ? 0: 1; |
|
108 | - return $res; |
|
109 | - } |
|
103 | + 'validate' => static function($var) { |
|
104 | + return $var ? 0: 1; |
|
105 | + }, |
|
106 | + 'validateItem' => static function($book) { |
|
107 | + $res = isset($book['author']) || isset($book['title']) ? 0: 1; |
|
108 | + return $res; |
|
109 | + } |
|
110 | 110 | ], |
111 | 111 | ], |
112 | 112 | 'resolve' => static function ($value) : bool { |
@@ -162,30 +162,30 @@ discard block |
||
162 | 162 | ] |
163 | 163 | ); |
164 | 164 | |
165 | - static::assertEmpty($res->errors); |
|
165 | + static::assertEmpty($res->errors); |
|
166 | 166 | |
167 | 167 | static::assertEquals( |
168 | - array ( |
|
169 | - 'valid' => false, |
|
170 | - 'result' => NULL, |
|
171 | - 'suberrors' => |
|
172 | - array ( |
|
173 | - 'bookAttributes' => |
|
174 | - array ( |
|
175 | - 'code' => NULL, |
|
176 | - 'msg' => NULL, |
|
177 | - 'suberrors' => |
|
178 | - array ( |
|
179 | - 'code' => 1, |
|
180 | - 'msg' => '', |
|
181 | - 'path' => |
|
182 | - array ( |
|
183 | - 0 => 1, |
|
184 | - ), |
|
185 | - ), |
|
186 | - ), |
|
187 | - ), |
|
188 | - ), |
|
168 | + array ( |
|
169 | + 'valid' => false, |
|
170 | + 'result' => NULL, |
|
171 | + 'suberrors' => |
|
172 | + array ( |
|
173 | + 'bookAttributes' => |
|
174 | + array ( |
|
175 | + 'code' => NULL, |
|
176 | + 'msg' => NULL, |
|
177 | + 'suberrors' => |
|
178 | + array ( |
|
179 | + 'code' => 1, |
|
180 | + 'msg' => '', |
|
181 | + 'path' => |
|
182 | + array ( |
|
183 | + 0 => 1, |
|
184 | + ), |
|
185 | + ), |
|
186 | + ), |
|
187 | + ), |
|
188 | + ), |
|
189 | 189 | $res->data['updateBooks'] |
190 | 190 | ); |
191 | 191 |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | 'title' => [ |
63 | 63 | 'type' => Type::string(), |
64 | 64 | 'description' => 'Enter a book title, no more than 10 characters in length', |
65 | - 'validate' => static function (string $title) { |
|
65 | + 'validate' => static function(string $title) { |
|
66 | 66 | if (strlen($title) > 10) { |
67 | 67 | return [1, 'book title must be less than 10 chaacters']; |
68 | 68 | } |
@@ -76,8 +76,8 @@ discard block |
||
76 | 76 | 'unknownAuthor', |
77 | 77 | 'authorDeceased', |
78 | 78 | ], |
79 | - 'validate' => function (string $authorId) { |
|
80 | - if (! isset($this->data['people'][$authorId])) { |
|
79 | + 'validate' => function(string $authorId) { |
|
80 | + if (!isset($this->data['people'][$authorId])) { |
|
81 | 81 | return ['unknownAuthor', 'We have no record of that author']; |
82 | 82 | } |
83 | 83 | return 0; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | 'query' => $this->query, |
93 | 93 | 'mutation' => new ObjectType([ |
94 | 94 | 'name' => 'Mutation', |
95 | - 'fields' => function () { |
|
95 | + 'fields' => function() { |
|
96 | 96 | return [ |
97 | 97 | 'updateBooks' => new ValidatedFieldDefinition([ |
98 | 98 | 'name' => 'updateBooks', |
@@ -101,15 +101,15 @@ discard block |
||
101 | 101 | 'bookAttributes' => [ |
102 | 102 | 'type' => Type::listOf($this->bookAttributesInputType), |
103 | 103 | 'validate' => static function($var) { |
104 | - return $var ? 0: 1; |
|
104 | + return $var ? 0 : 1; |
|
105 | 105 | }, |
106 | 106 | 'validateItem' => static function($book) { |
107 | - $res = isset($book['author']) || isset($book['title']) ? 0: 1; |
|
107 | + $res = isset($book['author']) || isset($book['title']) ? 0 : 1; |
|
108 | 108 | return $res; |
109 | 109 | } |
110 | 110 | ], |
111 | 111 | ], |
112 | - 'resolve' => static function ($value) : bool { |
|
112 | + 'resolve' => static function($value) : bool { |
|
113 | 113 | return !!$value; |
114 | 114 | }, |
115 | 115 | ]), |
@@ -165,21 +165,21 @@ discard block |
||
165 | 165 | static::assertEmpty($res->errors); |
166 | 166 | |
167 | 167 | static::assertEquals( |
168 | - array ( |
|
168 | + array( |
|
169 | 169 | 'valid' => false, |
170 | 170 | 'result' => NULL, |
171 | 171 | 'suberrors' => |
172 | - array ( |
|
172 | + array( |
|
173 | 173 | 'bookAttributes' => |
174 | - array ( |
|
174 | + array( |
|
175 | 175 | 'code' => NULL, |
176 | 176 | 'msg' => NULL, |
177 | 177 | 'suberrors' => |
178 | - array ( |
|
178 | + array( |
|
179 | 179 | 'code' => 1, |
180 | 180 | 'msg' => '', |
181 | 181 | 'path' => |
182 | - array ( |
|
182 | + array( |
|
183 | 183 | 0 => 1, |
184 | 184 | ), |
185 | 185 | ), |
@@ -9,46 +9,46 @@ |
||
9 | 9 | |
10 | 10 | final class BasicTest extends TestCase |
11 | 11 | { |
12 | - public function testInferName() |
|
13 | - { |
|
14 | - $def = new NamelessDef([ |
|
15 | - 'type' => Type::boolean(), |
|
16 | - 'args' => [ |
|
17 | - 'bookId' => [ |
|
18 | - 'type' => Type::id(), |
|
19 | - 'errorCodes' => ['bookNotFound'], |
|
20 | - 'validate' => function ($bookId) { |
|
21 | - return $bookId ? 0 : 1; |
|
22 | - }, |
|
23 | - ], |
|
24 | - ], |
|
25 | - 'resolve' => static function ($value) : bool { |
|
26 | - return !!$value; |
|
27 | - }, |
|
28 | - ]); |
|
12 | + public function testInferName() |
|
13 | + { |
|
14 | + $def = new NamelessDef([ |
|
15 | + 'type' => Type::boolean(), |
|
16 | + 'args' => [ |
|
17 | + 'bookId' => [ |
|
18 | + 'type' => Type::id(), |
|
19 | + 'errorCodes' => ['bookNotFound'], |
|
20 | + 'validate' => function ($bookId) { |
|
21 | + return $bookId ? 0 : 1; |
|
22 | + }, |
|
23 | + ], |
|
24 | + ], |
|
25 | + 'resolve' => static function ($value) : bool { |
|
26 | + return !!$value; |
|
27 | + }, |
|
28 | + ]); |
|
29 | 29 | |
30 | - static::assertEquals("namelessDef", $def->name); |
|
31 | - } |
|
30 | + static::assertEquals("namelessDef", $def->name); |
|
31 | + } |
|
32 | 32 | |
33 | - public function testInferNameNameAlreadyExists() |
|
34 | - { |
|
35 | - $def = new NamelessDef([ |
|
36 | - 'name' => "FooDef", |
|
37 | - 'type' => Type::boolean(), |
|
38 | - 'args' => [ |
|
39 | - 'bookId' => [ |
|
40 | - 'type' => Type::id(), |
|
41 | - 'errorCodes' => ['bookNotFound'], |
|
42 | - 'validate' => function ($bookId) { |
|
43 | - return $bookId ? 0 : 1; |
|
44 | - }, |
|
45 | - ], |
|
46 | - ], |
|
47 | - 'resolve' => static function ($value) : bool { |
|
48 | - return !!$value; |
|
49 | - }, |
|
50 | - ]); |
|
33 | + public function testInferNameNameAlreadyExists() |
|
34 | + { |
|
35 | + $def = new NamelessDef([ |
|
36 | + 'name' => "FooDef", |
|
37 | + 'type' => Type::boolean(), |
|
38 | + 'args' => [ |
|
39 | + 'bookId' => [ |
|
40 | + 'type' => Type::id(), |
|
41 | + 'errorCodes' => ['bookNotFound'], |
|
42 | + 'validate' => function ($bookId) { |
|
43 | + return $bookId ? 0 : 1; |
|
44 | + }, |
|
45 | + ], |
|
46 | + ], |
|
47 | + 'resolve' => static function ($value) : bool { |
|
48 | + return !!$value; |
|
49 | + }, |
|
50 | + ]); |
|
51 | 51 | |
52 | - static::assertEquals("FooDef", $def->name); |
|
53 | - } |
|
52 | + static::assertEquals("FooDef", $def->name); |
|
53 | + } |
|
54 | 54 | } |
@@ -17,12 +17,12 @@ discard block |
||
17 | 17 | 'bookId' => [ |
18 | 18 | 'type' => Type::id(), |
19 | 19 | 'errorCodes' => ['bookNotFound'], |
20 | - 'validate' => function ($bookId) { |
|
20 | + 'validate' => function($bookId) { |
|
21 | 21 | return $bookId ? 0 : 1; |
22 | 22 | }, |
23 | 23 | ], |
24 | 24 | ], |
25 | - 'resolve' => static function ($value) : bool { |
|
25 | + 'resolve' => static function($value) : bool { |
|
26 | 26 | return !!$value; |
27 | 27 | }, |
28 | 28 | ]); |
@@ -39,12 +39,12 @@ discard block |
||
39 | 39 | 'bookId' => [ |
40 | 40 | 'type' => Type::id(), |
41 | 41 | 'errorCodes' => ['bookNotFound'], |
42 | - 'validate' => function ($bookId) { |
|
42 | + 'validate' => function($bookId) { |
|
43 | 43 | return $bookId ? 0 : 1; |
44 | 44 | }, |
45 | 45 | ], |
46 | 46 | ], |
47 | - 'resolve' => static function ($value) : bool { |
|
47 | + 'resolve' => static function($value) : bool { |
|
48 | 48 | return !!$value; |
49 | 49 | }, |
50 | 50 | ]); |