How to use graphql-compose-mongoose - 10 common examples

To help you get started, we’ve selected a few graphql-compose-mongoose examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github rabbotio / nap / examples / schema-graphql-compose / user / UserSchema.js View on Github external
// - - - - - - 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 }
github rabbotio / nap / examples / schema-minimal / UserSchema.js View on Github external
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 }
github Lambda-IT / chestnutJS / server / src / schema.ts View on Github external
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) => {
github zapkub / cu-vivid-museum-wiki / legacy / server / schema.js View on Github external
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.')
github zapkub / cu-vivid-museum-wiki / legacy / server / schema.js View on Github external
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)
github zapkub / cu-vivid-museum-wiki / legacy / server / schema.js View on Github external
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) {
github zapkub / cu-vivid-museum-wiki / legacy / server / schema.js View on Github external
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)
    })
github zapkub / cu-vivid-museum-wiki / legacy / server / schema.js View on Github external
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.')
      }
github graphql-compose / graphql-compose / src / metaApiProposal.js View on Github external
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({
github graphql-compose / graphql-compose-examples / examples / mongooseDiscriminators / models.js View on Github external
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
);