Skip to content

Commit ae6e84e

Browse files
authored
Merge pull request #13 from mcg-web/fix-default-value
Fix input and args defaultValue
2 parents 05df8b0 + 82ae839 commit ae6e84e

11 files changed

+37
-24
lines changed

.styleci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ enabled:
55

66
disabled:
77
- unalign_equals
8+
- psr0

src/Generator/AbstractTypeGenerator.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,17 @@ protected function varExport($var, $default = null, array $compilerNames = [])
151151
// handle multi-line strings
152152
$lines = explode("\n", $string);
153153
if (count($lines) > 1) {
154-
$firstLine = array_shift($lines) . "'" . ' . "\n"';
155-
$lastLine = "'" . array_pop($lines);
156-
$lines = array_map(function($s) { return "'" . $s . "'" . ' . "\n"'; }, $lines);
154+
$firstLine = sprintf('%s\' . "\n"', array_shift($lines));
155+
$lastLine = sprintf("'%s", array_pop($lines));
156+
$lines = array_map(
157+
function ($line) {
158+
return sprintf('\'%s\' . "\n"', $line);
159+
},
160+
$lines
161+
);
157162
array_unshift($lines, $firstLine);
158163
array_push($lines, $lastLine);
159-
$string = implode(" . \n", $lines);
164+
$string = implode(' . ', $lines);
160165
}
161166

162167
return $string;

src/Generator/TypeGenerator.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ protected function generateDeprecationReason(array $value)
5555

5656
protected function generateDefaultValue(array $value)
5757
{
58-
return $this->varExportFromArrayValue($value, 'defaultValue');
58+
$key = 'defaultValue';
59+
if (!array_key_exists($key, $value)) {
60+
return '';
61+
}
62+
63+
return sprintf("\n<spaces>'%s' => %s,", $key, $this->varExportFromArrayValue($value, $key));
5964
}
6065

6166
protected function generateType(array $value)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
[
22
<spaces>'name' => <name>,
33
<spaces>'type' => <type>,
4-
<spaces>'description' => <description>,
5-
<spaces>'defaultValue' => <defaultValue>,
4+
<spaces>'description' => <description>,<defaultValue>
65
],
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<name> => [
22
<spaces>'type' => <type>,
3-
<spaces>'defaultValue' => <defaultValue>,
4-
<spaces>'description' => <description>,
3+
<spaces>'description' => <description>,<defaultValue>
54
],

tests/AbstractStarWarsTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function setUp()
4444
* Helper function to test a query and the expected response.
4545
* @param $query
4646
* @param $expected
47-
* @param null $params
47+
* @param null $variables
4848
*/
49-
protected function assertValidQuery($query, $expected, $params = null)
49+
protected function assertValidQuery($query, $expected, $variables = null)
5050
{
51-
$result = GraphQL::execute($this->schema, $query, null, null, $params);
51+
$result = GraphQL::execute($this->schema, $query, null, null, $variables);
5252

5353
$this->assertEquals(['data' => $expected], $result, json_encode($result));
5454
}

tests/Resolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static function getFriends($droidOrHuman)
7272

7373
public static function getHero($root, $args)
7474
{
75-
return StarWarsData::getHero(isset($args['episode']) ? $args['episode'] : null);
75+
return StarWarsData::getHero(isset($args['episode']['name']) ? $args['episode']['name'] : null);
7676
}
7777

7878
public static function getHuman($root, $args)

tests/StarWarsIntrospectionTest.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function testAllowsQueryingTheSchemaForTypes()
3131
'__schema' => [
3232
'types' => [
3333
['name' => 'Query'],
34+
['name' => 'HeroInput'],
35+
['name' => 'ID'],
3436
['name' => 'Episode'],
3537
['name' => 'Character'],
3638
['name' => 'String'],
@@ -46,7 +48,6 @@ public function testAllowsQueryingTheSchemaForTypes()
4648
['name' => '__EnumValue'],
4749
['name' => '__Directive'],
4850
['name' => '__DirectiveLocation'],
49-
['name' => 'ID'],
5051
['name' => 'Float'],
5152
['name' => 'Int'],
5253
]
@@ -311,12 +312,12 @@ public function testAllowsQueryingTheSchemaForFieldArgs()
311312
'name' => 'hero',
312313
'args' => [
313314
[
314-
'defaultValue' => 'null',
315+
'defaultValue' => null,
315316
'description' => "If omitted, returns the hero of the whole saga.\nIf provided, returns the hero of that particular episode.\n",
316317
'name' => 'episode',
317318
'type' => [
318-
'kind' => 'ENUM',
319-
'name' => 'Episode',
319+
'kind' => 'INPUT_OBJECT',
320+
'name' => 'HeroInput',
320321
'ofType' => null,
321322
],
322323
],
@@ -370,7 +371,7 @@ public function testAllowsQueryingTheSchemaForFieldArgs()
370371
'kind' => 'SCALAR',
371372
'ofType' => null,
372373
],
373-
'defaultValue' => 'null',
374+
'defaultValue' => null,
374375
]
375376
],
376377
],

tests/StarWarsQueryTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ public function testVerifyThatR2D2IsADroid()
336336
public function testVerifyThatLukeIsHuman()
337337
{
338338
$query = '
339-
query CheckTypeOfLuke {
340-
hero(episode: EMPIRE) {
339+
query CheckTypeOfLuke($episode: HeroInput!) {
340+
hero(episode: $episode) {
341341
__typename
342342
name
343343
}
@@ -349,7 +349,7 @@ public function testVerifyThatLukeIsHuman()
349349
'name' => 'Luke Skywalker'
350350
],
351351
];
352-
$this->assertValidQuery($query, $expected);
352+
$this->assertValidQuery($query, $expected, ['episode' => ['name' => 'EMPIRE']]);
353353
}
354354

355355
public function testDateTime()

tests/TestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function assertCodeStandard($pathToCode, $level = null, $fixers = null)
1919
{
2020
// Run linter in dry-run mode so it changes nothing.
2121
$csBuilder = new ProcessBuilder([
22-
'php-cs-fixer',
22+
__DIR__ . '/../bin/php-cs-fixer',
2323
'fix',
2424
'--dry-run',
2525
'--diff',

tests/starWarsSchema.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Query:
142142
type: "Character"
143143
args:
144144
episode:
145-
type: "Episode"
145+
type: "HeroInput"
146146
description: |
147147
If omitted, returns the hero of the whole saga.
148148
If provided, returns the hero of that particular episode.
@@ -178,5 +178,8 @@ HeroInput:
178178
type: input-object
179179
config:
180180
fields:
181+
id:
182+
type: "ID"
181183
name:
182-
type: "Episode!"
184+
type: "Episode"
185+
defaultValue: 4

0 commit comments

Comments
 (0)