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.
在生成查询时,apollo 为每个
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
有没有办法告诉 apollo 这些是相同的类型,并且不应该为它们中的每一个都生成类型?
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
}
}
}
可以通过 fragment() 方法访问生成的片段.
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屋!