[英]Apollo GraphQL - Is it possible to do the same query requesting different fields?
我正在使用apollo-android ,我想知道是否可以執行以下方法:
我在服務器的schema.json
中定義了一個名為locations
的查詢。 定義如下:
type locations {
continents: [Continent!],
countries: [Country!],
usaStates: [UsaState!]
}
在Playground中嘗試此查詢非常有效,並且它返回大洲,國家和usaStates。 這是我在Playground中定義的查詢:
query getLocations {
locations {
continents {
name
code
}
countries {
name
code
}
usaStates {
code
name
}
}
}
當然,如果我只請求各大洲,它只會返回各大洲:
query getLocations {
locations {
continents {
name
code
}
}
我的問題是,是否有可能在客戶端執行相同的操作。 當我在Android項目的api.graphql
文件中定義查詢時,我會這樣做:
query getLocations {
locations {
continents {
name
code
}
countries {
code
name
}
usaStates {
code
name
}
}
}
然后,當我在代碼中調用它時,結果包含大洲,國家和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);
}
});
有一種方法可以進行相同的調用,但可以定義我只想要大洲,而不是大洲,國家和美國州?
先感謝您!
規格說明:
核心GraphQL規范恰好包含兩個指令,任何符合規范的GraphQL服務器實現都必須支持這些指令:
- @include (如果:布爾值)如果參數為true,則僅在結果中包括此字段。
- @skip (如果為Boolean)如果參數為true,則跳過此字段。
帶有skip指令的api.graphql
:
query getLocations($continentsOnly: Boolean) {
locations {
continents {
name
code
}
countries @skip(if: $continentsOnly) {
code
name
}
usaStates @skip(if: $continentsOnly) {
code
name
}
}
}
顯然,您將需要指定continentsOnly
的值
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.