2

i want to Query only selected fields using Apollo graphql in java.

Dont find any article which shows how we can achieve that

I define my query in .graphql file like this,

query getResources{
   resources(filterBy: {} ) {
      edges{
         cursor
         node   {
                id
                name
                canonicalName
                description
                createdAt
                updatedAt
                createdBy
                updatedBy
         }
      }
      pageInfo{
          hasPreviousPage
          hasNextPage
      }
   }
}

while making the query request execute = apolloClient.query(getResourcesQuery).execute();

i want to change the getResourcesQuery object to only query certain fields. how can we do that ?

9
  • just remove elements you don't need Commented May 14, 2020 at 16:49
  • @xadm i want to dynamically change the query Commented May 14, 2020 at 16:54
  • if i change this query, that would still be static Commented May 14, 2020 at 16:54
  • why do you need this ? Commented May 14, 2020 at 16:57
  • @xadm so i am building client sdk for my graphql application, while making the query, i want to allow the client to query only the fields it wants Commented May 14, 2020 at 17:01

2 Answers 2

3

Apollo Android is not really a query builder -- you can't specify individual fields to add to a selection set. Instead, your provided query is sent as-is. If you're looking for that sort of functionality, you may want to look into a different client (like nodes).

That said, you can utilize the @skip and @include directives, combined with some variables, to dynamically control what's included in your request's selection set. For example:

query getResources(
  $includeEdges: Boolean = true
  $includePageInfo: Boolean = true
) {
  resources(filterBy: {}) {
    edges @include(if: $includeEdges) {
      cursor
      node {
        id
        name
        canonicalName
        description
        createdAt
        updatedAt
        createdBy
        updatedBy
      }
    }
    pageInfo @include(if: $includePageInfo) {
      hasPreviousPage
      hasNextPage
    }
  }
}

Then just add the variables:

GetResources getResourcesQuery = GetResources.builder()
    .includePageInfo(false)
    .build();

apolloClient().query(getResourcesQuery).execute();
Sign up to request clarification or add additional context in comments.

Comments

1

This feature is not supported in apollo

https://github.com/apollographql/apollo-android/issues/1014 dynamic query graphql apollo with java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.