Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you return null from a @BatchMapping? #1136

Open
JBodkin-Amphora opened this issue Feb 24, 2025 · 1 comment
Open

How do you return null from a @BatchMapping? #1136

JBodkin-Amphora opened this issue Feb 24, 2025 · 1 comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged

Comments

@JBodkin-Amphora
Copy link

I've been trying to implement a controller which has a BatchMapping with a null value in the list being returned. Behind the scenes, this library is using reactive streams (flux/mono) which doesn't allow you to return a stream that has a null value.

What is the best way to return an optional relationship in the schema?

If you add the following test to the BatchLoadingTests file, you can reproduce the issue.

	@Test
	void batchLoaderWithNullValue() {
		String document = "{ " +
			"  booksByCriteria(criteria: {author:\"Orwell\"}) { " +
			"    author {" +
			"      firstName, " +
			"      lastName " +
			"    }" +
			"  }" +
			"}";

		this.registry.forTypePair(Long.class, Author.class)
			.registerBatchLoader((ids, env) -> Flux.fromIterable(ids.stream().<Author>map(id -> null).toList()));

		TestExecutionGraphQlService service = GraphQlSetup.schemaResource(BookSource.schema)
			.queryFetcher("booksByCriteria", env -> {
				Map<String, Object> criteria = env.getArgument("criteria");
				String authorName = (String) criteria.get("author");
				return BookSource.findBooksByAuthor(authorName).stream()
					.map(book -> new Book(book.getId(), book.getName(), book.getAuthorId()))
					.collect(Collectors.toList());
			})
			.dataFetcher("Book", "author", env -> {
				Book book = env.getSource();
				DataLoader<Long, Author> dataLoader = env.getDataLoader(Author.class.getName());
				return dataLoader.load(book.getAuthorId());
			})
			.dataLoaders(this.registry)
			.toGraphQlService();

		Mono<ExecutionGraphQlResponse> responseMono = service.execute(document);

		List<Book> books = ResponseHelper.forResponse(responseMono).toList("booksByCriteria", Book.class);
		assertThat(books).hasSize(2);

		Author author = books.get(0).getAuthor();
		assertThat(author).isNull();
	}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 24, 2025
@rstoyanchev
Copy link
Contributor

Indeed, a stream cannot have a null value, and therefore returning a List doesn't work. You can return a Map with null values..

@bclozel bclozel added the status: waiting-for-feedback We need additional information before we can continue label Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants