Skip to content

Commit

Permalink
feat: new struct (#10)
Browse files Browse the repository at this point in the history
* feat: new funcs

* fix: lint and upgrade dependencies
  • Loading branch information
productdevbook authored Nov 3, 2023
1 parent c645861 commit 24846d4
Show file tree
Hide file tree
Showing 16 changed files with 1,332 additions and 357 deletions.
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: '18'
node-version: '20'
cache: pnpm

- name: 📦 Install dependencies
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.DS_Store
node_modules
dist
package-lock.json
package-lock.json
.eslintcache
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ import { connectionFromArraySlice } from 'graphql-relay'

const
{
limit, offset, expectedSize,
hasNextPage, hasPreviousPage
limit,
offset,
expectedSize,
hasNextPage,
hasPreviousPage
} = offsetForArgs({
args: {
first: _args.first,
Expand Down
11 changes: 10 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import antfu from '@antfu/eslint-config'

export default antfu()
export default antfu(
{},
{
ignores: [
'dist',
'.github',
'node_modules',
],
},
)
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "ts-relay-cursor-paging",
"type": "module",
"version": "1.1.1",
"packageManager": "pnpm@8.8.0",
"version": "2.0.0-alpha.1",
"packageManager": "pnpm@8.10.0",
"description": "Relay Cursor Paging for GraphQL",
"author": "Mehmet @productdevbook",
"license": "MIT",
Expand All @@ -26,42 +26,41 @@
"exports": {
".": {
"types": "./dist/index.d.mts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"import": "./dist/index.js"
}
},
"main": "./dist/index.cjs",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"engines": {
"node": ">=16",
"pnpm": ">=8"
"node": ">=20"
},
"scripts": {
"build": "tsup",
"dev": "tsup --watch",
"prepublishOnly": "pnpm run build",
"release": "pnpm build && bumpp --commit --push --tag && pnpm publish",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"lint": "eslint . --cache",
"lint:fix": "eslint . --cache --fix",
"test": "vitest",
"test:watch": "vitest --watch",
"coverage": "vitest run --coverage"
},
"dependencies": {
"graphql-relay": "^0.10.0"
"peerDependencies": {
"graphql": "^16.8.0"
},
"devDependencies": {
"@antfu/eslint-config": "1.0.0-beta.18",
"@antfu/eslint-config": "^1.1.0",
"@vitest/coverage-v8": "^0.34.6",
"bumpp": "^9.2.0",
"eslint": "^8.50.0",
"eslint": "^8.52.0",
"graphql": "^16.8.1",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"vite": "^4.4.9",
"vite": "^4.5.0",
"vitest": "^0.34.6"
},
"publishConfig": {
Expand Down
66 changes: 26 additions & 40 deletions playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import { createServer } from 'node:http'
import { offsetForArgs } from 'ts-relay-cursor-paging'
import { connectionFromArraySlice } from 'graphql-relay'
import { resolveOffsetConnection } from 'ts-relay-cursor-paging'
import { GraphQLError } from 'graphql'
import { createSchema, createYoga } from 'graphql-yoga'

const data = [
{
id: 1,
name: 'Library 1',
},
{
id: 2,
name: 'Library 2',
},
{
id: 3,
name: 'Library 3',
},
{
id: 4,
name: 'Library 4',
},
]
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
Expand Down Expand Up @@ -62,30 +54,24 @@ export const schema = createSchema({
resolvers: {
Query: {
libraries: async (_parent, _args, _context, _info) => {
const { offset, expectedSize, hasNextPage } = offsetForArgs({
args: {
first: _args.first,
last: _args.last,
after: _args.after,
before: _args.before,
},
})
const generator = datasLine()

// eslint-disable-next-line no-console
console.log(hasNextPage(data.length))
async function resolveData({ offset, limit }: { offset: number; limit: number }) {
const slicedData = generator.slice(offset, offset + limit)
return slicedData
}

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

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

const page = connectionFromArraySlice(data, _args, {
arrayLength: data.length,
sliceStart: offset,
})
return {
edges: page.edges,
edges: datas.edges,
pageInfo: {
...page.pageInfo,
totalPageCount: expectedSize,
...datas.pageInfo,
},
}
},
Expand All @@ -100,7 +86,7 @@ const yoga = createYoga({ schema })
const server = createServer(yoga)

// Start the server and you're done!
server.listen(4000, () => {
server.listen(3100, () => {
// eslint-disable-next-line no-console
console.info('Server is running on http://localhost:4000/graphql')
console.info('Server is running on http://localhost:3100/graphql')
})
2 changes: 2 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "playground",
"type": "module",
"version": "1.0.0",
"description": "",
"author": "",
Expand All @@ -10,6 +11,7 @@
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"esno": "^0.17.0",
"graphql-yoga": "^4.0.5",
"ts-relay-cursor-paging": "workspace:^"
}
Expand Down
Loading

0 comments on commit 24846d4

Please sign in to comment.