I am using apollo-android and I want to know if it is possible to do the following approach:
I have defined a query called locations
in the schema.json
of my server. It is defined like this:
type locations {
continents: [Continent!],
countries: [Country!],
usaStates: [UsaState!]
}
Trying this query in the Playground works perfectly and it returns the continents, countries and usaStates. This is the query I define in the Playground:
query getLocations {
locations {
continents {
name
code
}
countries {
name
code
}
usaStates {
code
name
}
}
}
And of course, if I only request que continents, it only returns the continents:
query getLocations {
locations {
continents {
name
code
}
}
My question is if it's possible to do the same in the client part. When I define the query in the api.graphql
file of my Android project, I do it like this:
query getLocations {
locations {
continents {
name
code
}
countries {
code
name
}
usaStates {
code
name
}
}
}
And then, when I call it in the code, the result contains the continents, countries and usaStates:
GetLocationsQuery builder = GetLocationsQuery.builder().build();
api.query(builder)
.enqueue(new ApolloCall.Callback<GetLocationsQuery.Data>() {
@Override
public void onResponse(@NotNull com.apollographql.apollo.api.Response<GetLocationsQuery.Data> response) {
apiListener.onFinish(response);
}
@Override
public void onFailure(@NotNull ApolloException e) {
Log.v("APOLLO", e.toString());
apiListener.onError(e);
}
});
There is a way to do the same call but defining that I only want the continents instead continents, countries and usaStates?
Thank you in advance!
The specification says:
The core GraphQL specification includes exactly two directives, which must be supported by any spec-compliant GraphQL server implementation:
- @include(if: Boolean) Only include this field in the result if the argument is true.
- @skip(if: Boolean) Skip this field if the argument is true.
api.graphql
with skip directive:
query getLocations($continentsOnly: Boolean) {
locations {
continents {
name
code
}
countries @skip(if: $continentsOnly) {
code
name
}
usaStates @skip(if: $continentsOnly) {
code
name
}
}
}
Obviously, you will need to specify the value of continentsOnly