Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate object types in Apollo GraphQL for Android

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?

like image 994
Budius Avatar asked May 29 '19 07:05

Budius


People also ask

Is Apollo Available on Android?

Android. The Apollo Neuro mobile app is supported for smartphones running Android 9 and above.

What is __ Typename in GraphQL?

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

What other types can be used in a GraphQL schema?

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.

What is Gql in GraphQL?

gql functionA JavaScript template literal tag that parses GraphQL queries into an abstract syntax tree (AST).


1 Answers

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

like image 50
Aidan Laing Avatar answered Nov 13 '22 01:11

Aidan Laing