Skip to content

Commit

Permalink
fix(query-graphql): Fixed relations update/aggregate not respecting t…
Browse files Browse the repository at this point in the history
…he given name

Fixes #239
  • Loading branch information
TriPSs committed Mar 26, 2024
1 parent 3f594b0 commit c252b2a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
34 changes: 34 additions & 0 deletions examples/basic/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ type TodoItem {
"""Specify to sort results."""
sorting: [TagSort!]! = []
): TodoItemTagsConnection!
allSubTasks(
"""Specify to filter the records returned."""
filter: SubTaskFilter! = {}

"""Specify to sort results."""
sorting: [SubTaskSort!]! = []
): [SubTask!]!
}

input SubTaskFilter {
Expand Down Expand Up @@ -403,7 +410,10 @@ type Mutation {
setSubTasksOnTodoItem(input: SetSubTasksOnTodoItemInput!): TodoItem!
addTagsToTodoItem(input: AddTagsToTodoItemInput!): TodoItem!
setTagsOnTodoItem(input: SetTagsOnTodoItemInput!): TodoItem!
addAllSubTasksToTodoItem(input: AddAllSubTasksToTodoItemInput!): TodoItem!
setAllSubTasksOnTodoItem(input: SetAllSubTasksOnTodoItemInput!): TodoItem!
removeTagsFromTodoItem(input: RemoveTagsFromTodoItemInput!): TodoItem!
removeAllSubTasksFromTodoItem(input: RemoveAllSubTasksFromTodoItemInput!): TodoItem!
createOneTodoItem(input: CreateOneTodoItemInput!): TodoItem!
createManyTodoItems(input: CreateManyTodoItemsInput!): [TodoItem!]!
updateOneTodoItem(input: UpdateOneTodoItemInput!): TodoItem!
Expand Down Expand Up @@ -535,6 +545,22 @@ input SetTagsOnTodoItemInput {
relationIds: [ID!]!
}

input AddAllSubTasksToTodoItemInput {
"""The id of the record."""
id: ID!

"""The ids of the relations."""
relationIds: [ID!]!
}

input SetAllSubTasksOnTodoItemInput {
"""The id of the record."""
id: ID!

"""The ids of the relations."""
relationIds: [ID!]!
}

input RemoveTagsFromTodoItemInput {
"""The id of the record."""
id: ID!
Expand All @@ -543,6 +569,14 @@ input RemoveTagsFromTodoItemInput {
relationIds: [ID!]!
}

input RemoveAllSubTasksFromTodoItemInput {
"""The id of the record."""
id: ID!

"""The ids of the relations."""
relationIds: [ID!]!
}

input CreateOneTodoItemInput {
"""The record to create"""
todoItem: TodoItemInput!
Expand Down
7 changes: 6 additions & 1 deletion examples/basic/src/todo-item/dto/todo-item.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLISODateTime, ID, ObjectType } from '@nestjs/graphql'
import { CursorConnection, FilterableField } from '@ptc-org/nestjs-query-graphql'
import { CursorConnection, FilterableField, UnPagedRelation } from '@ptc-org/nestjs-query-graphql'

import { SubTaskDTO } from '../../sub-task/dto/sub-task.dto'
import { TagDTO } from '../../tag/dto/tag.dto'
Expand All @@ -12,6 +12,11 @@ import { TagDTO } from '../../tag/dto/tag.dto'
update: { enabled: true },
remove: { enabled: true }
})
@UnPagedRelation('allSubTasks', () => SubTaskDTO, {
relationName: 'subTasks',
update: { enabled: true },
remove: { enabled: true }
})
export class TodoItemDTO {
@FilterableField(() => ID)
id!: number
Expand Down
8 changes: 4 additions & 4 deletions examples/dataloader-configuration/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ input CursorPaging {
}

type Mutation {
addTodoItemsToSubTask(input: AddTodoItemsToSubTaskInput!): SubTask!
setTodoItemsOnSubTask(input: SetTodoItemsOnSubTaskInput!): SubTask!
addTodoItemToSubTask(input: AddTodoItemToSubTaskInput!): SubTask!
setTodoItemOnSubTask(input: SetTodoItemOnSubTaskInput!): SubTask!
createOneSubTask(input: CreateOneSubTaskInput!): SubTask!
createManySubTasks(input: CreateManySubTasksInput!): [SubTask!]!
updateOneSubTask(input: UpdateOneSubTaskInput!): SubTask!
Expand Down Expand Up @@ -395,15 +395,15 @@ type Mutation {
deleteManyTags(input: DeleteManyTagsInput!): DeleteManyResponse!
}

input AddTodoItemsToSubTaskInput {
input AddTodoItemToSubTaskInput {
"""The id of the record."""
id: ID!

"""The ids of the relations."""
relationIds: [ID!]!
}

input SetTodoItemsOnSubTaskInput {
input SetTodoItemOnSubTaskInput {
"""The id of the record."""
id: ID!

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ const AggregateRelationMixin =
const commonResolverOpts = relation.aggregate || removeRelationOpts(relation)
const relationDTO = relation.DTO
const dtoName = getDTONames(DTOClass).baseName
const { baseNameLower, pluralBaseNameLower, pluralBaseName } = getDTONames(relationDTO, {
const { baseName, baseNameLower } = getDTONames(relationDTO, {
dtoName: relation.dtoName
})
const relationName = relation.relationName ?? pluralBaseNameLower
const aggregateRelationLoaderName = `aggregate${pluralBaseName}For${dtoName}`
const relationName = relation.relationName ?? baseNameLower
const aggregateRelationLoaderName = `aggregate${baseName}For${dtoName}`
const aggregateLoader = new AggregateRelationsLoader<DTO, Relation>(relationDTO, relationName)

@ArgsType()
class RelationQA extends AggregateArgsType(relationDTO) {}

const [AR] = AggregateResponseType(relationDTO, { prefix: `${dtoName}${pluralBaseName}` })
const [AR] = AggregateResponseType(relationDTO, { prefix: `${dtoName}${baseName}` })

@Resolver(() => DTOClass, { isAbstract: true })
class AggregateMixin extends Base {
@ResolverField(
`${pluralBaseNameLower}Aggregate`,
`${baseNameLower}Aggregate`,
() => [AR],
{
description: relation.aggregate?.description
Expand All @@ -58,7 +58,7 @@ const AggregateRelationMixin =
interceptors: [AuthorizerInterceptor(DTOClass)]
}
)
async [`aggregate${pluralBaseName}`](
async [`aggregate${baseName}`](
@Parent() dto: DTO,
@Args() q: RelationQA,
@AggregateQueryParam() aggregateQuery: AggregateQuery<Relation>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ const UpdateManyRelationMixin =
const commonResolverOpts = removeRelationOpts(relation)
const relationDTO = relation.DTO
const dtoNames = getDTONames(DTOClass)
const { pluralBaseNameLower, pluralBaseName } = getDTONames(relationDTO, { dtoName: relation.dtoName })
const relationName = relation.relationName ?? pluralBaseNameLower
const { baseNameLower, baseName } = getDTONames(relationDTO, { dtoName: relation.dtoName })
const relationName = relation.relationName ?? baseNameLower

@InputType(`Add${pluralBaseName}To${dtoNames.baseName}Input`)
@InputType(`Add${baseName}To${dtoNames.baseName}Input`)
class AddRelationInput extends RelationsInputType(DTOClass, relationDTO) {}

@ArgsType()
class AddArgs extends MutationArgsType(AddRelationInput) {}

@InputType(`Set${pluralBaseName}On${dtoNames.baseName}Input`)
@InputType(`Set${baseName}On${dtoNames.baseName}Input`)
class SetRelationInput extends RelationsInputType(DTOClass, relationDTO) {}

@ArgsType()
Expand All @@ -99,9 +99,9 @@ const UpdateManyRelationMixin =
interceptors: [AuthorizerInterceptor(DTOClass)]
}
)
async [`add${pluralBaseName}To${dtoNames.baseName}`](
async [`add${baseName}To${dtoNames.baseName}`](
@Args() addArgs: AddArgs,
@ModifyRelationAuthorizerFilter(pluralBaseNameLower, {
@ModifyRelationAuthorizerFilter(baseNameLower, {
operationGroup: OperationGroup.UPDATE,
many: true
})
Expand All @@ -121,9 +121,9 @@ const UpdateManyRelationMixin =
interceptors: [AuthorizerInterceptor(DTOClass)]
}
)
async [`set${pluralBaseName}On${dtoNames.baseName}`](
async [`set${baseName}On${dtoNames.baseName}`](
@Args() addArgs: SetArgs,
@ModifyRelationAuthorizerFilter(pluralBaseNameLower, {
@ModifyRelationAuthorizerFilter(baseNameLower, {
operationGroup: OperationGroup.UPDATE,
many: true
})
Expand Down

0 comments on commit c252b2a

Please sign in to comment.