extracts resolvers to be able to reuse them

This commit is contained in:
James
2020-04-05 21:04:47 -04:00
parent f19809bd04
commit 855ae9dea7
4 changed files with 46 additions and 17 deletions

View File

@@ -0,0 +1,19 @@
const withPolicy = require('../../../graphql/resolvers/withPolicy');
const findQuery = require('../../queries/find');
const find = ({ config, model }) => {
return withPolicy(
config.policies.read,
async (_, args) => {
const results = await findQuery({
depth: 0,
model,
query: args,
});
return results;
},
);
};
module.exports = find;

View File

@@ -0,0 +1,17 @@
const withPolicy = require('../../../graphql/resolvers/withPolicy');
const findByIDQuery = require('../../queries/findByID');
const findByID = ({ config, model }) => withPolicy(
config.policies.read,
async (_, { id }) => {
const result = await findByIDQuery({
depth: 0,
model,
id,
});
return result;
},
);
module.exports = findByID;

View File

@@ -0,0 +1,7 @@
const getFindByID = require('./getFindByID');
const getFind = require('./getFind');
module.exports = {
getFindByID,
getFind,
};

View File

@@ -11,10 +11,9 @@ const graphQLHTTP = require('express-graphql');
const getBuildObjectType = require('./schema/getBuildObjectType');
const buildWhereInputType = require('./schema/buildWhereInputType');
const formatName = require('./utilities/formatName');
const withPolicy = require('./resolvers/withPolicy');
const getLocaleStringType = require('./types/getLocaleStringType');
const getLocaleFloatType = require('./types/getLocaleFloatType');
const { find, findByID } = require('../collections/queries');
const { getFind, getFindByID } = require('../collections/graphql/resolvers');
const Query = {
name: 'Query',
@@ -62,7 +61,6 @@ function init() {
const {
config: {
policies,
fields,
labels: {
singular,
@@ -87,13 +85,7 @@ function init() {
args: {
id: { type: GraphQLString },
},
resolve: withPolicy(policies.read, async (_, { id }) => {
return findByID({
depth: 0,
model: collection.model,
id,
});
}),
resolve: getFindByID(collection),
};
Query.fields[pluralLabel] = {
@@ -118,13 +110,7 @@ function init() {
args: {
where: { type: collection.graphQLWhereInputType },
},
resolve: withPolicy(policies.read, async (_, args) => {
return find({
depth: 0,
model: collection.model,
query: args,
});
}),
resolve: getFind(collection),
};
});