Socket
Socket
Sign inDemoInstall

mongoose-relay-connection

Package Overview
Dependencies
4
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mongoose-relay-connection

Easiest and lightweight way to create a relay connection from mongoose query for paginate over.


Version published
Weekly downloads
1
decreased by-66.67%
Maintainers
1
Install size
9.19 MB
Created
Weekly downloads
 

Readme

Source

mongoose-relay-connection

Easiest and lightweight way to create a Relay Connection from Mongoose query for paginate thru.

Install

npm i mongoose-relay-connection --save

Use

Import connectionFromMongooseQuery function from package to file with your Relay Connection definition:

import connectionFromMongooseQuery from 'mongoose-relay-connection'

Use it inside resolver of your connection (don't forget to await!):

const PostsConnection = {
  type: PostsConnectionType,
  args: connectionArgs,
  resolve: async (relayRoot, args) => {
    const query = Post.find()
    return await connectionFromMongooseQuery(query, args)
  }
}

Example

Full working example could look like so:

import { GraphQLInt, GraphQLList } from 'graphql'
import { connectionArgs, connectionDefinitions } from 'graphql-relay'
import GraphQLPost from 'projectRoot/data/schema/types/GraphQLPost'
import Post from 'projectRoot/data/models/Post'
import connectionFromMongooseQuery from 'mongoose-relay-connection'

const { connectionType: PostsConnectionType } = connectionDefinitions({
  name: 'Posts',
  nodeType: GraphQLPost,
  connectionFields: () => ({
    totalCount: {
      type: GraphQLInt,
      resolve: connection => connection.totalCount
    },
    posts: {
      type: new GraphQLList(GraphQLPost),
      resolve: connection => connection.edges.map(edge => edge.node)
    }
  })
})

const PostsConnection = {
  type: PostsConnectionType,
  args: connectionArgs,
  resolve: async (relayRoot, args) => {
    const query = Post.find() // Here you can add any filtering or sorting to your query
    return await connectionFromMongooseQuery(query, args)
  }
}

Keywords

FAQs

Last updated on 26 May 2019

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