From 8bf3ddec291199f2330ec0d6de44cdd0d571b80b Mon Sep 17 00:00:00 2001 From: Pavel Lang Date: Sun, 14 Jan 2018 05:32:07 +0100 Subject: [PATCH] Fix errors in graphql-tools implementation --- src/data/graphql/Database/users/CreateUser.js | 46 ++++++++----------- .../graphql/Database/users/GetAllUsers.js | 26 +++++------ .../graphql/Database/users/GetLoggedInUser.js | 39 ++++++---------- .../News/reactjsnews.com/GetAllReactJSNews.js | 20 ++++---- src/server.js | 1 + 5 files changed, 56 insertions(+), 76 deletions(-) diff --git a/src/data/graphql/Database/users/CreateUser.js b/src/data/graphql/Database/users/CreateUser.js index b7e1cd21e..60bafd4dd 100644 --- a/src/data/graphql/Database/users/CreateUser.js +++ b/src/data/graphql/Database/users/CreateUser.js @@ -37,37 +37,29 @@ export const mutation = [ export const resolvers = { Mutation: { - databaseCreateUser: (parent, args) => { - async function createUser() { - // If user already exists, throw error - const lookupUser = await User.findOne({ where: { email: args.email } }); + async databaseCreateUser(parent, args) { + // If user already exists, throw error + const lookupUser = await User.findOne({ where: { email: args.email } }); - if (lookupUser) { - // eslint-disable-next-line no-throw-literal - throw 'User already exists!'; - } + if (lookupUser) { + // eslint-disable-next-line no-throw-literal + throw 'User already exists!'; + } - // Create new user with profile in database - const user = await User.create( - { - email: args.email, - profile: { - ...args.profile, - }, - }, - { - include: [{ model: UserProfile, as: 'profile' }], + // Create new user with profile in database + const user = await User.create( + { + email: args.email, + profile: { + ...args.profile, }, - ); - - return user; - } + }, + { + include: [{ model: UserProfile, as: 'profile' }], + }, + ); - return createUser() - .then(user => user) - .catch(err => { - throw err; - }); + return user; }, }, }; diff --git a/src/data/graphql/Database/users/GetAllUsers.js b/src/data/graphql/Database/users/GetAllUsers.js index 10e0137e0..3878b9051 100644 --- a/src/data/graphql/Database/users/GetAllUsers.js +++ b/src/data/graphql/Database/users/GetAllUsers.js @@ -59,30 +59,26 @@ export const queries = [ export const resolvers = { RootQuery: { - databaseGetAllUsers: () => - User.findAll({ + async databaseGetAllUsers() { + const users = await User.findAll({ include: [ { model: UserLogin, as: 'logins' }, { model: UserClaim, as: 'claims' }, { model: UserProfile, as: 'profile' }, ], - }) - .then(users => users.map(user => user)) - .catch(err => { - throw err; - }), - databaseGetUser: args => - User.findOne({ - where: { email: args.email }, + }); + return users; + }, + async databaseGetUser(parent, { email }) { + const user = await User.findOne({ + where: { email }, include: [ { model: UserLogin, as: 'logins' }, { model: UserClaim, as: 'claims' }, { model: UserProfile, as: 'profile' }, ], - }) - .then(user => user) - .catch(err => { - throw err; - }), + }); + return user; + }, }, }; diff --git a/src/data/graphql/Database/users/GetLoggedInUser.js b/src/data/graphql/Database/users/GetLoggedInUser.js index a520ae4f7..c60a2c220 100644 --- a/src/data/graphql/Database/users/GetLoggedInUser.js +++ b/src/data/graphql/Database/users/GetLoggedInUser.js @@ -9,32 +9,23 @@ export const queries = [ export const resolvers = { RootQuery: { - databaseGetLoggedInUser: context => { - async function getLoggedInUser() { - // Throw error if user is not authenticated - if (!context.user) { - // eslint-disable-next-line no-throw-literal - throw 'Unauthorized: Access is denied.'; - } - - // Create new user with profile in database - const newUser = await User.findOne({ - where: { email: context.user.email }, - include: [ - { model: UserLogin, as: 'logins' }, - { model: UserClaim, as: 'claims' }, - { model: UserProfile, as: 'profile' }, - ], - }); - - return newUser; + async databaseGetLoggedInUser(parent, args, context) { + // Throw error if user is not authenticated + if (!context.user) { + return null; } - return getLoggedInUser() - .then(user => user) - .catch(err => { - throw err; - }); + // Find logged in user from database + const dbUser = await User.findOne({ + where: { email: context.user.email }, + include: [ + { model: UserLogin, as: 'logins' }, + { model: UserClaim, as: 'claims' }, + { model: UserProfile, as: 'profile' }, + ], + }); + + return dbUser; }, }, }; diff --git a/src/data/graphql/News/reactjsnews.com/GetAllReactJSNews.js b/src/data/graphql/News/reactjsnews.com/GetAllReactJSNews.js index 6db01f500..1166e8130 100644 --- a/src/data/graphql/News/reactjsnews.com/GetAllReactJSNews.js +++ b/src/data/graphql/News/reactjsnews.com/GetAllReactJSNews.js @@ -29,18 +29,18 @@ export const queries = [ `, ]; -export const resolvers = { - RootQuery: { - reactjsGetAllNews: () => { - // React.js News Feed (RSS) - const url = - 'https://api.rss2json.com/v1/api.json' + - '?rss_url=https%3A%2F%2Freactjsnews.com%2Ffeed.xml'; +// React.js News Feed (RSS) +const url = + 'https://api.rss2json.com/v1/api.json' + + '?rss_url=https%3A%2F%2Freactjsnews.com%2Ffeed.xml'; - let items = []; - let lastFetchTask; - let lastFetchTime = new Date(1970, 0, 1); +let items = []; +let lastFetchTask; +let lastFetchTime = new Date(1970, 0, 1); +export const resolvers = { + RootQuery: { + reactjsGetAllNews() { if (lastFetchTask) { return lastFetchTask; } diff --git a/src/server.js b/src/server.js index 46b48fee3..f0ff2081b 100644 --- a/src/server.js +++ b/src/server.js @@ -102,6 +102,7 @@ app.get( // // Register API middleware // ----------------------------------------------------------------------------- +// https://github.com/graphql/express-graphql#options const graphqlMiddleware = expressGraphQL(req => ({ schema, graphiql: __DEV__,