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,100 @@
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 registerUser() {
const {
config: {
labels: {
singular,
plural,
},
fields,
},
} = this.User;
const singularLabel = formatName(singular);
const pluralLabel = formatName(plural);
this.User.graphQL = {};
this.User.graphQL.type = this.buildObjectType(
singularLabel,
fields,
singularLabel,
{
id: { type: GraphQLString },
},
);
this.User.graphQL.whereInputType = this.buildWhereInputType(
singularLabel,
fields,
singularLabel,
);
this.User.graphQL.mutationInputType = this.buildMutationInputType(
singularLabel,
fields,
singularLabel,
);
this.Query.fields[singularLabel] = {
type: this.User.graphQL.type,
args: {
id: { type: GraphQLString },
locale: { type: this.types.localeInputType },
fallbackLocale: { type: this.types.fallbackLocaleInputType },
},
resolve: getFindByID(this.config, this.User),
};
this.Query.fields[pluralLabel] = {
type: buildPaginatedListType(pluralLabel, this.User.graphQL.type),
args: {
where: { type: this.User.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.User),
};
this.Mutation.fields[`create${singularLabel}`] = {
type: this.User.graphQL.type,
args: {
data: { type: this.User.graphQL.mutationInputType },
},
resolve: getCreate(this.config, this.User),
};
this.Mutation.fields[`update${singularLabel}`] = {
type: this.User.graphQL.type,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
data: { type: this.User.graphQL.mutationInputType },
},
resolve: getUpdate(this.config, this.User),
};
this.Mutation.fields[`delete${singularLabel}`] = {
type: this.User.graphQL.type,
args: {
id: { type: new GraphQLNonNull(GraphQLString) },
},
resolve: getDelete(this.User),
};
}
module.exports = registerUser;

View File

@@ -12,20 +12,20 @@ function registerUser() {
this.config.user.fields.push(...baseUserFields);
const userSchema = buildCollectionSchema(this.config.user, this.config);
userSchema.plugin(passportLocalMongoose, { usernameField: this.config.user.auth.useAsUsername });
this.User = mongoose.model(this.config.user.labels.singular, userSchema);
this.User = {
config: this.config.user,
model: mongoose.model(this.config.user.labels.singular, userSchema),
};
passport.use(this.User.createStrategy());
passport.use(jwtStrategy(this.User, this.config));
passport.serializeUser(this.User.serializeUser());
passport.deserializeUser(this.User.deserializeUser());
passport.use(this.User.model.createStrategy());
passport.use(jwtStrategy(this.User.model, this.config));
passport.serializeUser(this.User.model.serializeUser());
passport.deserializeUser(this.User.model.deserializeUser());
passport.use(new AnonymousStrategy.Strategy());
this.router.use(authRoutes(this.config, this.User));
this.router.use(authRoutes(this.config, this.User.model));
this.router.use(collectionRoutes({
model: this.User,
config: this.config.user,
}));
this.router.use(collectionRoutes(this.User));
}
module.exports = registerUser;