Android版Apollo GraphQL中的重复对象类型 [英] Duplicate object types in Apollo GraphQL for Android
问题描述
在我的项目GraphQL模式上,对象 AllowedPeriod
(只有两个字段startsAt/endsAt)可以到达图形的不同对象内.
On my project GraphQL schema the object AllowedPeriod
(it's just two fields startsAt/endsAt) can arrive inside different objects of the graph.
生成查询时,阿波罗会为每个< parent_object> .AllowedPeriod
When generating queries, apollo is creating a new type for every <parent_object>.AllowedPeriod
例如,在 GetDevicesQuery
中, AllowedPeriod
可以位于 devices
, actions
或组
,从而生成以下类.
For example, in the GetDevicesQuery
, the AllowedPeriod
can be inside devices
, actions
or group
, hence generating the following classes.
-
GetDevicesQuery.AllowedPeriod
-
GetDevicesQuery.AllowedPeriod1
-
GetDevicesQuery.AllowedPeriod2
有没有办法告诉阿波罗这些类型是相同的类型,并且不应该为每个类型生成类型?
Is there a way to tell apollo that those are the same types and that it should not generate types for every one of them?
推荐答案
我认为您可以使用graphQL片段来解决您的问题.Apollo应该为每个查询生成相同的片段类.
I think you can use graphQL fragments to solve your issue. Apollo should generate the same fragment class for each of your queries.
例如:
fragment AllowedPeriodFragment on AllowedPeriod {
startsAt
endsAt
}
query GetDevicesQuery() {
devices {
allowedPeriod {
...AllowedPeriodFragment
}
}
actions {
allowedPeriod {
...AllowedPeriodFragment
}
}
}
可以通过fragments()方法访问生成的片段.
The generated fragment can be accessed through the fragments() method.
它看起来应该像这样: device.fragments().allowedPeriodFragment()
或 action.fragments().allowedPeriodFragment()
It should look something like:
device.fragments().allowedPeriodFragment()
or action.fragments().allowedPeriodFragment()
这篇关于Android版Apollo GraphQL中的重复对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!