extracts resolvers to be able to reuse them
This commit is contained in:
19
src/collections/graphql/resolvers/getFind.js
Normal file
19
src/collections/graphql/resolvers/getFind.js
Normal 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;
|
||||
17
src/collections/graphql/resolvers/getFindByID.js
Normal file
17
src/collections/graphql/resolvers/getFindByID.js
Normal 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;
|
||||
7
src/collections/graphql/resolvers/index.js
Normal file
7
src/collections/graphql/resolvers/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const getFindByID = require('./getFindByID');
|
||||
const getFind = require('./getFind');
|
||||
|
||||
module.exports = {
|
||||
getFindByID,
|
||||
getFind,
|
||||
};
|
||||
@@ -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),
|
||||
};
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user