Skip to content

Make authorizer work for nested relations #355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions examples/auth/e2e/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DataSource } from 'typeorm'

import { executeTruncate } from '../../helpers'
import { SubSubTaskEntity } from '../src/sub-sub-task/sub-sub-task.entity'
import { SubTaskEntity } from '../src/sub-task/sub-task.entity'
import { TagEntity } from '../src/tag/tag.entity'
import { TodoItemEntity } from '../src/todo-item/todo-item.entity'
Expand All @@ -15,6 +16,7 @@ export const refresh = async (dataSource: DataSource): Promise<void> => {
const userRepo = dataSource.getRepository(UserEntity)
const todoRepo = dataSource.getRepository(TodoItemEntity)
const subTaskRepo = dataSource.getRepository(SubTaskEntity)
const subSubTaskRepo = dataSource.getRepository(SubSubTaskEntity)
const tagsRepo = dataSource.getRepository(TagEntity)

const users = await userRepo.save([
Expand Down Expand Up @@ -50,15 +52,24 @@ export const refresh = async (dataSource: DataSource): Promise<void> => {
Promise.resolve([] as TodoItemEntity[])
)

await subTaskRepo.save(
todoItems.reduce(
(subTasks, todo) => [
...subTasks,
const subTasks: SubTaskEntity[] = await subTaskRepo.save(
todoItems.flatMap(
(todo) => [
{ completed: true, title: `${todo.title} - Sub Task 1`, todoItem: todo, ownerId: todo.ownerId },
{ completed: false, title: `${todo.title} - Sub Task 2`, todoItem: todo, ownerId: todo.ownerId },
{ completed: false, title: `${todo.title} - Sub Task 3`, todoItem: todo, ownerId: todo.ownerId }
],
[] as Partial<SubTaskEntity>[]
[]
)
)

await subSubTaskRepo.save(
subTasks.flatMap(
(parent) => [
{ subTask: parent, title: `${parent.title} - Sub Sub Task Public`, public: true },
{ subTask: parent, title: `${parent.title} - Sub Sub Task Private`, public: false }
],
[]
)
)
}
61 changes: 58 additions & 3 deletions examples/auth/e2e/todo-item.resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,61 @@ describe('TodoItemResolver (auth - e2e)', () => {
])
}))

it(`should allow fetching subSubTasks, but only the ones that are allowed by the nested authorizers`, () =>
request(app.getHttpServer())
.post('/graphql')
.auth(jwtToken, { type: 'bearer' })
.send({
operationName: null,
variables: {},
query: `{
todoItems {
edges {
node {
subTasks {
edges {
node {
subSubTasks {
title
}
}
}
}
}
}
}
}`
})
.expect(200)
.then(({ body }) => {
const {
edges: [{ node: task }]
}: {
edges: Array<{
node: {
subTasks: {
edges: Array<{
node: {
subSubTasks: Array<{
title: string
}>
}
}>
}
}
}>
} = body.data.todoItems
const [subTask] = task.subTasks.edges.map((e) => e.node)

expect(subTask).toEqual({
subSubTasks: [
{
title: 'Create Nest App - Sub Task 1 - Sub Sub Task Public'
}
]
})
}))

it(`should allow querying on tags`, () =>
request(app.getHttpServer())
.post('/graphql')
Expand Down Expand Up @@ -620,7 +675,7 @@ describe('TodoItemResolver (auth - e2e)', () => {
.send({
operationName: null,
variables: {},
query: `{
query: `{
todoItemAggregate {
${todoItemAggregateFields}
}
Expand All @@ -647,7 +702,7 @@ describe('TodoItemResolver (auth - e2e)', () => {
.send({
operationName: null,
variables: {},
query: `{
query: `{
todoItemAggregate {
${todoItemAggregateFields}
}
Expand All @@ -674,7 +729,7 @@ describe('TodoItemResolver (auth - e2e)', () => {
.send({
operationName: null,
variables: {},
query: `{
query: `{
todoItemAggregate(filter: { completed: { is: false } }) {
${todoItemAggregateFields}
}
Expand Down
Loading