Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 30 additions & 39 deletions src/mutations/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,35 @@ const signinUserInputType = new GraphQLInputObjectType({
}),
});

const mutationType = new GraphQLObjectType({
name: 'RootMutationType',
description: 'Domain API actions',
const createUser = {
description: 'Creates a new user',
type: userType,
args: {
input: { type: new GraphQLNonNull(createUserInputType) },
},
resolve: async (root, { input }) => {
const hash = await bcrypt.hash(input.password, 8);
const graph = await User.query().insertGraph({
...input,
password: hash,
});
return graph.toJSON();
},
};

fields: () => ({

createUser: {
description: 'Creates a new user',
type: userType,
args: {
input: { type: new GraphQLNonNull(createUserInputType) },
},
resolve: async (root, { input }) => {
const hash = await bcrypt.hash(input.password, 8);
const graph = await User.query().insertGraph({
...input,
password: hash,
});
return graph.toJSON();
},
},

signinUser: {
description: 'Sign in a user',
type: authResponseType,
args: {
input: { type: new GraphQLNonNull(signinUserInputType) },
},
resolve: async (root, { input }) => {
const token = await getTokenFromLogin(input);
if (!token) {
throw new AuthenticationError('Invalid credentials.');
}
return { token };
},
},

}),
});
const signinUser = {
description: 'Sign in a user',
type: authResponseType,
args: {
input: { type: new GraphQLNonNull(signinUserInputType) },
},
resolve: async (root, { input }) => {
const token = await getTokenFromLogin(input);
if (!token) {
throw new AuthenticationError('Invalid credentials.');
}
return { token };
},
};

module.exports = mutationType;
module.exports = { createUser, signinUser };
15 changes: 9 additions & 6 deletions src/mutations/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-disable global-require */
const mutations = {
User: require('./User'),
};
const mutationType = new GraphQLObjectType({
name: 'RootMutationType',
description: 'Domain API actions',

fields: () => ({
...require('./User'),
}),
});

const createMutations = (builder) => {
Object.keys(mutations).forEach((name) => {
builder.extendWithMutations(mutations[name]);
});
builder.extendWithMutations(mutationType);
};

module.exports = createMutations;