Skip to content

Commit c252b2a

Browse files
committed
fix(query-graphql): Fixed relations update/aggregate not respecting the given name
Fixes #239
1 parent 3f594b0 commit c252b2a

File tree

5 files changed

+58
-19
lines changed

5 files changed

+58
-19
lines changed

examples/basic/schema.gql

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ type TodoItem {
163163
"""Specify to sort results."""
164164
sorting: [TagSort!]! = []
165165
): TodoItemTagsConnection!
166+
allSubTasks(
167+
"""Specify to filter the records returned."""
168+
filter: SubTaskFilter! = {}
169+
170+
"""Specify to sort results."""
171+
sorting: [SubTaskSort!]! = []
172+
): [SubTask!]!
166173
}
167174

168175
input SubTaskFilter {
@@ -403,7 +410,10 @@ type Mutation {
403410
setSubTasksOnTodoItem(input: SetSubTasksOnTodoItemInput!): TodoItem!
404411
addTagsToTodoItem(input: AddTagsToTodoItemInput!): TodoItem!
405412
setTagsOnTodoItem(input: SetTagsOnTodoItemInput!): TodoItem!
413+
addAllSubTasksToTodoItem(input: AddAllSubTasksToTodoItemInput!): TodoItem!
414+
setAllSubTasksOnTodoItem(input: SetAllSubTasksOnTodoItemInput!): TodoItem!
406415
removeTagsFromTodoItem(input: RemoveTagsFromTodoItemInput!): TodoItem!
416+
removeAllSubTasksFromTodoItem(input: RemoveAllSubTasksFromTodoItemInput!): TodoItem!
407417
createOneTodoItem(input: CreateOneTodoItemInput!): TodoItem!
408418
createManyTodoItems(input: CreateManyTodoItemsInput!): [TodoItem!]!
409419
updateOneTodoItem(input: UpdateOneTodoItemInput!): TodoItem!
@@ -535,6 +545,22 @@ input SetTagsOnTodoItemInput {
535545
relationIds: [ID!]!
536546
}
537547

548+
input AddAllSubTasksToTodoItemInput {
549+
"""The id of the record."""
550+
id: ID!
551+
552+
"""The ids of the relations."""
553+
relationIds: [ID!]!
554+
}
555+
556+
input SetAllSubTasksOnTodoItemInput {
557+
"""The id of the record."""
558+
id: ID!
559+
560+
"""The ids of the relations."""
561+
relationIds: [ID!]!
562+
}
563+
538564
input RemoveTagsFromTodoItemInput {
539565
"""The id of the record."""
540566
id: ID!
@@ -543,6 +569,14 @@ input RemoveTagsFromTodoItemInput {
543569
relationIds: [ID!]!
544570
}
545571

572+
input RemoveAllSubTasksFromTodoItemInput {
573+
"""The id of the record."""
574+
id: ID!
575+
576+
"""The ids of the relations."""
577+
relationIds: [ID!]!
578+
}
579+
546580
input CreateOneTodoItemInput {
547581
"""The record to create"""
548582
todoItem: TodoItemInput!

examples/basic/src/todo-item/dto/todo-item.dto.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GraphQLISODateTime, ID, ObjectType } from '@nestjs/graphql'
2-
import { CursorConnection, FilterableField } from '@ptc-org/nestjs-query-graphql'
2+
import { CursorConnection, FilterableField, UnPagedRelation } from '@ptc-org/nestjs-query-graphql'
33

44
import { SubTaskDTO } from '../../sub-task/dto/sub-task.dto'
55
import { TagDTO } from '../../tag/dto/tag.dto'
@@ -12,6 +12,11 @@ import { TagDTO } from '../../tag/dto/tag.dto'
1212
update: { enabled: true },
1313
remove: { enabled: true }
1414
})
15+
@UnPagedRelation('allSubTasks', () => SubTaskDTO, {
16+
relationName: 'subTasks',
17+
update: { enabled: true },
18+
remove: { enabled: true }
19+
})
1520
export class TodoItemDTO {
1621
@FilterableField(() => ID)
1722
id!: number

examples/dataloader-configuration/schema.gql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,8 @@ input CursorPaging {
365365
}
366366

367367
type Mutation {
368-
addTodoItemsToSubTask(input: AddTodoItemsToSubTaskInput!): SubTask!
369-
setTodoItemsOnSubTask(input: SetTodoItemsOnSubTaskInput!): SubTask!
368+
addTodoItemToSubTask(input: AddTodoItemToSubTaskInput!): SubTask!
369+
setTodoItemOnSubTask(input: SetTodoItemOnSubTaskInput!): SubTask!
370370
createOneSubTask(input: CreateOneSubTaskInput!): SubTask!
371371
createManySubTasks(input: CreateManySubTasksInput!): [SubTask!]!
372372
updateOneSubTask(input: UpdateOneSubTaskInput!): SubTask!
@@ -395,15 +395,15 @@ type Mutation {
395395
deleteManyTags(input: DeleteManyTagsInput!): DeleteManyResponse!
396396
}
397397

398-
input AddTodoItemsToSubTaskInput {
398+
input AddTodoItemToSubTaskInput {
399399
"""The id of the record."""
400400
id: ID!
401401

402402
"""The ids of the relations."""
403403
relationIds: [ID!]!
404404
}
405405

406-
input SetTodoItemsOnSubTaskInput {
406+
input SetTodoItemOnSubTaskInput {
407407
"""The id of the record."""
408408
id: ID!
409409

packages/query-graphql/src/resolvers/relations/aggregate-relations.resolver.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ const AggregateRelationMixin =
3333
const commonResolverOpts = relation.aggregate || removeRelationOpts(relation)
3434
const relationDTO = relation.DTO
3535
const dtoName = getDTONames(DTOClass).baseName
36-
const { baseNameLower, pluralBaseNameLower, pluralBaseName } = getDTONames(relationDTO, {
36+
const { baseName, baseNameLower } = getDTONames(relationDTO, {
3737
dtoName: relation.dtoName
3838
})
39-
const relationName = relation.relationName ?? pluralBaseNameLower
40-
const aggregateRelationLoaderName = `aggregate${pluralBaseName}For${dtoName}`
39+
const relationName = relation.relationName ?? baseNameLower
40+
const aggregateRelationLoaderName = `aggregate${baseName}For${dtoName}`
4141
const aggregateLoader = new AggregateRelationsLoader<DTO, Relation>(relationDTO, relationName)
4242

4343
@ArgsType()
4444
class RelationQA extends AggregateArgsType(relationDTO) {}
4545

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

4848
@Resolver(() => DTOClass, { isAbstract: true })
4949
class AggregateMixin extends Base {
5050
@ResolverField(
51-
`${pluralBaseNameLower}Aggregate`,
51+
`${baseNameLower}Aggregate`,
5252
() => [AR],
5353
{
5454
description: relation.aggregate?.description
@@ -58,7 +58,7 @@ const AggregateRelationMixin =
5858
interceptors: [AuthorizerInterceptor(DTOClass)]
5959
}
6060
)
61-
async [`aggregate${pluralBaseName}`](
61+
async [`aggregate${baseName}`](
6262
@Parent() dto: DTO,
6363
@Args() q: RelationQA,
6464
@AggregateQueryParam() aggregateQuery: AggregateQuery<Relation>,

packages/query-graphql/src/resolvers/relations/update-relations.resolver.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ const UpdateManyRelationMixin =
7171
const commonResolverOpts = removeRelationOpts(relation)
7272
const relationDTO = relation.DTO
7373
const dtoNames = getDTONames(DTOClass)
74-
const { pluralBaseNameLower, pluralBaseName } = getDTONames(relationDTO, { dtoName: relation.dtoName })
75-
const relationName = relation.relationName ?? pluralBaseNameLower
74+
const { baseNameLower, baseName } = getDTONames(relationDTO, { dtoName: relation.dtoName })
75+
const relationName = relation.relationName ?? baseNameLower
7676

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

8080
@ArgsType()
8181
class AddArgs extends MutationArgsType(AddRelationInput) {}
8282

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

8686
@ArgsType()
@@ -99,9 +99,9 @@ const UpdateManyRelationMixin =
9999
interceptors: [AuthorizerInterceptor(DTOClass)]
100100
}
101101
)
102-
async [`add${pluralBaseName}To${dtoNames.baseName}`](
102+
async [`add${baseName}To${dtoNames.baseName}`](
103103
@Args() addArgs: AddArgs,
104-
@ModifyRelationAuthorizerFilter(pluralBaseNameLower, {
104+
@ModifyRelationAuthorizerFilter(baseNameLower, {
105105
operationGroup: OperationGroup.UPDATE,
106106
many: true
107107
})
@@ -121,9 +121,9 @@ const UpdateManyRelationMixin =
121121
interceptors: [AuthorizerInterceptor(DTOClass)]
122122
}
123123
)
124-
async [`set${pluralBaseName}On${dtoNames.baseName}`](
124+
async [`set${baseName}On${dtoNames.baseName}`](
125125
@Args() addArgs: SetArgs,
126-
@ModifyRelationAuthorizerFilter(pluralBaseNameLower, {
126+
@ModifyRelationAuthorizerFilter(baseNameLower, {
127127
operationGroup: OperationGroup.UPDATE,
128128
many: true
129129
})

0 commit comments

Comments
 (0)