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

Query by Example doesn't have a binding customization mechanism #1083

Open
danvega opened this issue Nov 7, 2024 · 1 comment
Open

Query by Example doesn't have a binding customization mechanism #1083

danvega opened this issue Nov 7, 2024 · 1 comment
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@danvega
Copy link

danvega commented Nov 7, 2024

I have a GraphQLRepository that extends QueryByExampleExectutor

@GraphQlRepository
public interface BookRepository extends JpaRepository<Book, Long>, QueryByExampleExecutor<Book> {}

If I try to do a partial match on the book title no results come back. If I enter the full book title the results come back as expected.

// Find books by partial title match
query {
  books(book: { title: "Spring Boot" }) {
    id
    title
    author
    publishedYear
  }
}

I realize I can write a data fetcher for this but this:

@Controller
public class BookController {

    private final BookRepository repository;

    public BookController(BookRepository repository) {
        this.repository = repository;
    }

    @QueryMapping
    public List<Book> booksContaining(@Argument(name = "book") Book filterBook) {
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase()
                .withIgnoreNullValues();

        Example<Book> example = Example.of(filterBook, matcher);
        return repository.findAll(example);
    }
}

But I would like a way to customize this so I can use a StringMatcher.CONTAINING and match on part of the book title.

@mp911de mp911de added this to the 1.x Backlog milestone Nov 7, 2024
@mp911de mp911de added the type: enhancement A general enhancement label Nov 7, 2024
@mp911de mp911de self-assigned this Nov 7, 2024
@mp911de
Copy link
Member

mp911de commented Nov 7, 2024

Customizing ExampleMatcher is a reasonable request. Introducing a strategy interface that creates ExampleMatcher would be my initial approach. I'm not quite sure about the context that we should provide to the method. I think something along the lines for an initial cut:

interface ExampleMatcherProvider<T> {

  ExampleMatcher getExampleMatcher(T probe, DataFetchingEnvironment environment);
}

Such a signature would provide the most meaningful context.

For auto-registration, I think a programming model to let repositories implement the interface through a default method would be neat:

@GraphQlRepository
public interface BookRepository extends JpaRepository<Book, Long>, 
                                        QueryByExampleExecutor<Book>, ExampleMatcherProvider<Book> {
    
  default ExampleMatcher getExampleMatcher(Book probe, DataFetchingEnvironment environment) {
    return …;
  }
}

Alternatively, defining a @Bean ExampleMatcherProvider<Book> and using ResolvableType to correlate the strategy object could work too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants