Skip to main content

Abstract Types

Abstract types like Interfaces and Unions, require type resolution of its variants on execution. For that, we provide a couple of tools explained in the following sections. You can read the code that executes the following logic in package:leto's GraphQL.resolveAbstractType method.

resolveType

A parameter of Interface and Union types is a function with the signature: String Function(Object result, T abstractType, ResolveObjectCtx ctx). Given a resolved result, the abstract type itself and the ObjectCtx, return the name of the type associated with the result value.

Generics

We compare the resolved result's Dart type with the possible types generic type parameter, if there is only one match (withing the possible types), that will be the resolved type. This happens very often, specially with code generation or when providing a distinct class for each GraphQLObjectType.

This can't be used with Union types which are wrappers over the inner types (like Result<V, E> or Either<L, R>), since generic type of the possible types (V and E) will not match the wrapper type (Result). For this cases you will need to provide a resolveType and extractInner callbacks. With freezed-like unions you don't have to do that since the variants extend the union type.

isTypeOf

If any of the previous fail, you can provide a isTypeOf callback for objects, which determine whether a given value is an instance of that GraphQLObjectType.

__typename

If the resolved result is a Map and contains a key "__typename", we will use it to resolve the type by comparing it with possible types names. If there is a match, we use the matched type in the next steps of execution.

Serialize and validate