4

I have in my database a couple of related tables:

rooms table:

    "id" SERIAL PRIMARY KEY,
    "details" jsonb,
    "metadata" jsonb,
    "region" character varying(255) NOT NULL DEFAULT 'uk'::character varying,
    "createdAt" timestamp with time zone NOT NULL,
    "updatedAt" timestamp with time zone NOT NULL,
    "houseId" integer REFERENCES houses(id) ON DELETE SET NULL ON UPDATE CASCADE

houses table:

    "id" SERIAL PRIMARY KEY,
    "coordinates" jsonb,
    "type" jsonb NOT NULL,
    "region" character varying(255) NOT NULL DEFAULT 'uk'::character varying,
    "createdAt" timestamp with time zone NOT NULL,
    "updatedAt" timestamp with time zone NOT NULL,
    "blockId" integer REFERENCES blocks(id) ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT "houses_originalProdId_region_key" UNIQUE ("originalProdId", region)

In my project, I use PostGraphile (graphile-utils) as my GraphQL provider.

I created a custom resolver to execute dynamically-built complex queries on rooms using Sequelize. I chose to query the DB through Sequelize and not directly through PostGraphile because various parts of the query object are built dynamically (the where object and the include array).

The resolver looks like this:

async (_query, args) => {
      const { input, pagination } = args;

      const filteredRooms = await roomRepository.findRoomsUsingDynamicFilters(
        pagination,
        input,
      );

      return filteredRooms || null;
    }

This query works, but now relations cannot be loaded for rooms, so I can’t query house through my relevant filtered rooms:

{
  filterRooms(input: { filterByAttributes: ["large", "white"] }, pagination: {}) {
    house{
      id
    }
  }
}

Results in

{
  "data": {
    "filterRooms": [
      {
        "house": null
      },
      {
        "house": null
      },
      {
        "house": null
      },
    ]
  }
}

In the PostGraphile docs I’ve found the method selectGraphQLResultFromTable which seems to load relations, but I’m unsure how to use it in conjunction with the Sequelize query; that is, how can my resolver return only the rooms that I’ve filtered through Sequelize, in a format that will enable the querying of their relations (like in the example above)?

BaliTov
  • 41
  • 3

0 Answers0