getting requested fields on resolver level from graphql
Asked Answered
F

2

9

Model of book from graphql schema

type Book {
  id: ID
  name: String
  pageCount: Int
  author: Author
}

So I am having this resolver for Book

public class BookResolver implements BookByIdQueryResolver, GraphQLQueryResolver {
    private final MockRepository mockRepository;

    public BookResolver(MockRepository mockRepository) {
        this.mockRepository = mockRepository;
    }

    @Override
    public BookTO bookById(String id) {
        return mockRepository.getBookById(id);
    }
}

It works fine.

Now lets assume that I am using this graphql query, which is requesting only one field of the Book

{
  bookById(id: "someId") {
    name
  }
}

The question is, how to get info on the bookById method level about the fields which are requested (in this case only the name field)? Is this even possible with the GraphQLQueryResolver concept?

Example with dataFetcher

public DataFetcher getBookByIdDataFetcher() {
        return dataFetchingEnvironment -> {
            String bookId = dataFetchingEnvironment.getArgument("id");
            List<SelectedField> requestedFields = dataFetchingEnvironment.getSelectionSet()
                    .getFields()
                    .stream()
                    .collect(Collectors.toList());
            return books
                    .stream()
                    .filter(book -> book.get("id").equals(bookId))
                    .findFirst()
                    .orElse(null);
        };
    }

This works quite well, but I am interested in the Resolver way. Is it possible?

Fidelia answered 28/1, 2021 at 10:20 Comment(6)
Can you explain your requirement more?Tomas
added some more details, the thing is that in the resolver I am getting only the parameters info (id in this case) by which I am looking for the book. The thing I want to achieve it that I want to be able to check which fields from book are requested in queryFidelia
Did you check this? #48005305Tomas
this is a js solution, I am looking for a solution in java and with specific approach mentionedFidelia
'Each DataFetcher is passed a graphql.schema.DataFetchingEnvironment object which contains what field is being fetched, what arguments have been supplied to the field and other information such as the field’s parent object, the query root object or the query context object.'Moriyama
The other question is how to select only the requested fields with JPA. By default it selects all the columns.Vacancy
M
13

You can add the dataFetchingEnvironment as a parameter and get the list of requested field names using that.

public BookTO bookById(String id, DataFetchingEnvironment dataFetchingEnvironment) {

    List<SelectedField> requestedFields = dataFetchingEnvironment.getSelectionSet()
                .getFields()
                .stream()
                .collect(Collectors.toList());
    // Use this for your use cases


    return mockRepository.getBookById(id);
}

Just to be clear, for the resolver you created and given query request, the response will have only name and no other fields.

Moncada answered 28/1, 2021 at 13:57 Comment(2)
wow, did not expect that it would be this easy, will check thatFidelia
tested it, works perfectly, no idea why this is not mentioned in any resolver examples which I was looking for thans!Fidelia
B
1

I tried the solution posted by Mat G, but it didn't work for me. What worked for me is this:

public BookTO bookById(String id, DataFetchingEnvironment env) throws Exception {
        
        List<String> arguments = env.getField()
                .getSelectionSet()
                .getSelections()
                .parallelStream()
                .map(Field.class::cast)
                .map(Field::getName)
        .collect(Collectors.toList());
        
        // Query using the arguments
        return this.someService.getbookById(id, arguments);
    }
Brethren answered 11/4, 2022 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.