Skip to content

Commit ab89d0c

Browse files
authored
FEATURE: get constraints for arbitrary type (#8)
* FEATURE: get constraints for arbitrary type * @joindeed/mongoose-models-1.2.0
1 parent 60d8d40 commit ab89d0c

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ resolve: async (parent, args, context) => {
104104
}
105105
}))
106106
}
107+
```
108+
109+
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),
110+
and want to get the `where` constraints for it, you may leave the `path` argument empty and only provide the `type`:
111+
112+
```js
113+
resolve: async (parent, args, context) => {
114+
return context.prisma.users.count(context.withAuth({
115+
where: {
116+
some: 'query'
117+
}
118+
}, undefined, 'UserType'))
119+
}
120+
```
121+
122+
**NOTE**: in this case `withAuth` would only generate the `where` constraint, but not a `select` clause.
123+
107124
108125
If your resolver requires some data to be available on the `parent`, you should specify it in the config passed to `makeAuthorizationMiddlewares`:
109126

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@joindeed/prisma-auth",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Declarative Prisma Authorization layer",
55
"main": "dist/index.js",
66
"scripts": {

src/makeListConstraintMiddleware.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Middleware, Configuration } from '.'
2+
import { getListWhereConstrains } from './getListWhereConstrains'
23

34
import { PrismaSelect } from './select'
45

@@ -24,10 +25,32 @@ import { PrismaSelect } from './select'
2425
export const makeListConstraintMiddleware: (config: Configuration) => Middleware =
2526
(options) => async (resolve, parent, args, context, info) => {
2627
const select = new PrismaSelect(info, options, context)
27-
// The order here is important: auth must be set last so it wouldn't be possible to override it with the query
28+
2829
const withAuth = <T extends unknown>(query: T, path?: string, type?: string): T => {
29-
const selectValue = path && type ? select.valueOf(path, type) : select.value
30-
return PrismaSelect.mergeDeep({}, query, selectValue)
30+
let selectValue
31+
32+
// Get nested value from the return type of the resolver
33+
if (path && type) {
34+
selectValue = select.valueOf(path, type)
35+
// Get ONLY `where` clause for the specified type, without relation to the resolver at all
36+
// 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
37+
// Useful for getting the auth constraints in situations where it's not possible to get it from the resolver return type
38+
// (e.g. a `count` resolver that only returns a number)
39+
} else if (!path && type) {
40+
const where =
41+
getListWhereConstrains(
42+
type,
43+
info.schema.getType(type)?.description || '',
44+
options,
45+
context
46+
)
47+
selectValue = { where }
48+
// Get the value for the root return type of the resolver
49+
} else {
50+
selectValue = select.value
51+
}
52+
// The order here is important: auth must be set last so it wouldn't be possible to override it with the query
53+
return PrismaSelect.mergeDeep({}, query || {}, selectValue)
3154
}
3255

3356
return resolve(parent, args, {

0 commit comments

Comments
 (0)