Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-relay-connection

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

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.

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
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

Package last updated on 26 May 2019

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc