From 92d2f3e036185061678d5a7988a2db2600100ebb Mon Sep 17 00:00:00 2001 From: Haydn Ewers <27211+haydn@users.noreply.github.com> Date: Thu, 5 Aug 2021 20:30:07 +1000 Subject: [PATCH] Return all items for queries without ids --- index.ts | 2 +- test.ts | 130 +++++++++++++++++++++++++------------------------------ 2 files changed, 59 insertions(+), 73 deletions(-) diff --git a/index.ts b/index.ts index 72ef909..925eba1 100644 --- a/index.ts +++ b/index.ts @@ -340,7 +340,7 @@ class RecordSet extends EventTarget { resolve: (_, { ids }) => Array.isArray(ids) ? ids.map((id) => this.records.find((record) => record.id === id)) - : [], + : this.records.filter((record) => record.type === type), }; dynamicMutationFields[`create${type}`] = { type: dynamicTypes[type], diff --git a/test.ts b/test.ts index bd8b415..cd32eec 100644 --- a/test.ts +++ b/test.ts @@ -576,7 +576,7 @@ describe("RecordSet query resolution", () => { ).toEqual({ data: { relationships: [] } }); }); - test("minimal schema", () => { + test("single record", () => { const recordSet = new RecordSet(MINIMAL_SCHEMA, { init: [ { @@ -594,6 +594,33 @@ describe("RecordSet query resolution", () => { record(id: "267d5cb0-90aa-471c-b5fc-3b31afd73184") { id } + } + `, + ), + ).toEqual({ + data: { + record: { + id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", + }, + }, + }); + }); + + test("multiple records", () => { + const recordSet = new RecordSet(MINIMAL_SCHEMA, { + init: [ + { + type: "Record", + id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", + }, + ], + }); + + expect( + execute( + recordSet.schema, + gql` + query { records(ids: ["267d5cb0-90aa-471c-b5fc-3b31afd73184"]) { id } @@ -602,9 +629,38 @@ describe("RecordSet query resolution", () => { ), ).toEqual({ data: { - record: { + records: [ + { + id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", + }, + ], + }, + }); + }); + + test("all records", () => { + const recordSet = new RecordSet(MINIMAL_SCHEMA, { + init: [ + { + type: "Record", id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", }, + ], + }); + + expect( + execute( + recordSet.schema, + gql` + query { + records { + id + } + } + `, + ), + ).toEqual({ + data: { records: [ { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", @@ -642,15 +698,6 @@ describe("RecordSet query resolution", () => { date } } - records(ids: ["267d5cb0-90aa-471c-b5fc-3b31afd73184"]) { - id - ... on Record { - string - number - boolean - date - } - } } `, ), @@ -663,15 +710,6 @@ describe("RecordSet query resolution", () => { boolean: true, date: "2021-06-17T19:54:14Z", }, - records: [ - { - id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", - string: "hello", - number: 1, - boolean: true, - date: "2021-06-17T19:54:14Z", - }, - ], }, }); }); @@ -710,21 +748,6 @@ describe("RecordSet query resolution", () => { id } } - records(ids: ["267d5cb0-90aa-471c-b5fc-3b31afd73184"]) { - id - manyToMany { - id - } - manyToOne { - id - } - oneToOne { - id - } - oneToMany { - id - } - } } `, ), @@ -737,15 +760,6 @@ describe("RecordSet query resolution", () => { oneToOne: { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }, oneToMany: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], }, - records: [ - { - id: "267d5cb0-90aa-471c-b5fc-3b31afd73184", - manyToMany: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], - manyToOne: { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }, - oneToOne: { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }, - oneToMany: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], - }, - ], }, }); }); @@ -795,24 +809,6 @@ describe("RecordSet query resolution", () => { id } } - records(ids: ["71dddd29-680d-4dbd-92ed-2ea085a569cb"]) { - id - inverseManyToMany { - id - } - inverseManyToOne { - id - } - inverseOneToOne { - id - } - inverseOneToMany { - id - } - inverseMultiple { - id - } - } } `, ), @@ -826,16 +822,6 @@ describe("RecordSet query resolution", () => { inverseOneToMany: { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }, inverseMultiple: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], }, - records: [ - { - id: "71dddd29-680d-4dbd-92ed-2ea085a569cb", - inverseManyToMany: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], - inverseManyToOne: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], - inverseOneToOne: { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }, - inverseOneToMany: { id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }, - inverseMultiple: [{ id: "267d5cb0-90aa-471c-b5fc-3b31afd73184" }], - }, - ], }, }); }); -- 2.51.2