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?
Android. The Apollo Neuro mobile app is supported for smartphones running Android 9 and above.
The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).
The GraphQL schema language supports the scalar types of String , Int , Float , Boolean , and ID , so you can use these directly in the schema you pass to buildSchema . By default, every type is nullable - it's legitimate to return null as any of the scalar types.
gql functionA JavaScript template literal tag that parses GraphQL queries into an abstract syntax tree (AST).
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With