Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

java - getting requested fields on resolver level from graphql

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?

question from:https://stackoverflow.com/questions/65935113/getting-requested-fields-on-resolver-level-from-graphql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...