mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 01:51:40 -05:00
Use default resolvers and mutations for instagram example
This commit is contained in:
parent
de486a607e
commit
31d0490a36
8 changed files with 27 additions and 336 deletions
|
@ -4,11 +4,9 @@ The main Comments collection definition file.
|
|||
|
||||
*/
|
||||
|
||||
import { createCollection } from 'meteor/vulcan:core';
|
||||
import { createCollection, getDefaultResolvers, getDefaultMutations } from 'meteor/vulcan:core';
|
||||
import schema from './schema.js';
|
||||
import resolvers from './resolvers.js';
|
||||
import './fragments.js';
|
||||
import mutations from './mutations.js';
|
||||
import './permissions.js';
|
||||
|
||||
const Comments = createCollection({
|
||||
|
@ -22,9 +20,9 @@ const Comments = createCollection({
|
|||
|
||||
schema,
|
||||
|
||||
resolvers,
|
||||
resolvers: getDefaultResolvers('Comments'),
|
||||
|
||||
mutations,
|
||||
mutations: getDefaultMutations('Comments'),
|
||||
|
||||
});
|
||||
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
|
||||
Define the three default mutations:
|
||||
|
||||
- new (e.g.: commentsNew(document: commentsInput) : Movie )
|
||||
- edit (e.g.: commentsEdit(documentId: String, set: commentsInput, unset: commentsUnset) : Movie )
|
||||
- remove (e.g.: commentsRemove(documentId: String) : Movie )
|
||||
|
||||
Each mutation has:
|
||||
|
||||
- A name
|
||||
- A check function that takes the current user and (optionally) the document affected
|
||||
- The actual mutation
|
||||
|
||||
*/
|
||||
|
||||
import { newMutation, editMutation, removeMutation, Utils } from 'meteor/vulcan:core';
|
||||
import Users from 'meteor/vulcan:users';
|
||||
|
||||
const mutations = {
|
||||
|
||||
new: {
|
||||
|
||||
name: 'commentsNew',
|
||||
|
||||
check(user) {
|
||||
if (!user) return false;
|
||||
return Users.canDo(user, 'comments.new');
|
||||
},
|
||||
|
||||
mutation(root, {document}, context) {
|
||||
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
return newMutation({
|
||||
collection: context.Comments,
|
||||
document: document,
|
||||
currentUser: context.currentUser,
|
||||
validate: true,
|
||||
context,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
edit: {
|
||||
|
||||
name: 'commentsEdit',
|
||||
|
||||
check(user, document) {
|
||||
if (!user || !document) return false;
|
||||
return Users.canDo(user, `comments.edit.all`) || Users.owns(user, document) && Users.canDo(user, 'comments.edit.own');
|
||||
},
|
||||
|
||||
mutation(root, {documentId, set, unset}, context) {
|
||||
|
||||
const document = context.Comments.findOne(documentId);
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
return editMutation({
|
||||
collection: context.Comments,
|
||||
documentId: documentId,
|
||||
set: set,
|
||||
unset: unset,
|
||||
currentUser: context.currentUser,
|
||||
validate: true,
|
||||
context,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
remove: {
|
||||
|
||||
name: 'commentsRemove',
|
||||
|
||||
check(user, document) {
|
||||
if (!user || !document) return false;
|
||||
return Users.owns(user, document) ? Users.canDo(user, 'comments.remove.own') : Users.canDo(user, `comments.remove.all`);
|
||||
},
|
||||
|
||||
mutation(root, {documentId}, context) {
|
||||
|
||||
const document = context.Comments.findOne(documentId);
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
return removeMutation({
|
||||
collection: context.Comments,
|
||||
documentId: documentId,
|
||||
currentUser: context.currentUser,
|
||||
validate: true,
|
||||
context,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
export default mutations;
|
|
@ -1,60 +0,0 @@
|
|||
/*
|
||||
|
||||
Three resolvers are defined:
|
||||
|
||||
- list (e.g.: commentsList(terms: JSON, offset: Int, limit: Int) )
|
||||
- single (e.g.: commentsSingle(_id: String) )
|
||||
- listTotal (e.g.: commentsTotal )
|
||||
|
||||
*/
|
||||
|
||||
import { addGraphQLResolvers } from 'meteor/vulcan:core';
|
||||
|
||||
// basic list, single, and total query resolvers
|
||||
const resolvers = {
|
||||
|
||||
list: {
|
||||
|
||||
name: 'commentsList',
|
||||
|
||||
resolver(root, {terms = {}}, context, info) {
|
||||
let {selector, options} = context.Comments.getParameters(terms, {}, context.currentUser);
|
||||
return context.Comments.find(selector, options).fetch();
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
single: {
|
||||
|
||||
name: 'commentsSingle',
|
||||
|
||||
resolver(root, {documentId}, context) {
|
||||
const document = context.Comments.findOne({_id: documentId});
|
||||
return context.Users.restrictViewableFields(context.currentUser, context.Comments, document);
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
total: {
|
||||
|
||||
name: 'commentsTotal',
|
||||
|
||||
resolver(root, {terms = {}}, context) {
|
||||
const {selector, options} = context.Comments.getParameters(terms, {}, context.currentUser);
|
||||
return context.Comments.find(selector, options).count();
|
||||
},
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// add the "user" resolver for the Comment type separately
|
||||
const commentUserResolver = {
|
||||
Comment: {
|
||||
user(comment, args, context) {
|
||||
return context.Users.findOne({ _id: comment.userId }, { fields: context.Users.getViewableFields(context.currentUser, context.Users) });
|
||||
},
|
||||
},
|
||||
};
|
||||
addGraphQLResolvers(commentUserResolver);
|
||||
|
||||
export default resolvers;
|
|
@ -25,7 +25,13 @@ const schema = {
|
|||
type: String,
|
||||
optional: true,
|
||||
viewableBy: ['guests'],
|
||||
resolveAs: 'user: User', // resolve as "user" on the client
|
||||
resolveAs: {
|
||||
fieldName: 'user',
|
||||
typeName: 'User',
|
||||
resolver(comment, args, context) {
|
||||
return context.Users.findOne({ _id: comment.userId }, { fields: context.Users.getViewableFields(context.currentUser, context.Users) });
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// custom properties
|
||||
|
|
|
@ -4,11 +4,9 @@ The main Pics collection definition file.
|
|||
|
||||
*/
|
||||
|
||||
import { createCollection } from 'meteor/vulcan:core';
|
||||
import { createCollection, getDefaultResolvers, getDefaultMutations } from 'meteor/vulcan:core';
|
||||
import schema from './schema.js';
|
||||
import resolvers from './resolvers.js';
|
||||
import './fragments.js';
|
||||
import mutations from './mutations.js';
|
||||
import './permissions.js';
|
||||
|
||||
const Pics = createCollection({
|
||||
|
@ -19,9 +17,9 @@ const Pics = createCollection({
|
|||
|
||||
schema,
|
||||
|
||||
resolvers,
|
||||
resolvers: getDefaultResolvers('Pics'),
|
||||
|
||||
mutations,
|
||||
mutations: getDefaultMutations('Pics'),
|
||||
|
||||
});
|
||||
|
||||
|
|
|
@ -1,100 +0,0 @@
|
|||
/*
|
||||
|
||||
Define the three default mutations:
|
||||
|
||||
- new (e.g.: picsNew(document: picsInput) : Movie )
|
||||
- edit (e.g.: picsEdit(documentId: String, set: picsInput, unset: picsUnset) : Movie )
|
||||
- remove (e.g.: picsRemove(documentId: String) : Movie )
|
||||
|
||||
Each mutation has:
|
||||
|
||||
- A name
|
||||
- A check function that takes the current user and (optionally) the document affected
|
||||
- The actual mutation
|
||||
|
||||
*/
|
||||
|
||||
import { newMutation, editMutation, removeMutation, Utils } from 'meteor/vulcan:core';
|
||||
import Users from 'meteor/vulcan:users';
|
||||
|
||||
const mutations = {
|
||||
|
||||
new: {
|
||||
|
||||
name: 'picsNew',
|
||||
|
||||
check(user) {
|
||||
if (!user) return false;
|
||||
return Users.canDo(user, 'pics.new');
|
||||
},
|
||||
|
||||
mutation(root, {document}, context) {
|
||||
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
return newMutation({
|
||||
collection: context.Pics,
|
||||
document: document,
|
||||
currentUser: context.currentUser,
|
||||
validate: true,
|
||||
context,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
edit: {
|
||||
|
||||
name: 'picsEdit',
|
||||
|
||||
check(user, document) {
|
||||
if (!user || !document) return false;
|
||||
return Users.canDo(user, `pics.edit.all`) || Users.owns(user, document) && Users.canDo(user, 'pics.edit.own');
|
||||
},
|
||||
|
||||
mutation(root, {documentId, set, unset}, context) {
|
||||
|
||||
const document = context.Pics.findOne(documentId);
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
return editMutation({
|
||||
collection: context.Pics,
|
||||
documentId: documentId,
|
||||
set: set,
|
||||
unset: unset,
|
||||
currentUser: context.currentUser,
|
||||
validate: true,
|
||||
context,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
remove: {
|
||||
|
||||
name: 'picsRemove',
|
||||
|
||||
check(user, document) {
|
||||
if (!user || !document) return false;
|
||||
return Users.owns(user, document) ? Users.canDo(user, 'pics.remove.own') : Users.canDo(user, `pics.remove.all`);
|
||||
},
|
||||
|
||||
mutation(root, {documentId}, context) {
|
||||
|
||||
const document = context.Pics.findOne(documentId);
|
||||
Utils.performCheck(this.check, context.currentUser, document);
|
||||
|
||||
return removeMutation({
|
||||
collection: context.Pics,
|
||||
documentId: documentId,
|
||||
currentUser: context.currentUser,
|
||||
validate: true,
|
||||
context,
|
||||
});
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
export default mutations;
|
|
@ -1,63 +0,0 @@
|
|||
/*
|
||||
|
||||
Three resolvers are defined:
|
||||
|
||||
- list (e.g.: picsList(terms: JSON, offset: Int, limit: Int) )
|
||||
- single (e.g.: picsSingle(_id: String) )
|
||||
- listTotal (e.g.: picsTotal )
|
||||
|
||||
*/
|
||||
|
||||
import { addGraphQLResolvers } from 'meteor/vulcan:core';
|
||||
|
||||
// basic list, single, and total query resolvers
|
||||
const resolvers = {
|
||||
|
||||
list: {
|
||||
|
||||
name: 'picsList',
|
||||
|
||||
resolver(root, {terms = {}}, context, info) {
|
||||
let {selector, options} = context.Pics.getParameters(terms, {}, context.currentUser);
|
||||
return context.Pics.find(selector, options).fetch();
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
single: {
|
||||
|
||||
name: 'picsSingle',
|
||||
|
||||
resolver(root, {documentId}, context) {
|
||||
const document = context.Pics.findOne({_id: documentId});
|
||||
return context.Users.restrictViewableFields(context.currentUser, context.Pics, document);
|
||||
},
|
||||
|
||||
},
|
||||
|
||||
total: {
|
||||
|
||||
name: 'picsTotal',
|
||||
|
||||
resolver(root, {terms = {}}, context) {
|
||||
const {selector, options} = context.Pics.getParameters(terms, {}, context.currentUser);
|
||||
return context.Pics.find(selector, options).count();
|
||||
},
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// add the "user" resolver for the Pic type separately
|
||||
const picUserResolver = {
|
||||
Pic: {
|
||||
user(pic, args, context) {
|
||||
return context.Users.findOne({ _id: pic.userId }, { fields: context.Users.getViewableFields(context.currentUser, context.Users) });
|
||||
},
|
||||
commentsCount(pic, args, context) {
|
||||
return context.Comments.find({picId: pic._id}).count();
|
||||
}
|
||||
},
|
||||
};
|
||||
addGraphQLResolvers(picUserResolver);
|
||||
|
||||
export default resolvers;
|
|
@ -27,7 +27,13 @@ const schema = {
|
|||
type: String,
|
||||
optional: true,
|
||||
viewableBy: ['guests'],
|
||||
resolveAs: 'user: User', // resolve this field as "user" on the client
|
||||
resolveAs: {
|
||||
fieldName: 'user',
|
||||
typeName: 'User',
|
||||
resolver(pic, args, context) {
|
||||
return context.Users.findOne({ _id: pic.userId }, { fields: context.Users.getViewableFields(context.currentUser, context.Users) });
|
||||
},
|
||||
}
|
||||
},
|
||||
|
||||
// custom properties
|
||||
|
@ -62,7 +68,13 @@ const schema = {
|
|||
optional: true,
|
||||
viewableBy: ['guests'],
|
||||
hidden: true,
|
||||
resolveAs: 'commentsCount: Float' // resolve as commentCount on the client
|
||||
resolveAs: {
|
||||
fieldName: 'commentsCount',
|
||||
typeName: 'Float',
|
||||
resolver(pic, args, context) {
|
||||
return context.Comments.find({picId: pic._id}).count();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue