11

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?

Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
Budius
  • 39,391
  • 16
  • 102
  • 144
  • I think you actually can not specify yourself the name of the object in the current state of the library. You have to use the generated ones – Fcps Jun 12 '19 at 12:25

1 Answers1

8

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()

Aidan Laing
  • 1,260
  • 11
  • 23
  • Unfortunately I can't test it until tomorrow at work (when the bounty time is over), but I've read on the docs and it seems reasonable. So I'm letting the bounty go to you and this next week I'll test it and comment back – Budius Jun 16 '19 at 06:53