enables User and Upload graphql CRUD

This commit is contained in:
James
2020-04-12 15:54:21 -04:00
parent cfffe52e88
commit 94fd642e5c
10 changed files with 231 additions and 25 deletions

View File

@@ -0,0 +1,92 @@
const {
GraphQLString,
GraphQLNonNull,
GraphQLInt,
} = require('graphql');
const formatName = require('../../graphql/utilities/formatName');
const {
getCreate, getFind, getFindByID, getDelete, getUpdate,
} = require('../../collections/graphql/resolvers');
const buildPaginatedListType = require('../../graphql/schema/buildPaginatedListType');
function registerUpload() {
const {
config: {
labels: {
singular,
plural,
},
fields,
},
} = this.Upload;
const singularLabel = formatName(singular);
const pluralLabel = formatName(plural);
this.Upload.graphQL = {};
this.Upload.graphQL.type = this.buildObjectType(
singularLabel,
fields,
singularLabel,
{
id: { type: GraphQLString },
},
);
this.Upload.graphQL.whereInputType = this.buildWhereInputType(
singularLabel,
fields,
singularLabel,
);
this.Upload.graphQL.mutationInputType = this.buildMutationInputType(
singularLabel,
fields,
singularLabel,
);
this.Query.fields[singularLabel] = {
type: this.Upload.graphQL.type,
args: {
id: { type: GraphQLString },
locale: { type: this.types.localeInputType },
fallbackLocale: { type: this.types.fallbackLocaleInputType },
},
resolve: getFindByID(this.config, this.Upload),
};
this.Query.fields[pluralLabel] = {
type: buildPaginatedListType(pluralLabel, this.Upload.graphQL.type),
args: {
where: { type: this.Upload.graphQL.whereInputType },
locale: { type: this.types.localeInputType },
fallbackLocale: { type: this.types.fallbackLocaleInputType },
page: { type: GraphQLInt },
limit: { type: GraphQLInt },
sort: { type: GraphQLString },
},
resolve: getFind(this.config, this.Upload),
};
this.Mutation.fields[`update${singularLabel}`] = {
type: this.Upload.graphQL.type,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
data: { type: this.Upload.graphQL.mutationInputType },
},
resolve: getUpdate(this.config, this.Upload),
};
this.Mutation.fields[`delete${singularLabel}`] = {
type: this.Upload.graphQL.type,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: getDelete(this.Upload),
};
}
module.exports = registerUpload;

View File

@@ -19,17 +19,17 @@ function registerUpload() {
discriminatorKey: 'type',
});
this.Upload = mongoose.model(this.config.upload.labels.singular, uploadSchema);
this.Upload = {
model: mongoose.model(this.config.upload.labels.singular, uploadSchema),
config: this.config.upload,
};
// TODO: image type hard coded, but in the future we need some way of customizing how uploads are handled in customizable pattern
this.Upload.discriminator('image', imageSchema);
this.Upload.model.discriminator('image', imageSchema);
this.router.use(uploadRoutes(this.config, this.Upload));
this.router.use(collectionRoutes({
model: this.Upload,
config: this.config.upload,
}));
this.router.use(collectionRoutes(this.Upload));
}
module.exports = registerUpload;

View File

@@ -7,12 +7,12 @@ const uploadMiddleware = require('./middleware');
const router = express.Router();
const uploadRoutes = (config, Upload) => {
const { upload: uploadConfig } = config;
const { config: uploadConfig, model } = Upload;
router.all(`/${uploadConfig.slug}*`,
fileUpload(),
passport.authenticate('jwt', { session: false }),
uploadMiddleware(config, Upload));
uploadMiddleware(config, model));
router.route(`/${uploadConfig.slug}`)
.post(