Home / Class/ GraphQLCollectionBuilder Class — supabase Architecture

GraphQLCollectionBuilder Class — supabase Architecture

Architecture documentation for the GraphQLCollectionBuilder class in connections.ts from the supabase codebase.

Entity Profile

Relationship Graph

Source Code

apps/docs/resources/utils/connections.ts lines 278–333

export class GraphQLCollectionBuilder {
  static async create<ItemType, FetchArgs = unknown, ErrorType = Error>(
    options: CollectionBuildArgs<ItemType, FetchArgs, ErrorType>
  ): Promise<Result<GraphQLCollection<ItemType>, GraphQLError | ErrorType>> {
    const { fetch, args = {}, getCursor, items } = options

    if (items) {
      return Result.ok(GraphQLCollectionBuilder.paginateArray({ items, args }))
    }

    if (args.first && args.last) {
      return Result.error(new GraphQLError('Cannot specify both first and last arguments'))
    }

    return (await fetch(args)).map(
      ({ items: fetchedItems, totalCount, hasNextPage = false, hasPreviousPage = false }) => {
        const edges = fetchedItems.map((item) => {
          return { node: item, cursor: getCursor(item) }
        })

        return {
          edges,
          nodes: fetchedItems,
          totalCount,
          pageInfo: {
            hasNextPage,
            hasPreviousPage,
            startCursor: edges.length > 0 ? edges[0].cursor : null,
            endCursor: edges.length > 0 ? edges[edges.length - 1].cursor : null,
          },
        }
      }
    )
  }

  private static paginateArray<T>({ items, args }: CollectionInMemory<T>) {
    const getCursor = (_item: T, idx: number) => String(idx)
    const allEdges = items.map((item, idx) => {
      return { node: item, cursor: getCursor(item, idx) }
    })

    const { edges, hasPreviousPage, hasNextPage } = getRequestedSlice(allEdges, args ?? {})

    return {
      edges,
      nodes: edges.map((edge) => edge.node),
      totalCount: allEdges.length,
      pageInfo: {
        hasNextPage,
        hasPreviousPage,
        startCursor: edges.length > 0 ? edges[0].cursor : null,
        endCursor: edges.length > 0 ? edges[edges.length - 1].cursor : null,
      },
    }
  }
}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free