wesleyyoung / perch-query-builder

Dynamically build TypeORM queries based on GraphQL queries for NestJS and TypeORM
GNU General Public License v3.0
45 stars 9 forks source link

Issue: relations are `null` when joined fields are all null #9

Open Migushthe2nd opened 3 years ago

Migushthe2nd commented 3 years ago

Describe the bug When the PerchQueryBuilder executes it's query, it does not always SELECT the primary key. This can result in a

Cannot return null for non-nullable field Object.details.

if the GraphQL field is marked non-nullable. The TypeORM Repository.find() does always select the primary key, and thus does not cause an error.

For this example, the object's details row in the db does not have a name, but it does have a message.

Example query:

query {
  objects {
    id
    details {
      name
    }
  }  
}

Example response:

{
  id: 1,
  details: null,
}

Example query:

query {
  objects {
    id
    details {
      name
      message
    }
  }  
}

Example response:

{
  id: 1,
  details: {
    name: null,
    message: "hello!",
  }
}

A relevant comment in the TypeORM repository: https://github.com/typeorm/typeorm/issues/5177#issuecomment-877825919

Expected behavior

Query:

query {
  objects {
    id
    details {
      name
    }
  }  
}

Expected response:

{
  id: 1,
  details: {
    name: null,
  }
}

Example Code As basic as using

PerchQueryBuilder.find<Type>(repository, info);

Additional context v1.3.0