Declaring non-null nodes for edges when using Absinthe.Relay.Connection

I’m using absinthe_relay to paginate a list in my graphql api. I have a query defined as follows:

connection field(:matches, node_type: :match) do
  arg(:order, type: :sort_order, default_value: :desc)
  resolve(&MatchResolver.list_paginated/3)
end

This results in the MatchConnection type which contains:

edges: [MatchEdge]

where MatchEdge is:

cursor: String!
node: Match

As you can see all these types are nullable.

I use graphql-code-generator to generate TypeScript types and this results in a mess of nullable types:


export interface MatchConnection {
  edges?: Maybe<(Maybe<MatchEdge>)[]>;

  pageInfo: PageInfo;
}

export interface MatchEdge {
  cursor: string;
  node?: Maybe<Match>;
}

Why are all these fields nullable and is there a way to make edges and node non null, as they should always be present, regardless of the number of edges in the result? (if there’s nothing in the list the edges list can just be empty.

Assuming you have a line like this somewhere in your schema.ex:

connection(node_type: :match)

you will get this in your schema:

edges: [MatchEdge]

If you change to:

connection(node_type: :match, non_null: true)

you will get this in your schema:

edges: [MatchEdge!]!

If you change to:

connection(node_type: :match, non_null_edge: true)

you will get this in your schema:

edges: [MatchEdge!]

If you change to:

connection(node_type: :match, non_null_edges: true)

you will get this in your schema:

edges: [MatchEdge]!

So doing this:

connection(node_type: :match, non_null: true)

is the same as doing this:

connection(node_type: :match, non_null_edge: true, non_null_edges: true)

which is what you want.

1 Like

Thanks, yes I managed to solve this once support for non-null edges was released in absinthe_relay 1.5.

In fact, what I used was

connection(:match, node_type: non_null(:match), non_null: true)

which results in

edges: [MatchEdge!]!

where MatchEdge is

cursor: String
node: Match!
2 Likes

Why not just do:

connection(node_type: :match, non_null: true)

is it to get the Node to be non-nullable?

Edit: never mind, I tested it and, yes, that’s what it does.
Thanks @Harrygr I’m actually going to copy that exact syntax! My Typescript language server is going to love this.