Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Relay connection for sale artworks to sale schema #794

Merged
merged 3 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/loaders/loaders_with_authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
19 changes: 19 additions & 0 deletions schema/sale/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -51,6 +53,10 @@ const BuyersPremium = new GraphQLObjectType({
},
})

const saleArtworkConnection = connectionDefinitions({
nodeType: SaleArtwork.type,
}).connectionType

const SaleType = new GraphQLObjectType({
name: "Sale",
fields: () => {
Expand Down Expand Up @@ -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,
},
Expand Down
65 changes: 65 additions & 0 deletions test/schema/sale/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
@@ -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,
},
},
},
}
`;
32 changes: 31 additions & 1 deletion test/schema/sale/index.js
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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 = `
Expand Down
6 changes: 1 addition & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down