Duplicate object types in Apollo GraphQL for Android

情到浓时终转凉″ 提交于 2021-02-18 21:58:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!