Skip to content

Commit

Permalink
FEATURE: get constraints for arbitrary type (#8)
Browse files Browse the repository at this point in the history
* FEATURE: get constraints for arbitrary type

* @joindeed/mongoose-models-1.2.0
  • Loading branch information
dimaip authored Oct 31, 2022
1 parent 60d8d40 commit ab89d0c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,23 @@ resolve: async (parent, args, context) => {
}
}))
}
```
Additionally, if you want to make any arbitrary Prisma query not related to the GraphQL return type at all (e.g. say you want to count something and get the count with respect to the constraints),
and want to get the `where` constraints for it, you may leave the `path` argument empty and only provide the `type`:
```js
resolve: async (parent, args, context) => {
return context.prisma.users.count(context.withAuth({
where: {
some: 'query'
}
}, undefined, 'UserType'))
}
```
**NOTE**: in this case `withAuth` would only generate the `where` constraint, but not a `select` clause.
If your resolver requires some data to be available on the `parent`, you should specify it in the config passed to `makeAuthorizationMiddlewares`:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@joindeed/prisma-auth",
"version": "1.1.1",
"version": "1.2.0",
"description": "Declarative Prisma Authorization layer",
"main": "dist/index.js",
"scripts": {
Expand Down
29 changes: 26 additions & 3 deletions src/makeListConstraintMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Middleware, Configuration } from '.'
import { getListWhereConstrains } from './getListWhereConstrains'

import { PrismaSelect } from './select'

Expand All @@ -24,10 +25,32 @@ import { PrismaSelect } from './select'
export const makeListConstraintMiddleware: (config: Configuration) => Middleware =
(options) => async (resolve, parent, args, context, info) => {
const select = new PrismaSelect(info, options, context)
// The order here is important: auth must be set last so it wouldn't be possible to override it with the query

const withAuth = <T extends unknown>(query: T, path?: string, type?: string): T => {
const selectValue = path && type ? select.valueOf(path, type) : select.value
return PrismaSelect.mergeDeep({}, query, selectValue)
let selectValue

// Get nested value from the return type of the resolver
if (path && type) {
selectValue = select.valueOf(path, type)
// Get ONLY `where` clause for the specified type, without relation to the resolver at all
// The `select` clause would have to be crafted manually in this case, as it's not possible to deduce it from the return type of the resolver
// Useful for getting the auth constraints in situations where it's not possible to get it from the resolver return type
// (e.g. a `count` resolver that only returns a number)
} else if (!path && type) {
const where =
getListWhereConstrains(
type,
info.schema.getType(type)?.description || '',
options,
context
)
selectValue = { where }
// Get the value for the root return type of the resolver
} else {
selectValue = select.value
}
// The order here is important: auth must be set last so it wouldn't be possible to override it with the query
return PrismaSelect.mergeDeep({}, query || {}, selectValue)
}

return resolve(parent, args, {
Expand Down

0 comments on commit ab89d0c

Please sign in to comment.