Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'graphql-compose-mongoose' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
// - - - - - - 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
);