nestjs-query: OnetoMany relationships not returning results, blocked in graphql

After migration to TriPSs I have identified that all of my @OneToMany relationships on the single “has many” side return empty arrays instead of results. Interestingly, aggregate results on this relationship still appear to work. Here is an example where a location should have n actions and you can see the actionAggregate has 1, but the results are empty.

{
  "data": {
    "locations": {
      "edges": [
        {
          "node": {
            "id": "us-minnesota-sauk rapids",
            "actionsAggregate": [
              {
                "count": {
                  "id": 1
                }
              }
            ],
            "actions": {
              "edges": []
            }
          }
        },
<--snip-->

I’ve confirmed this is not a typeorm issue. using the following,

  @Query(() => [Location])
  async test() {
    const results = await this.locationRepository.find({
      where: { id: 'us-minnesota-sauk rapids' },
      relations: {
        actions: true,
      },
    });
    console.log(results);
    return results;
  }
[
  Location {
    isActive: true,
    isArchived: false,
    internalComment: null,
    created: 2023-03-21T15:50:04.902Z,
    createdBy: null,
    updated: 2023-03-21T15:50:04.902Z,
    updatedBy: null,
    deletedOn: null,
    deletedBy: null,
    id: 'us-minnesota-sauk rapids',
    city: 'Sauk Rapids',
    province: 'Minnesota',
    country: 'US',
    timezone: 'America/Chicago',
    region: 'NA',
    actions: [ [Action] ]
  }
]

yet the returned results are still incorrect:

{
  "data": {
    "test": [
      {
        "id": "us-minnesota-sauk rapids",
        "actions": {
          "edges": []
        }
      }
    ]
  }
}

which leads me to believe the graphql declaration on the model… which looks like this:

@Entity()
@ObjectType()
@QueryOptions({ defaultResultSize: DEFAULT_QUERY_RESULTS, maxResultsSize: -1 })
@CursorConnection('articles', () => Article, {
  nullable: true,
  disableRemove: false,
})
@CursorConnection('actions', () => Action, {
  nullable: true,
  disableRemove: false,
})
export class Location extends CommonFields {
  @IDField(() => ID)
  @PrimaryColumn()
  id: string;

  @FilterableField({ nullable: true })
  @Column({ nullable: false })
  city?: string;

  @FilterableField({ nullable: true })
  @Column({ nullable: true })
  province?: string;

  @FilterableField({ nullable: true })
  @Column({ nullable: true })
  country!: string;

  @Field({ nullable: true })
  @Column({ nullable: true })
  timezone?: string;

  @FilterableField({ nullable: true })
  @Column({ nullable: true })
  region?: string;

  @OneToMany(() => Article, (article) => article.location)
  articles: Article[];

  @OneToMany(() => Action, (action) => action.location)
  actions: Action[];
}

I’m not sure if this is a nestjs issue or not… For reference, here are my relevant packages:

    "@nestjs/apollo": "11.0.0",
    "@nestjs/axios": "^2.0.0",
    "@nestjs/common": "9.3.9",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "9.3.9",
    "@nestjs/graphql": "11.0.0",
    "@nestjs/jwt": "^10.0.2",
    "@nestjs/passport": "^9.0.3",
    "@nestjs/platform-express": "9.3.9",
    "@nestjs/sequelize": "^9.0.0",
    "@nestjs/typeorm": "^9.0.1",
    "@ptc-org/nestjs-query-core": "^2.3.0",
    "@ptc-org/nestjs-query-graphql": "^2.3.0",
    "@ptc-org/nestjs-query-sequelize": "^2.3.0",
    "@ptc-org/nestjs-query-typeorm": "^2.3.0",

I should mention that I have other OneToMany examples which are annotated as FilterableUnpagedRelation and UnpagedRelation that also fail to return results and only return empty arrays.

Any help or guidance would be greatly appreciated

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 2
  • Comments: 32 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Should be fixed by #175, thanks @jpv-os for the PR!

@jpv-os feel free to use my demo project which is set up and demonstrates the issue: https://github.com/coupster74/nestjs-query-issue

@TriPSs I didn’t have @JoinColumn on both sides. But the issue I had was already fixed when I updated the package, my bad.

The main reason that is changed was simply that the way we do it now performs way better then the old one (see for example #1495), the old one builded queries that where very very inefficient if you where going to fetch more then a couple of rows.

Having to add all the columns to your entity was not a issue for me (since I was already doing it as I also want to filter on it) , I also do not see this as a “hack” as in my optionion it is better to have your entity actually reflect with that is in the database, so when I created the initial fork for this I did not have any issues with it. Also, adding this field does have no impact on the database since Typeorm already creates the field, adding it only makes sure that it’s also being fetched/set when doing queries.

You can downgrade to the old versions again if you want but this fork is not going back to the old implementation simply because of the massive performance drawback the old implementation has.