Closed IvanKudryavskyReq closed 5 months ago
Could you try to update User to:
@Entity()
export class User {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column(() => Address)
address?: Address;
@ManyToOne(() => State, { cascade: ['remove'] })
state: State;
// Add this
@Column()
stateId: string
}
I added
@Column()
stateId: string
in user.entity.ts - no change In address.ts - no change either
The response looks the same as in the last screenshot
Then please create a example repo or add it as a test case to the examples.
Done: https://github.com/IvanKudryavskyReq/nestjs-query-issue-255 Added startup instructions to README.md
I see, did not know this was possible:
@Entity()
export class User {
@Column(() => Address)
address?: Address;
}
But in the repo you gave the ManyToOne
was inside Address
, so adding the following in address.ts
will fill the stateId
field inside:
@Column()
stateId: string;
This package does not handle the case you have here, every "object" in the main should be a relation if you want to go deeper. Since the field addressStateId
is added to the root one you could keep address as is but add that field to the entity and then add the relation from user -> state to the user type.
Closing this, if still an issue you can re-open the ticket.
Describe the bug If some enity has an Embedded field that has a ManyToOne relationship, the responses will return null (User -> Address (embedded) -> State (ManyToOne))
Have you read the Contributing Guidelines?
Yes
To Reproduce Steps to reproduce the behavior:
@Entity() export class State { @PrimaryGeneratedColumn() id: number;
@Column({ length: 50, unique: true }) @Index() name: string; }
import { ManyToOne } from 'typeorm'; import { State } from './state.entity';
export class Address { @ManyToOne(() => State, { cascade: ['remove'] }) state: State; }
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm'; import { Address } from './address.entity'; import { State } from './state.entity';
@Entity() export class User { @PrimaryGeneratedColumn('uuid') id: string;
@Column(() => Address) address?: Address;
@ManyToOne(() => State, { cascade: ['remove'] }) state: State; }
import { Field, Int, ObjectType } from '@nestjs/graphql'; import { FilterableField, IDField } from '@ptc-org/nestjs-query-graphql';
@ObjectType('State') export class StateResponse { @IDField(() => Int) id: number;
@Field() @FilterableField() name: string; }
import { ObjectType } from '@nestjs/graphql'; import { StateResponse } from './state.response'; import { Relation } from '@ptc-org/nestjs-query-graphql';
@ObjectType('Address') @Relation('state', () => StateResponse) export class AddressResponse { }
import { Field, GraphQLISODateTime, ID, ObjectType } from '@nestjs/graphql'; import { AddressResponse } from './address.response'; import { Address } from './address'; import { FilterableField, IDField, PagingStrategies, QueryOptions, Relation } from '@ptc-org/nestjs-query-graphql'; import { StateResponse } from './state.response';
@ObjectType('User') @QueryOptions({ pagingStrategy: PagingStrategies.OFFSET, enableTotalCount: true, }) @Relation('state', () => StateResponse) export class UserResponse { @IDField(() => ID) id: string;
@Field((type) => AddressResponse, { nullable: true }) address?: Address; }
query User($userId: ID!) { user(id: $userId) { address { state { name } } state { id name shortName } } }
{ "data": { "user": { "address": { "state": null }, "state": { "id": 5, "name": "California", } } } }