Socket
Socket
Sign inDemoInstall

graphql-connection-resolver

Package Overview
Dependencies
3
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    graphql-connection-resolver

Helps to easily implement the relay connection specification (inspired by Nexus.js Connection Plugin)


Version published
Weekly downloads
7
increased by600%
Maintainers
1
Install size
113 kB
Created
Weekly downloads
 

Readme

Source

🔗 GraphQL Connection Resolver

Helps to easily implement the relay connection specification (inspired by Nexus.js Connection Plugin)

What is the Connection?

Install

$ yarn add graphql-connection-resolver

Example

Schema

scalar DateTime

type Query {
  chatRoom(id: String!): ChatRoom
}

type ChatRoom {
  id: ID!

  # if messages empty, returns null
  messages(
    first: Int
    last: Int
    before: Int
    after: Int
  ): ChatMessageConnection!
}

type ChatMessage {
  id: ID!
  createdAt: DateTime!
}

type ChatMessageConnection {
  edges: [ChatMessageEdge!]!
  pageInfo: PageInfo!
}

type ChatMessageEdge {
  node: ChatMessage!
  cursor: String!
}

type PageInfo {
  hasPreviousPage: Boolean!
  hasNextPage: Boolean!
  startCursor: String
  endCursor: String
}

Resolver

import { connection } from 'graphql-connection-resolver'

export const ChatRoom = {
  messages: connection({
    /**
     * returns a list of the model with `parent`, `args`, `ctx`
     * You must request one more than given by first and last.
     * Inside the library, if nodes return the same number as the given `first` or `last`, the next page is considered to not exist.
     * and if nodes return more than the given number, the next page is considered to exist.
     */
    async nodes(parent, args, ctx) {
      return [
        /* ... */
      ]
    },

    /**
     * Extract a string to be used as a cursor from node.
     * It automatically performs base64 encoding and decoding inside,
     * so just return plain text.
     */ 
    cursorFromNode(node) {
      return node.createdAt.toISOString()
    },
  }),
}

Note

  • You must request one more than given by first and last. Inside the library, if nodes return the same number as the given first or last, the next page is considered to not exist, and if nodes return more than the given number, the next page is considered to exist.

    connection({
      async nodes(args) {
        const items = await fetchItems({
          /* ... */,
          limit: args.first + 1,
        })
    
        /* ... */
      }
    })
    

References

If you have a feature request or a bug, please create a new issue. And also, pull requests are always welcome 🙏

Keywords

FAQs

Last updated on 02 Nov 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc