GraphContainer

@Serializable
data class GraphContainer<T>(val data: T? = null, val errors: List<GraphError>? = null, val extensions: Map<Any, Any>? = null)(source)

Deprecated

Legacy serializer-coupled response wrapper. Use GraphQLResponse with the backend-neutral GraphQLConverterFactory instead.

GraphQL response that is spec compliant.

Legacy: this serializer-coupled response wrapper lives in :compat for the deprecated co.anitrend.retrofit.graphql.converter.GraphConverter flow. New code should use the backend-neutral co.anitrend.retrofit.graphql.model.GraphQLResponse contract with co.anitrend.retrofit.graphql.converter.GraphQLConverterFactory.

This is the top-level response wrapper used as the Retrofit return type for GraphQL operations. It is annotated with kotlinx.serialization.Serializable to enable direct deserialization by KotlinxGraphQLJson when Retrofit passes a parameterized type like GraphContainer<GetCurrentUserData>.

Serialization Behavior

kotlinx.serialization

  • data is deserialized using the type parameter T (requires @Serializable on T)

  • errors is deserialized via GraphError which is @Serializable

  • extensions is @Transient (excluded) because Map<Any, Any> cannot be statically resolved by the kotlinx compiler plugin

Gson (via GsonGraphQLJson)

  • All fields are serialized/deserialized normally, including extensions

Accessing extensions with kotlinx

To read extensions while using kotlinx serialization, define your own @Serializable response wrapper with concrete JSON element types. No custom serializer is required unless the actual wire structure needs transformation:

@Serializable
data class JsonGraphContainer<T>(
val data: T? = null,
val errors: List<JsonGraphError>? = null,
val extensions: JsonObject? = null,
)

@Serializable
data class JsonGraphError(
val message: String? = null,
val path: List<JsonElement>? = null,
val locations: List<GraphError.Location>? = null,
val extensions: JsonObject? = null,
)

@POST("graphql")
suspend fun getCurrentUser(
@Body request: GraphQLRequest<EmptyGraphQLVariables>
): Response<JsonGraphContainer<GetCurrentUserData>>

Alternatively, extract extension values from the raw JSON payload before kotlinx deserialization runs.

Usage with generated response DTOs

@POST("graphql")
suspend fun getCurrentUser(
@Body request: GraphQLRequest<EmptyGraphQLVariables>
): Response<GraphContainer<GetCurrentUserData>>

Parameters

data

The successful response data, or null if the response contains only errors.

errors

A list of GraphQL errors, or null if the response is successful. Each error is deserialized via GraphError.

extensions

The response extensions map. Gson-only; @Transient for kotlinx.

See also

Constructors

Link copied to clipboard
constructor(data: T? = null, errors: List<GraphError>? = null, extensions: Map<Any, Any>? = null)

Properties

Link copied to clipboard
val data: T?
Link copied to clipboard
Link copied to clipboard
@Transient
val extensions: Map<Any, Any>?

Functions

Link copied to clipboard
inline fun <T, R> GraphContainer<T>.mapData(transform: (T) -> R): R?

Transforms GraphContainer.data with transform if present, or returns null.

Link copied to clipboard

Returns the non-null GraphContainer.data or throws GraphQLResponseException if the response contains errors.