graphql-relay-connection
A GraphQL Relay connection with custom cursor functions. Can be used for MongoDB, Mongoose, plain objects, scalars, or any data formats.
Install
npm install --save graphql-relay-connection
Usage
mongooseConnection
is predefined for you.
For other usage, scroll down to Custom connection
section below.
; ; const connectionFromPromisedArray cursorToDocument = mongooseConnection; const UserType = name: 'User' friends: type: UserConnection args: connectionArgs ; const connectionType: UserConnection = ;
Custom connection
Simply provide { comparableToCursor, cursorToComparable, comparator }
to defineConnection
.
;; const PREFIX = 'number:'; // Given a comparable value, return a string cursor. { return ;} // Given a string cursor,// return a comparable value for the comparator function. { const num = ; return ? null : num;} // Sort function for array.sort().// Given two values, return an interger. { return num1 - num2;} const connectionFromArray connectionFromPromisedArray = ;
How mongoose connection is defined
;;; ; ; const PREFIX = 'mongoose:'; : ConnectionCursor { return ;} : ?Document { const unbased = ; if const id = unbased; if id return id; return null;} : number { if doc1id < doc2id return -1; if doc1id > doc2id return 1; return 0;} const connectionFromArray connectionFromPromisedArray = ; ;