UDF GraphQL resolver with nested type

@ClaudiaGiv My apologies again, you clearly said you needed Columns in order.

Unfortunately, that is not possible out-of-the-box. There are no ordering arguments possible in nested queries. There is a proposal put out there to make the generated API match the OpenCRUD specification, but that sounds a ways off.

(Note that Topic doesn’t address nested sorting directly, but OpenCRUD would)

I think you have a couple of options then to get the Columns data sorted.

Sort in your app

Are you paginating over the columns and need to only fetch the first 50 results of the lowest weight? Then sorting in the index will be important, and this won’t really be a good option.

If you will always fetch 100% of the columns for a given Board, then trying to sort on the Column weight inside the fauna query is probably much more of a burden than than the value you might get some it. Consider sorting the array in your app.

Query the columns directly

Instead of using a nested query, the UDF resolver can return the columns directly.

schema:

type Query {
  columnsByBoardTitle(title: String!): [Column!]! @resolver(name: 'columnsByBoardTitle')
}

UDF:

CreateFunction({
  name: 'columnsByBoardTitle',
  body: Query(
    Lambda(
      ['title'],
      Let(
        {
          boardRef: Select(
            ['data', 0],
            Paginate(Match(Index('unique_Board_title'), Var('title')))
          )
        },
        Select(
          'data',
          Map(
            Paginate(
              Match(
                Index('all_columns_by_board_order_by_weight'),
                Var('boardRef')
              ),
              { size: 100000 } // Max size, since not paginating
            ),
            Lambda(['weight', 'ref'], Get(Var('ref')))
          )
        )
      )
    )
  )
})

I got this working on my end:

Side Notes:

This will NOT be friendly to updating client-side caches, like in Apollo. The more canonical way to keep your boards and columns linked and cached properly is to run the nested query. Which means only doing the sort in your app.

This query shown provides no pagination for the columns, and return ALL of them (assuming under 100000 Documents). If you need to paginate, then you need to account for that in the UDF.

Using the @unique directive, there is already an index created automatically, and the default name would be unique_Board_title.

Create custom payload with @embedded

If you need both Board info and the columns info in one payload, then you can create a new @embedded type and return the data in that format.

1 Like