9

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?

4

1 回答 1

6

我认为您可以使用 graphQL 片段来解决您的问题。Apollo 应该为您的每个查询生成相同的片段类。

例如:

fragment AllowedPeriodFragment on AllowedPeriod {
    startsAt
    endsAt
}

query GetDevicesQuery() {
    devices {
        allowedPeriod { 
            ...AllowedPeriodFragment 
        }
    }

    actions {
        allowedPeriod { 
            ...AllowedPeriodFragment 
        }
    }
}

生成的片段可以通过fragments() 方法访问。

它应该看起来像: device.fragments().allowedPeriodFragment()action.fragments().allowedPeriodFragment()

于 2019-06-14T22:30:14.737 回答