diff --git a/lib/loaders/loaders_with_authentication/index.js b/lib/loaders/loaders_with_authentication/index.js index d3e6a1df0f..822025948d 100644 --- a/lib/loaders/loaders_with_authentication/index.js +++ b/lib/loaders/loaders_with_authentication/index.js @@ -31,6 +31,7 @@ export default (accessToken, userID, requestID) => { "artworks", "is_saved" ), + saleArtworksLoader: gravityLoader(id => `sale/${id}/sale_artworks`), lotStandingLoader: gravityLoader("me/lot_standings"), authenticatedPopularArtistsLoader: gravityLoader("artists/popular"), ...convectionLoaders(accessToken, requestID), diff --git a/schema/sale/index.js b/schema/sale/index.js index 3f4216c4d4..5f73e3ab87 100644 --- a/schema/sale/index.js +++ b/schema/sale/index.js @@ -7,6 +7,8 @@ import date from "schema/fields/date" import gravity from "lib/loaders/legacy/gravity" import moment from "moment" import { GravityIDFields } from "schema/object_identification" +import { pageable, getPagingParameters } from "relay-cursor-paging" +import { connectionFromArraySlice, connectionDefinitions } from "graphql-relay" import { amount } from "schema/fields/money" import { exclude } from "lib/helpers" import { map } from "lodash" @@ -51,6 +53,10 @@ const BuyersPremium = new GraphQLObjectType({ }, }) +const saleArtworkConnection = connectionDefinitions({ + nodeType: SaleArtwork.type, +}).connectionType + const SaleType = new GraphQLObjectType({ name: "Sale", fields: () => { @@ -214,6 +220,19 @@ const SaleType = new GraphQLObjectType({ return gravity(`sale/${id}/sale_artworks`, options) }, }, + sale_artworks_connection: { + type: saleArtworkConnection, + args: pageable(), + resolve: (sale, options, request, { rootValue: { saleArtworksLoader } }) => { + const { limit: size, offset } = getPagingParameters(options) + return saleArtworksLoader(sale.id, { size, offset }).then(saleArtworks => + connectionFromArraySlice(saleArtworks, options, { + arrayLength: sale.eligible_sale_artworks_count, + sliceStart: offset, + }) + ) + }, + }, sale_type: { type: GraphQLString, }, diff --git a/test/schema/sale/__snapshots__/index.js.snap b/test/schema/sale/__snapshots__/index.js.snap new file mode 100644 index 0000000000..53bf24370e --- /dev/null +++ b/test/schema/sale/__snapshots__/index.js.snap @@ -0,0 +1,65 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Sale type sale_artworks_connection returns data from gravity 1`] = ` +Object { + "sale": Object { + "sale_artworks_connection": Object { + "edges": Array [ + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + Object { + "node": Object { + "id": "some-id", + }, + }, + ], + "pageInfo": Object { + "hasNextPage": true, + }, + }, + }, +} +`; diff --git a/test/schema/sale/index.js b/test/schema/sale/index.js index 37501e9f90..bda41796f5 100644 --- a/test/schema/sale/index.js +++ b/test/schema/sale/index.js @@ -1,6 +1,7 @@ import moment from "moment" import schema from "schema" -import { runQuery } from "test/utils" +import { fill } from "lodash" +import { runQuery, runAuthenticatedQuery } from "test/utils" describe("Sale type", () => { const Sale = schema.__get__("Sale") @@ -128,6 +129,35 @@ describe("Sale type", () => { }) }) + describe("sale_artworks_connection", () => { + it("returns data from gravity", () => { + const query = ` + { + sale(id: "foo-foo") { + sale_artworks_connection(first: 10) { + pageInfo { + hasNextPage + } + edges { + node { + id + } + } + } + } + } + ` + sale.eligible_sale_artworks_count = 20 + const rootValue = { + saleArtworksLoader: () => Promise.resolve(fill(Array(sale.eligible_sale_artworks_count), { id: "some-id" })), + } + + return runAuthenticatedQuery(query, rootValue).then(data => { + expect(data).toMatchSnapshot() + }) + }) + }) + describe("buyers premium", () => { it("returns a valid object even if the sale has no buyers premium", () => { const query = ` diff --git a/yarn.lock b/yarn.lock index a28314de51..8159f9f0a1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3514,11 +3514,7 @@ moment-timezone@^0.5.5: dependencies: moment ">= 2.6.0" -moment@*: - version "2.18.1" - resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f" - -"moment@>= 2.6.0", moment@^2.14.1: +moment@*, "moment@>= 2.6.0", moment@^2.14.1: version "2.17.0" resolved "https://registry.yarnpkg.com/moment/-/moment-2.17.0.tgz#a4c292e02aac5ddefb29a6eed24f51938dd3b74f"