Nullability in Object Relationships

Hello everyone.

I am facing an issue with nullability in types generated for Hasura object relationships using GraphQL codegen (yarn graphql-codegen --config codegen.ts ) to generate the graphql.schema.json and graphql.ts for typed queries in my next js application.

We have two tables summary and list with foreign key and an Hasura object relationship named summary in list table mapping summary.id (PK) and list.summary_id columns in both tables respectively.

Earlier when I was generating types for this query -

`query getListDetails {
    list(where: $where) {
        //...
        id
        summary {
        ...SummaryFragment
      }
    }
  }`

we got types generated as

list: {
   //...
   id: number;
   summary: SummaryFragment;
};

After some changes in our postgres database, the foreign keys were dropped and we recreated them. But now I am getting types generated as

list: {
   //...
   id: number;
   summary: SummaryFragment | null;
};

We are unable to figure out what change in db could have caused this issue.
Can someone please guide me what could be the reason that now I am getting this | null in

summary: SummaryFragment | null; 

I can share more details if required. Any help would be appreciated.

Thank you!

Hey @Ritik_Jain :wave:

Did your schema change as a result of the db operation? If so, this could be a possible reason.

If you think this is a bug, I’d recommend opening an issue with graphql-code-generator (assuming you’re using that tool) since we (Apollo) don’t own or maintain that tool.

Hi @jerelmiller
The DB Schema did change but the column structure did not change. Only the primary keys and foreign keys in some tables were changed. Which we set up again just like before.

We have a foreign key in list table between list.summary_id and summary.id
Id column in summary table is a non nullable auto incremented primary key. So for each summary_id in list table we must get a row returned from summary table. But still I get summary: SummaryFragment | null;
The query is still working as expected, it’s just the data type generated which is causing issue in our functions which don’t except null values.
The thing I’m trying to figure out is what controls the data type of relationships. Is there some unique key or foreign key constraint required for data returned from relationship to be non null only.

Thanks for your response.

@Ritik_Jain I probably should have asked this the first time, but what tool are you using to generate types for you application?