passes res to graphql context in order to login via gql

This commit is contained in:
James
2020-07-09 21:58:11 -04:00
parent 41373f5e16
commit 26f4e7303e
18 changed files with 46 additions and 42 deletions

View File

@@ -13,10 +13,10 @@ const formatConfigNames = (results, configs) => {
return formattedResults;
};
const policyResolver = config => async (_, __, context) => {
const policyResolver = (config) => async (_, __, context) => {
const options = {
config,
req: context,
req: context.req,
};
let policyResults = await access(options);

View File

@@ -7,7 +7,7 @@ const forgotPasswordResolver = (config, collection, email) => async (_, args, co
collection,
data: args,
email,
req: context,
req: context.req,
};
await forgotPassword(options);

View File

@@ -4,7 +4,7 @@ const { init } = require('../../operations');
const initResolver = ({ Model }) => async (_, __, context) => {
const options = {
Model,
req: context,
req: context.req,
};
const result = await init(options);

View File

@@ -9,7 +9,8 @@ const loginResolver = (config, collection) => async (_, args, context) => {
email: args.email,
password: args.password,
},
req: context,
req: context.req,
res: context.res,
};
const token = await login(options);

View File

@@ -1,5 +1,5 @@
const { me } = require('../../operations');
const meResolver = (config) => async (_, __, context) => me({ req: context, config });
const meResolver = (config) => async (_, __, context) => me({ req: context.req, config });
module.exports = meResolver;

View File

@@ -10,7 +10,8 @@ const refreshResolver = (config, collection) => async (_, __, context) => {
config,
collection,
token,
req: context,
req: context.req,
res: context.res,
};
const result = await refresh(options);

View File

@@ -7,16 +7,16 @@ const registerResolver = (config, collection) => async (_, args, context) => {
collection,
data: args.data,
depth: 0,
req: context,
req: context.req,
};
if (args.locale) {
context.locale = args.locale;
context.req.locale = args.locale;
options.locale = args.locale;
}
if (args.fallbackLocale) {
context.fallbackLocale = args.fallbackLocale;
context.req.fallbackLocale = args.fallbackLocale;
options.fallbackLocale = args.fallbackLocale;
}

View File

@@ -2,16 +2,15 @@
const { resetPassword } = require('../../operations');
const resetPasswordResolver = (config, collection) => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const options = {
collection,
config,
data: args,
req: context,
req: context.req,
api: 'GraphQL',
user: context.user,
};
const token = await resetPassword(options);

View File

@@ -2,8 +2,8 @@
const { update } = require('../../operations');
const updateResolver = ({ Model, config }) => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const options = {
config,
@@ -11,7 +11,7 @@ const updateResolver = ({ Model, config }) => async (_, args, context) => {
data: args.data,
id: args.id,
depth: 0,
req: context,
req: context.req,
};
const user = await update(options);

View File

@@ -1,16 +1,16 @@
/* eslint-disable no-param-reassign */
const { create } = require('../../operations');
const createResolver = collection => async (_, args, context) => {
const createResolver = (collection) => async (_, args, context) => {
if (args.locale) {
context.locale = args.locale;
context.req.locale = args.locale;
}
const options = {
config: collection.config,
Model: collection.Model,
data: args.data,
req: context,
req: context.req,
};
const result = await create(options);

View File

@@ -1,15 +1,15 @@
/* eslint-disable no-param-reassign */
const { deleteQuery } = require('../../operations');
const deleteResolver = collection => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
const deleteResolver = (collection) => async (_, args, context) => {
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const options = {
config: collection.config,
Model: collection.Model,
id: args.id,
req: context,
req: context.req,
};
const result = await deleteQuery(options);

View File

@@ -2,8 +2,8 @@
const { find } = require('../../operations');
const findResolver = (collection) => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const options = {
config: collection.config,
@@ -12,7 +12,7 @@ const findResolver = (collection) => async (_, args, context) => {
limit: args.limit,
page: args.page,
sort: args.sort,
req: context,
req: context.req,
};
const results = await find(options);

View File

@@ -2,14 +2,14 @@
const { findByID } = require('../../operations');
const findByIDResolver = (collection) => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const options = {
config: collection.config,
Model: collection.Model,
id: args.id,
req: context,
req: context.req,
};
const result = await findByID(options);

View File

@@ -1,9 +1,9 @@
/* eslint-disable no-param-reassign */
const { update } = require('../../operations');
const updateResolver = collection => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
const updateResolver = (collection) => async (_, args, context) => {
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const options = {
config: collection.config,
@@ -11,7 +11,7 @@ const updateResolver = collection => async (_, args, context) => {
data: args.data,
id: args.id,
depth: 0,
req: context,
req: context.req,
};
const result = await update(options);

View File

@@ -2,8 +2,8 @@
const { findOne } = require('../../operations');
const findOneResolver = (Model, config) => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const { slug } = config;
@@ -12,7 +12,7 @@ const findOneResolver = (Model, config) => async (_, args, context) => {
config,
slug,
depth: 0,
req: context,
req: context.req,
};
const result = await findOne(options);

View File

@@ -2,8 +2,8 @@
const { update } = require('../../operations');
const updateResolver = (Model, config) => async (_, args, context) => {
if (args.locale) context.locale = args.locale;
if (args.fallbackLocale) context.fallbackLocale = args.fallbackLocale;
if (args.locale) context.req.locale = args.locale;
if (args.fallbackLocale) context.req.fallbackLocale = args.fallbackLocale;
const { slug } = config;
@@ -13,7 +13,7 @@ const updateResolver = (Model, config) => async (_, args, context) => {
data: args.data,
slug,
depth: 0,
req: context,
req: context.req,
};
const result = await update(options);

View File

@@ -14,9 +14,11 @@ const errorHandler = require('./errorHandler');
const { access } = require('../auth/graphql/resolvers');
class GraphQL {
constructor(init) {
constructor(init, req, res) {
Object.assign(this, init);
this.init = this.init.bind(this);
this.req = req;
this.res = res;
this.types = {
blockTypes: {},
@@ -94,6 +96,7 @@ class GraphQL {
return response;
},
extensions,
context: { req: this.req, res: this.res },
});
}
}

View File

@@ -58,7 +58,7 @@ class Payload {
this.router.use(
this.config.routes.graphQL,
identifyAPI('GraphQL'),
new GraphQL(this).init(),
(req, res) => new GraphQL(this, req, res).init()(req, res),
);
this.router.get(this.config.routes.graphQLPlayground, graphQLPlayground({