Skip to content

Commit 6b3e908

Browse files
authored
Remove undefined type. (#524)
1 parent 0334d28 commit 6b3e908

17 files changed

Lines changed: 152 additions & 56 deletions
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
<?php
22

3-
namespace Drupal\graphql\GraphQL\Type;
3+
namespace Drupal\graphql\GraphQL\Type\Scalars;
44

5+
use Symfony\Component\HttpFoundation\File\UploadedFile;
56
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
67

7-
class UndefinedType extends AbstractScalarType {
8+
class AnyType extends AbstractScalarType {
89

910
/**
1011
* {@inheritdoc}
1112
*/
1213
public function getName() {
13-
return 'Undefined';
14+
return 'Any';
1415
}
1516

1617
/**
1718
* {@inheritdoc}
1819
*/
1920
public function serialize($value) {
20-
if (is_bool($value) || is_null($value) || is_scalar($value)) {
21+
if (is_scalar($value)) {
2122
return $value;
2223
}
2324

24-
if (is_object($value) && method_exists($value, '__toString')) {
25-
return (string) $value;
25+
if (is_array($value)) {
26+
return json_encode($value);
2627
}
2728

28-
if (is_array($value) || is_object($value)) {
29-
return json_encode($value);
29+
if (is_object($value) && method_exists($value, '__toString')) {
30+
return (string) $value;
3031
}
3132

3233
return '';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Drupal\graphql\GraphQL\Type\Scalars;
4+
5+
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;
6+
7+
class MapType extends AbstractScalarType {
8+
9+
/**
10+
* {@inheritdoc}
11+
*/
12+
public function getName() {
13+
return 'Map';
14+
}
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function serialize($value) {
20+
}
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function isValidValue($value) {
26+
return is_array($value);
27+
}
28+
29+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Drupal\graphql\GraphQL\Type;
3+
namespace Drupal\graphql\GraphQL\Type\Scalars;
44

55
use Symfony\Component\HttpFoundation\File\UploadedFile;
66
use Youshido\GraphQL\Type\Scalar\AbstractScalarType;

src/GraphQL/Utility/TypeCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static function collectTypes(AbstractSchema $schema) {
4646
* @param \Youshido\GraphQL\Type\TypeInterface[] $types
4747
* The type map to write to.
4848
*/
49-
protected static function doCollectTypes(TypeInterface $type, array &$types = []) {
49+
protected static function doCollectTypes($type, array &$types = []) {
5050
if (is_object($type) && array_key_exists($type->getName(), $types)) {
5151
return;
5252
}

src/Plugin/GraphQL/Enums/EnumPluginBase.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ abstract class EnumPluginBase extends PluginBase implements TypeSystemPluginInte
2828
*/
2929
public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
3030
if (!isset($this->definition)) {
31-
$this->definition = new EnumType($this, $schemaBuilder, [
32-
'name' => $this->buildName(),
33-
'description' => $this->buildDescription(),
34-
'values' => $this->buildValues($schemaBuilder),
35-
]);
31+
if ($values = $this->buildValues($schemaBuilder)) {
32+
$this->definition = new EnumType($this, $schemaBuilder, [
33+
'name' => $this->buildName(),
34+
'description' => $this->buildDescription(),
35+
'values' => $values,
36+
]);
37+
}
3638
}
3739

3840
return $this->definition;

src/Plugin/GraphQL/Fields/FieldPluginBase.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,19 @@ public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
4040
if (!isset($this->definition)) {
4141
$definition = $this->getPluginDefinition();
4242

43-
$config = [
44-
'name' => $this->buildName(),
45-
'description' => $this->buildDescription(),
46-
'type' => $this->buildType($schemaBuilder),
47-
'args' => $this->buildArguments($schemaBuilder),
48-
'isDeprecated' => !empty($definition['deprecated']),
49-
'deprecationReason' => !empty($definition['deprecated']) ? !empty($definition['deprecated']) : '',
50-
];
51-
52-
$this->definition = new Field($this, $schemaBuilder, $config);
43+
if ($type = $this->buildType($schemaBuilder)) {
44+
$this->definition = new Field($this, $schemaBuilder, [
45+
'name' => $this->buildName(),
46+
'description' => $this->buildDescription(),
47+
'type' => $type,
48+
'isDeprecated' => !empty($definition['deprecated']),
49+
'deprecationReason' => !empty($definition['deprecated']) ? !empty($definition['deprecated']) : '',
50+
]);
51+
52+
if ($args = $this->buildArguments($schemaBuilder)) {
53+
$this->definition->addArguments($args);
54+
}
55+
}
5356
}
5457

5558
return $this->definition;

src/Plugin/GraphQL/Mutations/MutationPluginBase.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
3333
if (!isset($this->definition)) {
3434
$definition = $this->getPluginDefinition();
3535

36-
$this->definition = new Field($this, $schemaBuilder, [
37-
'name' => $this->buildName(),
38-
'description' => $this->buildDescription(),
39-
'type' => $this->buildType($schemaBuilder),
40-
'args' => $this->buildArguments($schemaBuilder),
41-
'isDeprecated' => !empty($definition['deprecated']),
42-
'deprecationReason' => !empty($definition['deprecated']) ? !empty($definition['deprecated']) : '',
43-
]);
36+
if ($type = $this->buildType($schemaBuilder)) {
37+
$this->definition = new Field($this, $schemaBuilder, [
38+
'name' => $this->buildName(),
39+
'description' => $this->buildDescription(),
40+
'type' => $type,
41+
'isDeprecated' => !empty($definition['deprecated']),
42+
'deprecationReason' => !empty($definition['deprecated']) ? !empty($definition['deprecated']) : '',
43+
]);
44+
45+
if ($args = $this->buildArguments($schemaBuilder)) {
46+
$this->definition->addArguments($args);
47+
}
48+
}
4449
}
4550

4651
return $this->definition;

src/Plugin/GraphQL/PluggableSchemaBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function findByDataTypeOrName($input, array $types) {
149149
return $type;
150150
}
151151

152-
return $this->getInstance('scalar', 'undefined');
152+
return NULL;
153153
}
154154

155155
/**

src/Plugin/GraphQL/Scalars/GraphQLUpload.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\graphql\Plugin\GraphQL\Scalars;
44

5-
use Drupal\graphql\GraphQL\Type\UploadType;
5+
use Drupal\graphql\GraphQL\Type\Scalars\UploadType;
66
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
77

88
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Drupal\graphql\Plugin\GraphQL\Scalars\TypedData;
4+
5+
use Drupal\graphql\GraphQL\Type\Scalars\AnyType;
6+
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaBuilderInterface;
7+
use Drupal\graphql\Plugin\GraphQL\Scalars\GraphQLString;
8+
9+
/**
10+
* @GraphQLScalar(
11+
* id = "any",
12+
* name = "Any",
13+
* type = "any"
14+
* )
15+
*/
16+
class Any extends GraphQLString {
17+
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function getDefinition(PluggableSchemaBuilderInterface $schemaBuilder) {
22+
if (!isset($this->definition)) {
23+
$this->definition = new AnyType();
24+
}
25+
26+
return $this->definition;
27+
}
28+
}

0 commit comments

Comments
 (0)