API Platform – GraphQL Schema mapping nullable Collection

API Platform – GraphQL Schema mapping nullable Collection


0

I’m pretty new to GraphQL, I’m building an API and a React App that connects to it.
I need some help defining my GraphQL schema, I can’t find out what I’m doing wrong, but my GraphQL schema keeps bringing a mapped Collection as nullable…

I’ve been following the official docs (https://api-platform.com/docs/core/graphql/) but can’t find out anything related to my problem, can someone lend me a hand?

Currently, I have an Entity named Author that has a OneToMany relation to the Entity Book:

Author.php

#[ORMOneToMany(mappedBy: 'author', targetEntity: Book::class, orphanRemoval: true)]
private Collection $books;

public function __construct()
{ 
     $this->books = new ArrayCollection();
}

/**
 * @return Collection<int, Book>
 */
public function getBooks(): Collection
{
     return $this->books;
}

Book.php

#[ORMManyToOne(inversedBy: 'books')]
#[ORMJoinColumn(nullable: false)]
private ?Author $author= null;

However, while the Book type comes out fine, with a required Author:

type Book implements Node {
  author: Author!
}

The Author type brings the Book collection as nullable:

type Author implements Node {
  books: [Book]
}

For it to be required, it should’ve been [Book!]!, no?

Edit: I forgot to add, I’ve disabled pagination, and I’m not using a custom resolver:

Author.php

#[ApiResource(graphQlOperations: [
    new QueryCollection(),
])]

New contributor

luskita is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *