1.0.10 • Published 3 years ago

graphql-mongoose-schemabuilder v1.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

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

Filter by fields

Filter by fields

Filter by fields

Forward pagination argument for returning at most first edges

Forward pagination argument for returning at most first edges

Backward pagination argument for returning at most last edges

Backward pagination argument for returning at most last edges

Filter by fields

Sort argument for data ordering

Page number for displaying

Filter by fields

Mutation

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

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

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.

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.

Filter by fields

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

Filter by fields

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

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

Filter by fields

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

Filter by fields

Objects

CreateManyExamplePayload

Created document ID

Created documents

Count of all documents created

CreateOneExamplePayload

Created document ID

Created document

Example

ExampleConnection

A connection to a list of items.

Total object count.

Information to aid in pagination.

Information to aid in pagination.

ExampleEdge

An edge in a connection.

The item at the end of the edge

A cursor for use in pagination

ExamplePagination

List of items with pagination.

Total object count.

Array of objects.

Information to aid in pagination.

PageInfo

Information about pagination in a connection.

When paginating forwards, are there more items?

When paginating backwards, are there more items?

When paginating backwards, the cursor to continue.

When paginating forwards, the cursor to continue.

PaginationInfo

RemoveByIdExamplePayload

Removed document ID

Removed document

RemoveManyExamplePayload

Affected documents number

RemoveOneExamplePayload

Removed document ID

Removed document

UpdateByIdExamplePayload

Updated document ID

Updated document

UpdateManyExamplePayload

Affected documents number

UpdateOneExamplePayload

Updated document ID

Updated document

Inputs

CreateManyExampleInput

CreateOneExampleInput

ExampleSearch

String or Regular Expression

field to apply regular expression

FilterExampleInput

List of indexed fields that can be filtered via operators.

FilterFindManyExampleInput

List of indexed fields that can be filtered via operators.

Search by String or Regular Expression

FilterFindOneExampleInput

List of indexed fields that can be filtered via operators.

FilterRemoveManyExampleInput

List of indexed fields that can be filtered via operators.

FilterRemoveOneExampleInput

List of indexed fields that can be filtered via operators.

FilterUpdateManyExampleInput

List of indexed fields that can be filtered via operators.

FilterUpdateOneExampleInput

List of indexed fields that can be filtered via operators.

OperatorsFilterExampleInput

For performance reason this type contains only indexed fields.

OperatorsFilterFindManyExampleInput

For performance reason this type contains only indexed fields.

OperatorsFilterFindOneExampleInput

For performance reason this type contains only indexed fields.

OperatorsFilterRemoveManyExampleInput

For performance reason this type contains only indexed fields.

OperatorsFilterRemoveOneExampleInput

For performance reason this type contains only indexed fields.

OperatorsFilterUpdateManyExampleInput

For performance reason this type contains only indexed fields.

OperatorsFilterUpdateOneExampleInput

For performance reason this type contains only indexed fields.

UpdateByIdExampleInput

UpdateManyExampleInput

UpdateOneExampleInput

_idOperatorsFilterExampleInput

_idOperatorsFilterFindManyExampleInput

_idOperatorsFilterFindOneExampleInput

_idOperatorsFilterRemoveManyExampleInput

_idOperatorsFilterRemoveOneExampleInput

_idOperatorsFilterUpdateManyExampleInput

_idOperatorsFilterUpdateOneExampleInput

Enums

SortConnectionExampleEnum

SortFindByIdsExampleInput

SortFindManyExampleInput

SortFindOneExampleInput

SortRemoveOneExampleInput

SortUpdateManyExampleInput

SortUpdateOneExampleInput

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

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago