From e4433b40ae3134783e2452ce6c8b7527be931e54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20Safa=20SA=C4=9ELIK?= <35993436+ogeday26@users.noreply.github.com> Date: Mon, 30 Mar 2020 06:53:40 +0300 Subject: [PATCH] mutation issue has been solved. --- src/mutations/User.js | 69 ++++++++++++++++++------------------------ src/mutations/index.js | 15 +++++---- 2 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/mutations/User.js b/src/mutations/User.js index 307afca..7f4247a 100644 --- a/src/mutations/User.js +++ b/src/mutations/User.js @@ -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 }; diff --git a/src/mutations/index.js b/src/mutations/index.js index 16bf8ee..37a8472 100644 --- a/src/mutations/index.js +++ b/src/mutations/index.js @@ -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;