enables User and Upload graphql CRUD
This commit is contained in:
100
src/auth/graphql/register.js
Normal file
100
src/auth/graphql/register.js
Normal 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;
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user