diff --git a/src/collections/graphql/resolvers/getFind.js b/src/collections/graphql/resolvers/getFind.js new file mode 100644 index 0000000000..c1f5c1f7e6 --- /dev/null +++ b/src/collections/graphql/resolvers/getFind.js @@ -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; diff --git a/src/collections/graphql/resolvers/getFindByID.js b/src/collections/graphql/resolvers/getFindByID.js new file mode 100644 index 0000000000..7ff475c8b0 --- /dev/null +++ b/src/collections/graphql/resolvers/getFindByID.js @@ -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; diff --git a/src/collections/graphql/resolvers/index.js b/src/collections/graphql/resolvers/index.js new file mode 100644 index 0000000000..a7c26d8f67 --- /dev/null +++ b/src/collections/graphql/resolvers/index.js @@ -0,0 +1,7 @@ +const getFindByID = require('./getFindByID'); +const getFind = require('./getFind'); + +module.exports = { + getFindByID, + getFind, +}; diff --git a/src/graphql/init.js b/src/graphql/init.js index 71127b49f3..7bb0e7df6d 100644 --- a/src/graphql/init.js +++ b/src/graphql/init.js @@ -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), }; });