npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-relay-cursor-paging

v2.1.0

Published

Relay Cursor Paging for GraphQL

Downloads

196

Readme

TS Relay Cursor Paging

TS Relay Cursor Paging

Description

Simple relay cursor paging for graphql

Installation

pnpm add ts-relay-cursor-paging

Demo

Open graphql playground in your browser port 4000/graphql

Edit ts-relay-cursor-paging

Docs

resolveOffsetConnection

import { resolveOffsetConnection } from 'ts-relay-cursor-paging'

resolveOffsetConnection({ args }, ({ limit, offset }) => {
  const items = []

  for (let i = offset; i < Math.min(offset + limit, 200); i += 1)
    items.push(new NumberThing(i))

  return items
})

resolveCursorConnection

import { resolveCursorConnection } from 'ts-relay-cursor-paging'

const objects: { id: number }[] = []

for (let i = 0; i < 100; i += 1)
  objects.push({ id: i + 1 })

function queryWithCursor(limit: number, inverted: boolean, after?: string, before?: string) {
  const list = objects.filter(({ id }) => {
    if (before && id >= Number.parseInt(before, 10))
      return false

    if (after && id <= Number.parseInt(after, 10))
      return false

    return true
  })

  return (inverted ? list.reverse() : list).slice(0, limit)
}

// resolveCursorConnection

resolveCursorConnection(
  {
    defaultSize: 5,
    maxSize: 8,
    args,
    toCursor: obj => obj.id.toString(),
  },
  ({ before, after, inverted, limit }: ResolveCursorConnectionArgs) =>
    queryWithCursor(limit, inverted, after, before),
)

resolveArrayConnection

import { resolveArrayConnection } from 'ts-relay-cursor-paging'

const numbers: { id: number }[] = []

for (let i = 0; i < 200; i += 1)
  numbers.push({ id: i + 1 })

resolveArrayConnection({ args }, numbers)

Usage

import { createServer } from 'node:http'
import { resolveOffsetConnection } from 'ts-relay-cursor-paging'
import { GraphQLError } from 'graphql'
import { createSchema, createYoga } from 'graphql-yoga'

function datasLine() {
  const datas = []

  for (let i = 0; i < 100; i++) {
    datas.push({
      id: i,
      name: `Library ${i}`,
    })
  }

  return datas
}
export const schema = createSchema({
  typeDefs: /* GraphQL */ `
    scalar Cursor

    type PageInfo {
      hasNextPage: Boolean
      hasPreviousPage: Boolean
      startCursor: Cursor
      endCursor: Cursor
      totalPageCount: Int
    }

    type Library {
      id: ID!
      name: String!
    }

    type LibraryEdge {
        cursor: String!
        node: Library!
    }

    type LibraryConnection {
      edges: [LibraryEdge!]!
      pageInfo: PageInfo!
    }

    type Query {
      libraries(
        first: Int
        after: Cursor
        last: Int
        before: Cursor
      ): LibraryConnection
    }
  `,
  resolvers: {
    Query: {
      libraries: async (_parent, _args, _context, _info) => {
        const generator = datasLine()

        async function resolveData({ offset, limit }: { offset: number, limit: number }) {
          const slicedData = generator.slice(offset, offset + limit)
          return slicedData
        }

        const datas = await resolveOffsetConnection({ args: _args }, ({ limit, offset }) => {
          return resolveData({ limit, offset })
        })

        if (!generator)
          throw new GraphQLError('No libraries found')

        return {
          edges: datas.edges,
          pageInfo: {
            ...datas.pageInfo,
          },
        }
      },
    },
  },
})

// Create a Yoga instance with a GraphQL schema.
const yoga = createYoga({ schema })

// Pass it into a server to hook into request handlers.
const server = createServer(yoga)

// Start the server and you're done!
server.listen(3100, () => {
  console.info('Server is running on http://localhost:3100/graphql')
})

Inspiration

Codes in this build are inspired by pothos and from there the codes were copied. Thanks you for your great work.

Credits

Sponsors

License

MIT License © 2022-PRESENT productdevbook