graphql-mongoose-schemabuilder

1.0.10 • Public • Published

graphql-mongoose-schemabuilder

This is a plugin for graphql-compose-mongoose and graphql-compose, which derives GraphQLType from your mongoose model. Also derives bunch of internal GraphQL Types. Auto generates schema composer, including graphql connection, also provided basic search via operators ($lt, $gt and so on) with a added feature to Search by regular expression.

Installation

npm install graphql graphql-compose mongoose graphql-compose-mongoose graphql-mongoose-schemabuilder --save

Modules graphql, graphql-compose, mongoose, graphql-compose-mongoose are in peerDependencies, so should be installed explicitly in your app. They have global objects and should not have ability to be installed as submodule.

Example

Source code: https://github.com/ShaneAlexGraham/graphql-mongoose-example

const mongoose = require('mongoose');
const { buildSchemaFromModels } = require('graphql-mongoose-schemabuilder');

// STEP 1: DEFINE MONGOOSE SCHEMA AND MODEL
const LanguagesSchema = new mongoose.Schema({
  language: String,
  skill: {
    type: String,
    enum: [ 'basic', 'fluent', 'native' ],
  },
});

const UserSchema = new mongoose.Schema({
  name: String, // standard types
  age: {
    type: Number,
    index: true,
  },
  ln: {
    type: [LanguagesSchema], // you may include other schemas (here included as array of embedded documents)
    default: [],
    alias: 'languages', // in schema `ln` will be named as `languages`
  },
  contacts: { // another mongoose way for providing embedded documents
    email: String,
    phones: [String], // array of strings
  },
  gender: { // enum field with values
    type: String,
    enum: ['male', 'female'],
  },
  someMixed: {
    type: mongoose.Schema.Types.Mixed,
    description: 'Can be any mixed type, that will be treated as JSON GraphQL Scalar Type',
  },
});
const User = mongoose.model('User', UserSchema);

const NotesSchema = new mongoose.Schema({
  name: String,
  description: String,
  type: {
    type: String,
    enum: [ 'sticky', 'default'],
  },
  createdby: {  type: mongoose.Schema.Types.ObjectId,  ref: 'User'  }
});
const Note = mongoose.model('User', UserSchema);

// STEP 2: DEFINE MODEL TO IMPORT (THIS CAN ALSO BE A STANDARD ARRAY INSTEAD OF A OBJECT)
const models = {
    Notes: Note,
    Users: User
}

// STEP 3: USE MODELS TO BUILD SCHEMA
const schema = buildSchemaFromModels(models);

// STEP 4: USE SCHEMA IN YOUR FAVORITE GRAPHQL ENGINE
const gqlServer = new ApolloServer({
  schema: schema 
});

Query

Field Argument Type Description
ExampleById Example
_id MongoID!
ExampleByIds [Example!]
_ids [MongoID]!
limit Int
sort SortFindByIdsExampleInput
ExampleOne Example
filter FilterFindOneExampleInput

Filter by fields

skip Int
sort SortFindOneExampleInput
ExampleMany [Example!]
filter FilterFindManyExampleInput

Filter by fields

skip Int
limit Int
sort SortFindManyExampleInput
ExampleCount Int
filter FilterExampleInput

Filter by fields

ExampleConnection ExampleConnection
first Int

Forward pagination argument for returning at most first edges

after String

Forward pagination argument for returning at most first edges

last Int

Backward pagination argument for returning at most last edges

before String

Backward pagination argument for returning at most last edges

filter FilterFindManyExampleInput

Filter by fields

sort SortConnectionExampleEnum

Sort argument for data ordering

ExamplePagination ExamplePagination
page Int

Page number for displaying

perPage Int
filter FilterFindManyExampleInput

Filter by fields

sort SortFindManyExampleInput

Mutation

Field Argument Type Description
ExampleCreateOne CreateOneExamplePayload

Create one document with mongoose defaults, setters, hooks and validation

record CreateOneExampleInput!
ExampleCreateMany CreateManyExamplePayload

Creates Many documents with mongoose defaults, setters, hooks and validation

records [CreateManyExampleInput!]!
ExampleUpdateById UpdateByIdExamplePayload

Update one document: 1) Retrieve one document by findById. 2) Apply updates to mongoose document. 3) Mongoose applies defaults, setters, hooks and validation. 4) And save it.

record UpdateByIdExampleInput!
ExampleUpdateOne UpdateOneExamplePayload

Update one document: 1) Retrieve one document via findOne. 2) Apply updates to mongoose document. 3) Mongoose applies defaults, setters, hooks and validation. 4) And save it.

record UpdateOneExampleInput!
filter FilterUpdateOneExampleInput

Filter by fields

sort SortUpdateOneExampleInput
skip Int
ExampleUpdateMany UpdateManyExamplePayload

Update many documents without returning them: Use Query.update mongoose method. Do not apply mongoose defaults, setters, hooks and validation.

record UpdateManyExampleInput!
filter FilterUpdateManyExampleInput

Filter by fields

sort SortUpdateManyExampleInput
skip Int
limit Int
ExampleRemoveById RemoveByIdExamplePayload

Remove one document: 1) Retrieve one document and remove with hooks via findByIdAndRemove. 2) Return removed document.

_id MongoID!
ExampleRemoveOne RemoveOneExamplePayload

Remove one document: 1) Remove with hooks via findOneAndRemove. 2) Return removed document.

filter FilterRemoveOneExampleInput

Filter by fields

sort SortRemoveOneExampleInput
ExampleRemoveMany RemoveManyExamplePayload

Remove many documents without returning them: Use Query.remove mongoose method. Do not apply mongoose defaults, setters, hooks and validation.

filter FilterRemoveManyExampleInput!

Filter by fields

Objects

CreateManyExamplePayload

Field Argument Type Description
recordIds [MongoID!]!

Created document ID

records [Example!]!

Created documents

createCount Int!

Count of all documents created

CreateOneExamplePayload

Field Argument Type Description
recordId MongoID

Created document ID

record Example

Created document

Example

Field Argument Type Description
example String
_id MongoID!

ExampleConnection

A connection to a list of items.

Field Argument Type Description
count Int!

Total object count.

pageInfo PageInfo!

Information to aid in pagination.

edges [ExampleEdge!]!

Information to aid in pagination.

ExampleEdge

An edge in a connection.

Field Argument Type Description
node Example!

The item at the end of the edge

cursor String!

A cursor for use in pagination

ExamplePagination

List of items with pagination.

Field Argument Type Description
count Int

Total object count.

items [Example!]

Array of objects.

pageInfo PaginationInfo!

Information to aid in pagination.

PageInfo

Information about pagination in a connection.

Field Argument Type Description
hasNextPage Boolean!

When paginating forwards, are there more items?

hasPreviousPage Boolean!

When paginating backwards, are there more items?

startCursor String

When paginating backwards, the cursor to continue.

endCursor String

When paginating forwards, the cursor to continue.

PaginationInfo

Field Argument Type Description
currentPage Int!
perPage Int!
pageCount Int
itemCount Int
hasNextPage Boolean
hasPreviousPage Boolean

RemoveByIdExamplePayload

Field Argument Type Description
recordId MongoID

Removed document ID

record Example

Removed document

RemoveManyExamplePayload

Field Argument Type Description
numAffected Int

Affected documents number

RemoveOneExamplePayload

Field Argument Type Description
recordId MongoID

Removed document ID

record Example

Removed document

UpdateByIdExamplePayload

Field Argument Type Description
recordId MongoID

Updated document ID

record Example

Updated document

UpdateManyExamplePayload

Field Argument Type Description
numAffected Int

Affected documents number

UpdateOneExamplePayload

Field Argument Type Description
recordId MongoID

Updated document ID

record Example

Updated document

Inputs

CreateManyExampleInput

Field Type Description
example String

CreateOneExampleInput

Field Type Description
example String

ExampleSearch

String or Regular Expression

Field Type Description
example [String]

field to apply regular expression

FilterExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterExampleInput!]
AND [FilterExampleInput!]

FilterFindManyExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterFindManyExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterFindManyExampleInput!]
AND [FilterFindManyExampleInput!]
Search ExampleSearch

Search by String or Regular Expression

FilterFindOneExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterFindOneExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterFindOneExampleInput!]
AND [FilterFindOneExampleInput!]

FilterRemoveManyExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterRemoveManyExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterRemoveManyExampleInput!]
AND [FilterRemoveManyExampleInput!]

FilterRemoveOneExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterRemoveOneExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterRemoveOneExampleInput!]
AND [FilterRemoveOneExampleInput!]

FilterUpdateManyExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterUpdateManyExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterUpdateManyExampleInput!]
AND [FilterUpdateManyExampleInput!]

FilterUpdateOneExampleInput

Field Type Description
example String
_id MongoID
_ids [MongoID]
_operators OperatorsFilterUpdateOneExampleInput

List of indexed fields that can be filtered via operators.

OR [FilterUpdateOneExampleInput!]
AND [FilterUpdateOneExampleInput!]

OperatorsFilterExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterExampleInput

OperatorsFilterFindManyExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterFindManyExampleInput

OperatorsFilterFindOneExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterFindOneExampleInput

OperatorsFilterRemoveManyExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterRemoveManyExampleInput

OperatorsFilterRemoveOneExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterRemoveOneExampleInput

OperatorsFilterUpdateManyExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterUpdateManyExampleInput

OperatorsFilterUpdateOneExampleInput

For performance reason this type contains only indexed fields.

Field Type Description
_id _idOperatorsFilterUpdateOneExampleInput

UpdateByIdExampleInput

Field Type Description
example String
_id MongoID!

UpdateManyExampleInput

Field Type Description
example String

UpdateOneExampleInput

Field Type Description
example String

_idOperatorsFilterExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

_idOperatorsFilterFindManyExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

_idOperatorsFilterFindOneExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

_idOperatorsFilterRemoveManyExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

_idOperatorsFilterRemoveOneExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

_idOperatorsFilterUpdateManyExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

_idOperatorsFilterUpdateOneExampleInput

Field Type Description
gt MongoID
gte MongoID
lt MongoID
lte MongoID
ne MongoID
in [MongoID]
nin [MongoID]

Enums

SortConnectionExampleEnum

Value Description
_ID_DESC
_ID_ASC

SortFindByIdsExampleInput

Value Description
_ID_ASC
_ID_DESC

SortFindManyExampleInput

Value Description
_ID_ASC
_ID_DESC

SortFindOneExampleInput

Value Description
_ID_ASC
_ID_DESC

SortRemoveOneExampleInput

Value Description
_ID_ASC
_ID_DESC

SortUpdateManyExampleInput

Value Description
_ID_ASC
_ID_DESC

SortUpdateOneExampleInput

Value Description
_ID_ASC
_ID_DESC

Scalars

Boolean

The Boolean scalar type represents true or false.

Int

The Int scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.

MongoID

The ID scalar type represents a unique MongoDB identifier in collection. MongoDB by default use 12-byte ObjectId value (https://docs.mongodb.com/manual/reference/bson-types/#objectid). But MongoDB also may accepts string or integer as correct values for _id field.

String

The String scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i graphql-mongoose-schemabuilder

Weekly Downloads

0

Version

1.0.10

License

MIT

Unpacked Size

57.9 kB

Total Files

11

Last publish

Collaborators

  • shane.alex.graham