GithubHelp home page GithubHelp logo

frankyang0529 / mongo-relay-connection Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jackytck/mongo-relay-connection

0.0 3.0 0.0 220 KB

Assist building relay connection from mongoose.

License: MIT License

JavaScript 100.00%

mongo-relay-connection's Introduction

build status NPM version Coverage Status js-standard-style devDependency Status devDevDependency Status

Install

yarn add mongo-relay-connection graphql graphql-relay

graphql and graphql-relay are required as peer dependencies.

Overview

To assist building a Relay Connection type from a mongoose schema. It supports dynamic collection. The order could be based on a field that is not necessarily unique. And existing schema need not be changed at all.

It is based on the Relay pagination algorithm. But as including a value for both first and last is confusing, the last is ignored if both are given.

# after first before last remarks support
1 returns all
2
3
4
5
6 same as #5
7
8 same as #7
9
10
11
12
13
14 same as #13
15
16 same as #15

Usage

Suppose you want to do cursor based pagination over a collection:

// models/product.js

import mongoose, { Schema } from 'mongoose'

const ProductSchema = new Schema({
  name: String,
  type: String,
  price: Number
})

export default mongoose.model('Product', ProductSchema)

First create a corresponding GraphQLObjectType:

// types/product.js

import {
  GraphQLObjectType,
  GraphQLID,
  GraphQLString,
  GraphQLInt
} from 'graphql'

const Product = new GraphQLObjectType({
  name: 'Product',
  fields: {
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    type: { type: GraphQLString },
    price: { type: GraphQLInt }
  }
})

export default Product

Then create your query by defining the type, args, and resolve function. Here all the food product is selected and sorted by price descendingly:

import {
  GraphQLObjectType
} from 'graphql'
import {
  mrType,
  mrArgs,
  mrResolve
} from 'mongo-relay-connection'
import Product from './types/product'
import ProductModel from './models/product'

const foodTypes = [
  "Bacon",
  "Cheese",
  "Chicken",
  "Chips",
  "Fish",
  "Pizza",
  "Salad",
  "Sausages",
  "Tuna"
]

const RootQuery = new GraphQLObjectType({
  name: 'RootQuery',
  fields: {
    allFoodProducts: {
      type: mrType('FoodProduct', Product),
      args: mrArgs,
      resolve (parentValue, args) {
        const query = {
          type: { $in: foodTypes }
        }
        const opts = {
          cursorField: 'price',
          direction: -1
        }
        return mrResolve(args, ProductModel, query, opts)
      }
    }
  }
})

export default RootQuery

Boom, you're done! No third step. All the hard work of resolving is done for you.

Limitation

It is based on sorting on a single given field (default is _id). If the field is not unique, it is compounded with _id as the secondary sort. So it could only be sorted in one given dimension.

License

MIT

mongo-relay-connection's People

Contributors

jackytck avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.