Skip to content

Commit

Permalink
Fix errors in graphql-tools implementation (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
langpavel authored Jan 14, 2018
1 parent d7d4187 commit 7373ec4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 76 deletions.
46 changes: 19 additions & 27 deletions src/data/graphql/Database/users/CreateUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
};
26 changes: 11 additions & 15 deletions src/data/graphql/Database/users/GetAllUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
};
39 changes: 15 additions & 24 deletions src/data/graphql/Database/users/GetLoggedInUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
},
};
20 changes: 10 additions & 10 deletions src/data/graphql/News/reactjsnews.com/GetAllReactJSNews.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ app.get(
//
// Register API middleware
// -----------------------------------------------------------------------------
// https://github.com/graphql/express-graphql#options
const graphqlMiddleware = expressGraphQL(req => ({
schema,
graphiql: __DEV__,
Expand Down

0 comments on commit 7373ec4

Please sign in to comment.