Skip to content

Commit 8aee46e

Browse files
committed
Lists always have non-null elements
Because in the vast majority of cases a list of something do not allow null. This is especially true for Doctrine collection of entities returned by getter, but also for most inputs used in setter. If a list of mixed null and non-null elements is needed then a custom type should be defined via annotation.
1 parent ba64605 commit 8aee46e

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

src/Factory/AbstractFieldsConfigurationFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ final protected function getTypeFromPhpDeclaration(ReflectionMethod $method, ?st
140140
$type = $this->getTypeFromRegistry($name, $isEntityId);
141141

142142
if ($isList) {
143-
$type = Type::listOf($type);
143+
$type = Type::listOf(Type::nonNull($type));
144144
}
145145

146146
if (!$isNullable) {
@@ -200,7 +200,7 @@ final protected function getTypeFromReturnTypeHint(ReflectionMethod $method, str
200200
throw new Exception('The method ' . $this->getMethodFullName($method) . ' is type hinted with a return type of `' . $returnTypeName . '`, but the entity contained in that collection could not be automatically detected. Either fix the type hint, fix the doctrine mapping, or specify the type with `@API\Field` annotation.');
201201
}
202202

203-
$type = Type::listOf($this->getTypeFromRegistry($targetEntity, false));
203+
$type = Type::listOf(Type::nonNull($this->getTypeFromRegistry($targetEntity, false)));
204204
if (!$returnType->allowsNull()) {
205205
$type = Type::nonNull($type);
206206
}

tests/data/ArrayOfEntity.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type ArrayOfEntity {
2-
users: [User]!
2+
users: [User!]!
33
id: ID!
44
creationDate: DateTime!
55
}

tests/data/IgnoredGetter.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type IgnoredGetter {
22
public: String!
3-
publicWithArgs(arg1: String!, arg2: Int!, arg3: [String] = ["foo"]): [String]!
3+
publicWithArgs(arg1: String!, arg2: Int!, arg3: [String!] = ["foo"]): [String!]!
44
isValid: Boolean!
55
hasMoney: Boolean!
66
id: ID!

tests/data/PostOutput.graphqls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ type Post {
1414

1515
"""Date of publication"""
1616
publicationDate: DateTime!
17-
words: [String]!
18-
hasWords(words: [String]!): Boolean!
17+
words: [String!]!
18+
hasWords(words: [String!]!): Boolean!
1919
isLong(wordLimit: Int = 50): Boolean!
2020
isAllowedEditing(user: UserID!): Boolean!
2121
id: ID!

tests/data/UserOutput.graphqls

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type User {
1515
The status of posts as defined in \GraphQLTests\Doctrine\Blog\Model\Post
1616
"""
1717
status: PostStatus = public
18-
): [Post]!
19-
postsWithIds(ids: [ID]!): [Post]!
18+
): [Post!]!
19+
postsWithIds(ids: [ID!]!): [Post!]!
2020
manager: User
2121
id: ID!
2222
creationDate: DateTime!

0 commit comments

Comments
 (0)