问题
On my project GraphQL schema the object AllowedPeriod
(it's just two fields startsAt/endsAt) can arrive inside different objects of the graph.
When generating queries, apollo is creating a new type for every <parent_object>.AllowedPeriod
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?
回答1:
I think you can use graphQL fragments to solve your issue. Apollo should generate the same fragment class for each of your queries.
For example:
fragment AllowedPeriodFragment on AllowedPeriod {
startsAt
endsAt
}
query GetDevicesQuery() {
devices {
allowedPeriod {
...AllowedPeriodFragment
}
}
actions {
allowedPeriod {
...AllowedPeriodFragment
}
}
}
The generated fragment can be accessed through the fragments() method.
It should look something like:
device.fragments().allowedPeriodFragment()
or action.fragments().allowedPeriodFragment()
来源:https://stackoverflow.com/questions/56355600/duplicate-object-types-in-apollo-graphql-for-android