Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
@risingstack/graffiti-mongoose
Advanced tools
Readme
Mongoose (MongoDB) adapter for GraphQL.
graffiti-mongoose
generates GraphQL
types and schemas from your existing mongoose
models, that's how simple it is. The generated schema is compatible with Relay.
For quick jump check out the Usage section.
npm install graphql @risingstack/graffiti-mongoose --save
Check out the /example folder.
cd graffiti-mongoose
npm install # install dependencies in the main folder
cd example
npm install # install dependencies in the example folder
npm start # run the example application and open your browser: http://localhost:8080
This adapter is written in ES6
and ES7
with Babel but it's published as transpiled ES5
JavaScript code to npm
, which means you don't need ES7
support in your application to run it.
Example queries can be found in the example folder.
import mongoose from 'mongoose';
const UserSchema = new mongoose.Schema({
name: {
type: String,
// field description
description: 'the full name of the user'
},
hiddenField: {
type: Date,
default: Date.now,
// the field is hidden, not available in GraphQL
hidden: true
},
age: {
type: Number,
indexed: true
},
friends: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
});
const User = mongoose.model('User', UserSchema);
export default User;
import {getSchema} from '@risingstack/graffiti-mongoose';
import graphql from 'graphql';
import User from './User';
const options = {
mutation: false, // mutation fields can be disabled
allowMongoIDMutation: false // mutation of mongo _id can be enabled
};
const schema = getSchema([User], options);
const query = `{
users(age: 28) {
name
friends(first: 2) {
edges {
cursor
node {
name
age
}
}
pageInfo {
startCursor
endCursor
hasPreviousPage
hasNextPage
}
}
}
}`;
graphql(schema, query)
.then((result) => {
console.log(result);
});
user
users
!ID
, and returns a Node
Which means, you are able to filter like below, if the age is indexed in your mongoose model:
users(age: 19) {}
user(id: "mongoId1") {}
user(id: "relayId") {}
users(id: ["mongoId", "mongoId2"]) {}
users(id: ["relayId1", "relayId2"]) {}
addUser
updateUser
deleteUser
Examples:
mutation addX {
addUser(input: {name: "X", age: 11, clientMutationId: "1"}) {
changedUserEdge {
node {
id
name
}
}
}
}
mutation updateX {
updateUser(input: {id: "id=", age: 10, clientMutationId: "2"}) {
changedUser {
id
name
age
}
}
}
mutation deleteX {
deleteUser(input: {id: "id=", clientMutationId: "3"}) {
ok
}
}
You can specify pre- and post-resolve hooks on fields in order to manipulate arguments and data passed in to the database resolve function, and returned by the GraphQL resolve function.
You can add hooks to type fields and query fields (singular & plural queries, mutations) too.
By passing arguments to the next
function, you can modify the parameters of the next hook or the return value of the resolve
function.
Examples:
viewer
, singular
, plural
, mutation
)const hooks = {
viewer: {
pre: (next, root, args, request) => {
// authorize the logged in user based on the request
authorize(request);
next();
},
post: (next, value) => {
console.log(value);
next();
}
},
// singular: {
// pre: (next, root, args, context) => next(),
// post: (next, value, args, context) => next()
// },
// plural: {
// pre: (next, root, args, context) => next(),
// post: (next, value, args, context) => next()
// },
// mutation: {
// pre: (next, args, context) => next(),
// post: (next, value, args, context) => next()
// }
};
const schema = getSchema([User], {hooks});
const UserSchema = new mongoose.Schema({
name: {
type: String,
hooks: {
pre: (next, root, args, request) => {
// authorize the logged in user based on the request
// throws error if the user has no right to request the user names
authorize(request);
next();
},
// manipulate response
post: [
(next, name) => next(`${name} first hook`),
(next, name) => next(`${name} & second hook`)
]
}
}
});
query UsersQuery {
viewer {
users(first: 1) {
edges {
node {
name
}
}
}
}
}
{
"data": {
"viewer": {
"users": {
"edges": [
{
"node": {
"name": "User0 first hook & second hook"
}
}
]
}
}
}
}
npm test
Please read the CONTRIBUTING.md file.
FAQs
Mongoose adapter for graffiti (Node.js GraphQL ORM)
We found that @risingstack/graffiti-mongoose demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).