From 019244f29ec2825a076fa031b720f03c3c3638a2 Mon Sep 17 00:00:00 2001 From: Ash Furrow Date: Fri, 27 Oct 2017 11:30:07 -0400 Subject: [PATCH 1/3] Adds relay connection to sale schema. --- .../loaders_with_authentication/index.js | 1 + schema/sale/index.js | 21 +++++++++++++++++++ yarn.lock | 6 +----- 3 files changed, 23 insertions(+), 5 deletions(-) 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..e8f36fa2e4 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,21 @@ 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) + const gravityArgs = { size, offset } + return saleArtworksLoader(sale.id, gravityArgs).then(saleArtworks => { + console.log(offset) + return connectionFromArraySlice(saleArtworks, options, { + arrayLength: sale.eligible_sale_artworks_count, + sliceStart: offset, + }) + }) + }, + }, sale_type: { type: GraphQLString, }, 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" From 38c06c0805190740778a5096d0aee95f69228cdd Mon Sep 17 00:00:00 2001 From: Ash Furrow Date: Fri, 27 Oct 2017 11:33:57 -0400 Subject: [PATCH 2/3] Cleanup. --- schema/sale/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/schema/sale/index.js b/schema/sale/index.js index e8f36fa2e4..5f73e3ab87 100644 --- a/schema/sale/index.js +++ b/schema/sale/index.js @@ -225,14 +225,12 @@ const SaleType = new GraphQLObjectType({ args: pageable(), resolve: (sale, options, request, { rootValue: { saleArtworksLoader } }) => { const { limit: size, offset } = getPagingParameters(options) - const gravityArgs = { size, offset } - return saleArtworksLoader(sale.id, gravityArgs).then(saleArtworks => { - console.log(offset) - return connectionFromArraySlice(saleArtworks, options, { + return saleArtworksLoader(sale.id, { size, offset }).then(saleArtworks => + connectionFromArraySlice(saleArtworks, options, { arrayLength: sale.eligible_sale_artworks_count, sliceStart: offset, }) - }) + ) }, }, sale_type: { From a445756729153c9580a263eb9ec96dccac91bbc3 Mon Sep 17 00:00:00 2001 From: Ash Furrow Date: Fri, 27 Oct 2017 11:44:59 -0400 Subject: [PATCH 3/3] Adds tests for SaleType's sale_artworks_connection. --- test/schema/sale/__snapshots__/index.js.snap | 65 ++++++++++++++++++++ test/schema/sale/index.js | 32 +++++++++- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/schema/sale/__snapshots__/index.js.snap 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 = `