Replies: 3 comments 1 reply
-
Good question, I don't know, if you tried it let me know. After checking locally I think this should be possible. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Including Child Records in a GraphQL MutationSteps to Configure Parent-Child Relationships1. Define DTOsDefine the parent and child DTOs using the import { ObjectType, Field, ID, InputType } from '@nestjs/graphql';
import { Relation } from '@nestjs-query/query-graphql';
@ObjectType('Xpto')
@InputType('XptoInput')
export class XptoDTO {
@Field(() => ID)
id: number;
@Field()
dataOne: string;
@Field()
dataDate: Date;
@Relation('xptoChild1', () => XptoChildDTO, { nullable: true })
xptoChild1?: XptoChildDTO;
}
@ObjectType('XptoChild')
@InputType('XptoChildInput')
export class XptoChildDTO {
@Field(() => ID)
id: number;
@Field(() => ID)
idXpto: number;
@Field()
descriptionXptoChild: string;
}
2. Configure Services
Ensure that your services support relationships. If using @nestjs-query/query-typeorm, relationships are automatically managed if they are configured in your entities.
3. Configure Mutations
Create a mutation in your GraphQL resolver to handle inserting parent and child data.
import { Resolver, Mutation, Args } from '@nestjs/graphql';
import { XptoDTO } from './xpto.dto';
import { XptoService } from './xpto.service';
@Resolver(() => XptoDTO)
export class XptoResolver {
constructor(private readonly xptoService: XptoService) {}
@Mutation(() => XptoDTO)
async createOneXpto(@Args('input') input: XptoDTO): Promise<XptoDTO> {
return this.xptoService.create(input);
}
}
4. Example Mutation Query
Once configured, you can run the following mutation to insert parent and child records:
mutation {
createOneXpto(input: {
id: 1,
dataOne: "xpto",
dataDate: "2024-12-06T13:32:14-03:00",
xptoChild1: {
id: 1,
idXpto: 1,
descriptionXptoChild: "xpto child 1"
}
}) {
id
dataOne
dataDate
xptoChild1 {
id
descriptionXptoChild
}
}
}
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Using
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I would like to know if it is possible or if so how to include child records in a mutation, is it possible? Are there any examples or topics in the documentation about it?
example:
Beta Was this translation helpful? Give feedback.
All reactions