|
1 | 1 | /*
|
2 |
| - * Copyright 2020-2023 the original author or authors. |
| 2 | + * Copyright 2020-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
59 | 59 | import org.springframework.graphql.Author;
|
60 | 60 | import org.springframework.graphql.Book;
|
61 | 61 | import org.springframework.graphql.data.ArgumentValue;
|
| 62 | +import org.springframework.graphql.data.federation.EntityMapping; |
62 | 63 | import org.springframework.graphql.data.method.annotation.Argument;
|
63 | 64 | import org.springframework.graphql.data.method.annotation.BatchMapping;
|
64 | 65 | import org.springframework.graphql.data.method.annotation.ContextValue;
|
@@ -451,6 +452,113 @@ public GraphQLError handleBindException(BindException exc) {
|
451 | 452 | }
|
452 | 453 | }
|
453 | 454 |
|
| 455 | + @Nested |
| 456 | + class EntityMappingTests { |
| 457 | + |
| 458 | + @Test |
| 459 | + void registerBindingReflectionOnReturnType() { |
| 460 | + processBeanClasses(ReturnTypeController.class); |
| 461 | + assertThatIntrospectionOnMethodsHintRegisteredForType(ReturnTypeController.class); |
| 462 | + assertThatInvocationHintRegisteredForMethods(ReturnTypeController.class, "bookById"); |
| 463 | + assertThatHintsForJavaBeanBindingRegisteredForTypes(Book.class); |
| 464 | + } |
| 465 | + |
| 466 | + @Test |
| 467 | + void registerBindingReflectionOnOnNamedValue() { |
| 468 | + processBeanClasses(NamedValueController.class); |
| 469 | + assertThatIntrospectionOnMethodsHintRegisteredForType(NamedValueController.class); |
| 470 | + assertThatInvocationHintRegisteredForMethods(NamedValueController.class, "book"); |
| 471 | + assertThatHintsForJavaBeanBindingRegisteredForTypes(Book.class, Identifier.class); |
| 472 | + } |
| 473 | + |
| 474 | + @Test |
| 475 | + void registerBindingReflectionOnOnNamedValues() { |
| 476 | + processBeanClasses(NamedValuesController.class); |
| 477 | + assertThatIntrospectionOnMethodsHintRegisteredForType(NamedValuesController.class); |
| 478 | + assertThatInvocationHintRegisteredForMethods(NamedValuesController.class, "books"); |
| 479 | + assertThatHintsForJavaBeanBindingRegisteredForTypes(Book.class, Identifier.class); |
| 480 | + } |
| 481 | + |
| 482 | + @Test |
| 483 | + void registerBindingReflectionOnAsyncReturnType() { |
| 484 | + processBeanClasses(AsyncReturnTypeController.class); |
| 485 | + assertThatIntrospectionOnMethodsHintRegisteredForType(AsyncReturnTypeController.class); |
| 486 | + assertThatInvocationHintRegisteredForMethods(AsyncReturnTypeController.class, "author"); |
| 487 | + assertThatHintsForJavaBeanBindingRegisteredForTypes(Author.class); |
| 488 | + } |
| 489 | + |
| 490 | + @Test |
| 491 | + void doNotRegisterBindingForContextArguments() { |
| 492 | + processBeanClasses(ContextArgumentsController.class); |
| 493 | + assertThatIntrospectionOnMethodsHintRegisteredForType(ContextArgumentsController.class); |
| 494 | + assertThatInvocationHintRegisteredForMethods(ContextArgumentsController.class, "dataFetchingEnvironment"); |
| 495 | + assertThatHintsAreNotRegisteredForTypes(GraphQLContext.class, DataFetchingFieldSelectionSet.class, Locale.class); |
| 496 | + } |
| 497 | + |
| 498 | + @Test |
| 499 | + void doNotRegisterBindingForAnnotatedContextArguments() { |
| 500 | + processBeanClasses(AnnotatedContextArgumentController.class); |
| 501 | + assertThatIntrospectionOnMethodsHintRegisteredForType(AnnotatedContextArgumentController.class); |
| 502 | + assertThatInvocationHintRegisteredForMethods(AnnotatedContextArgumentController.class, "contextValue"); |
| 503 | + assertThatHintsAreNotRegisteredForTypes(Book.class); |
| 504 | + } |
| 505 | + |
| 506 | + |
| 507 | + @Controller |
| 508 | + static class ReturnTypeController { |
| 509 | + @EntityMapping |
| 510 | + public Book bookById(@Argument Long id) { |
| 511 | + return null; |
| 512 | + } |
| 513 | + |
| 514 | + } |
| 515 | + |
| 516 | + @Controller |
| 517 | + static class NamedValueController { |
| 518 | + @EntityMapping |
| 519 | + public Book book(@Argument Identifier id) { |
| 520 | + return null; |
| 521 | + } |
| 522 | + |
| 523 | + } |
| 524 | + |
| 525 | + @Controller |
| 526 | + static class NamedValuesController { |
| 527 | + @EntityMapping |
| 528 | + public List<Book> books(@Argument List<Identifier> idList) { |
| 529 | + return null; |
| 530 | + } |
| 531 | + |
| 532 | + } |
| 533 | + |
| 534 | + @Controller |
| 535 | + static class AsyncReturnTypeController { |
| 536 | + @EntityMapping |
| 537 | + public CompletableFuture<Author> author(Long bookId) { |
| 538 | + return null; |
| 539 | + } |
| 540 | + |
| 541 | + } |
| 542 | + |
| 543 | + @Controller |
| 544 | + static class ContextArgumentsController { |
| 545 | + @EntityMapping |
| 546 | + public void dataFetchingEnvironment(GraphQLContext context, DataFetchingFieldSelectionSet selectionSet, Locale locale) { |
| 547 | + } |
| 548 | + } |
| 549 | + |
| 550 | + @Controller |
| 551 | + class AnnotatedContextArgumentController { |
| 552 | + @EntityMapping |
| 553 | + public void contextValue(@ContextValue Book book, @LocalContextValue Book localBook) { |
| 554 | + } |
| 555 | + } |
| 556 | + |
| 557 | + record Identifier(String id) { |
| 558 | + |
| 559 | + } |
| 560 | + } |
| 561 | + |
454 | 562 |
|
455 | 563 | private void processBeanClasses(Class<?>... beanClasses) {
|
456 | 564 | DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
|
0 commit comments