Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// - - - - - - Plugins - - - - - -
// User roles
const role = require('mongoose-role')
UserSchema.plugin(role, {
roles: ['public', 'user', 'admin'],
accessLevels: {
'public': ['public', 'user', 'admin'],
'anon': ['public'],
'user': ['user', 'admin'],
'admin': ['admin']
}
})
const User = mongoose.model('User', UserSchema)
const UserTC = composeWithMongoose(User)
module.exports = { User, UserTC }
const role = require('mongoose-role')
UserSchema.plugin(role, {
roles: ['public', 'user', 'admin'],
accessLevels: {
'public': ['public', 'user', 'admin'],
'anon': ['public'],
'user': ['user', 'admin'],
'admin': ['admin']
}
})
// Auto timestamps
const timestamps = require('mongoose-timestamp')
UserSchema.plugin(timestamps);
const User = mongoose.model('User', UserSchema)
const UserTC = composeWithMongoose(User)
const Authen = mongoose.model('Authen', AuthenSchema)
module.exports = { User, UserTC, Authen }
Object.keys(store.models).forEach(key => {
const mongooseModel = store.models[key] as any; // mongooseModel
const modelName = camelcase(key);
const modelComposition = composeWithMongoose(mongooseModel, {}); // Mongoose to GraphQL
compositions[modelName] = modelComposition;
Object.keys(mongooseModel.schema.paths)
.filter(k => k !== '__v')
.forEach(p => {
const property = mongooseModel.schema.paths[p];
if (property.instance === 'String') {
modelComposition.setResolver(
'findMany',
modelComposition.getResolver('findMany').addFilterArg({
name: property.path + '_regex',
type: 'String',
description: 'Search by regExp',
query: (rawQuery, value) => {
const { Plant, Garden, Herbarium, Museum, Report } = context
const defaultTypeArgs = {
resolvers: {
findMany: {
sort: true,
skip: true,
limit: {
defaultValue: 100
}
}
}
}
const ReportTC = composeWithMongoose(Report)
const PlantTC = composeWithMongoose(Plant, defaultTypeArgs)
const HerbariumTC = composeWithMongoose(Herbarium, defaultTypeArgs)
const GardenTC = composeWithMongoose(Garden, defaultTypeArgs)
const MuseumTC = composeWithMongoose(Museum, defaultTypeArgs)
const modelsTC = { PlantTC, GardenTC, MuseumTC, HerbariumTC }
modelsTC.PlantSearchResultItemType = require('./types/PlantSearchResultItem.type')(modelsTC)
// setup resolver
require('./resolvers')(modelsTC)
const checkPermission = (resolvers) => {
Object.keys(resolvers).forEach((k) => {
resolvers[k] = resolvers[k].wrapResolve(next => (rp) => { // eslint-disable-line
// rp = resolveParams = { source, args, context, info }
if (!rp.context.isAuth) {
throw new Error('You should be admin, to have access to this action.')
const defaultTypeArgs = {
resolvers: {
findMany: {
sort: true,
skip: true,
limit: {
defaultValue: 100
}
}
}
}
const ReportTC = composeWithMongoose(Report)
const PlantTC = composeWithMongoose(Plant, defaultTypeArgs)
const HerbariumTC = composeWithMongoose(Herbarium, defaultTypeArgs)
const GardenTC = composeWithMongoose(Garden, defaultTypeArgs)
const MuseumTC = composeWithMongoose(Museum, defaultTypeArgs)
const modelsTC = { PlantTC, GardenTC, MuseumTC, HerbariumTC }
modelsTC.PlantSearchResultItemType = require('./types/PlantSearchResultItem.type')(modelsTC)
// setup resolver
require('./resolvers')(modelsTC)
const checkPermission = (resolvers) => {
Object.keys(resolvers).forEach((k) => {
resolvers[k] = resolvers[k].wrapResolve(next => (rp) => { // eslint-disable-line
// rp = resolveParams = { source, args, context, info }
if (!rp.context.isAuth) {
throw new Error('You should be admin, to have access to this action.')
}
return next(rp)
module.exports = (context) => {
const { Plant, Garden, Herbarium, Museum, Report } = context
const defaultTypeArgs = {
resolvers: {
findMany: {
sort: true,
skip: true,
limit: {
defaultValue: 100
}
}
}
}
const ReportTC = composeWithMongoose(Report)
const PlantTC = composeWithMongoose(Plant, defaultTypeArgs)
const HerbariumTC = composeWithMongoose(Herbarium, defaultTypeArgs)
const GardenTC = composeWithMongoose(Garden, defaultTypeArgs)
const MuseumTC = composeWithMongoose(Museum, defaultTypeArgs)
const modelsTC = { PlantTC, GardenTC, MuseumTC, HerbariumTC }
modelsTC.PlantSearchResultItemType = require('./types/PlantSearchResultItem.type')(modelsTC)
// setup resolver
require('./resolvers')(modelsTC)
const checkPermission = (resolvers) => {
Object.keys(resolvers).forEach((k) => {
resolvers[k] = resolvers[k].wrapResolve(next => (rp) => { // eslint-disable-line
// rp = resolveParams = { source, args, context, info }
if (!rp.context.isAuth) {
resolvers: {
findMany: {
sort: true,
skip: true,
limit: {
defaultValue: 100
}
}
}
}
const ReportTC = composeWithMongoose(Report)
const PlantTC = composeWithMongoose(Plant, defaultTypeArgs)
const HerbariumTC = composeWithMongoose(Herbarium, defaultTypeArgs)
const GardenTC = composeWithMongoose(Garden, defaultTypeArgs)
const MuseumTC = composeWithMongoose(Museum, defaultTypeArgs)
const modelsTC = { PlantTC, GardenTC, MuseumTC, HerbariumTC }
modelsTC.PlantSearchResultItemType = require('./types/PlantSearchResultItem.type')(modelsTC)
// setup resolver
require('./resolvers')(modelsTC)
const checkPermission = (resolvers) => {
Object.keys(resolvers).forEach((k) => {
resolvers[k] = resolvers[k].wrapResolve(next => (rp) => { // eslint-disable-line
// rp = resolveParams = { source, args, context, info }
if (!rp.context.isAuth) {
throw new Error('You should be admin, to have access to this action.')
}
return next(rp)
})
const defaultTypeArgs = {
resolvers: {
findMany: {
sort: true,
skip: true,
limit: {
defaultValue: 100
}
}
}
}
const ReportTC = composeWithMongoose(Report)
const PlantTC = composeWithMongoose(Plant, defaultTypeArgs)
const HerbariumTC = composeWithMongoose(Herbarium, defaultTypeArgs)
const GardenTC = composeWithMongoose(Garden, defaultTypeArgs)
const MuseumTC = composeWithMongoose(Museum, defaultTypeArgs)
const modelsTC = { PlantTC, GardenTC, MuseumTC, HerbariumTC }
modelsTC.PlantSearchResultItemType = require('./types/PlantSearchResultItem.type')(modelsTC)
// setup resolver
require('./resolvers')(modelsTC)
const checkPermission = (resolvers) => {
Object.keys(resolvers).forEach((k) => {
resolvers[k] = resolvers[k].wrapResolve(next => (rp) => { // eslint-disable-line
// rp = resolveParams = { source, args, context, info }
if (!rp.context.isAuth) {
throw new Error('You should be admin, to have access to this action.')
}
dob: String,
createdAt: Date,
updatedAt: Date,
});
import { composeType, composeField, composeResolve, composeInterface, composeStorage } from 'graphql-compose';
import { description, only, remove, restrict, add } from 'graphql-compose/type';
import { composeTypeFromMongoose } from 'graphql-compose-mongoose';
import { GraphQLString } from 'graphql';
//---------- TYPE MODIFICATORS
composeType('User',
composeTypeFromMongoose(UserMongooseModel),
addResolverParam('model', UserMongooseModel), // internally added by `composeTypeFromMongoose`
composeInterface('Timestable'), // internally call composeStorage.Interfaces.get('Timestable')
description('User model description'),
only(['myName', 'surname']), // get only described fields
remove(['stats', 'password']), // ... or leave others, and remove just listed here
rename({
myName: 'name',
surname: 'lastname',
}),
restrict({
hasAccess: (source, args, context, info) => {
return context.isAdmin;
},
fields: ['name', 'dob'],
}),
restrict({
gender: String,
hairColor: String,
starships: [String],
});
// set discriminator Key
CharacterSchema.set('discriminatorKey', DKey);
// create base Model
export const CharacterModel = mongoose.model('Character', CharacterSchema);
// create mongoose discriminator models
export const DroidModel = CharacterModel.discriminator(enumCharacterType.DROID, DroidSchema);
export const PersonModel = CharacterModel.discriminator(enumCharacterType.PERSON, PersonSchema);
export const CharacterDTC = composeWithMongooseDiscriminators(CharacterModel, {
schemaComposer,
});
type DroidT = any;
export const DroidTC: ObjectTypeComposer = CharacterDTC.discriminator(
DroidModel
);
type PersonT = any;
export const PersonTC: ObjectTypeComposer = CharacterDTC.discriminator(
PersonModel
);