Skip to content

Commit

Permalink
feat [CX-2605]: Add the list of the artists in My Collection to myCol…
Browse files Browse the repository at this point in the history
…lectionInfo (#4127)

* expose my collection artists endpoint

* add totalCount

* add tests

* update description and use paginationResolver

* add size and page to `pageable`

* use `page` instead of `offset` for pagination

* chore

* refactor promise chaining to async/await
  • Loading branch information
MrSltun authored Jun 10, 2022
1 parent 9b85a8d commit fb379b0
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
20 changes: 20 additions & 0 deletions _schemaV2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -10482,6 +10482,16 @@ enum MyCollectionArtworkSorts {
type MyCollectionConnection {
artistsCount: Int!
artworksCount: Int!

# A connection of artists in the users' collection
collectedArtistsConnection(
after: String
before: String
first: Int
last: Int
page: Int
size: Int
): ArtistConnection
default: Boolean!
description: String!

Expand Down Expand Up @@ -10554,6 +10564,16 @@ type MyCollectionEdge {
type MyCollectionInfo {
artistsCount: Int!
artworksCount: Int!

# A connection of artists in the users' collection
collectedArtistsConnection(
after: String
before: String
first: Int
last: Int
page: Int
size: Int
): ArtistConnection
default: Boolean!
description: String!
includesPurchasedArtworks: Boolean!
Expand Down
97 changes: 97 additions & 0 deletions src/schema/v2/me/__tests__/myCollectionInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,101 @@ describe("me.myCollectionInfo", () => {
}
`)
})

describe("collectedArtistsConnection", () => {
it("returns a connection for the artists in the users' collection", async () => {
const query = gql`
{
me {
myCollectionInfo {
collectedArtistsConnection(first: 5) {
totalCount
edges {
node {
name
}
}
}
}
}
}
`

const context: Partial<ResolverContext> = {
collectionLoader: async () => ({}),
meLoader: async () => ({
id: "some-user-id",
}),
collectionArtistsLoader: async () => ({
headers: { "x-total-count": "3" },
body: [
{
name: "Artist 1",
},
{
name: "Artist 2",
},
{
name: "Artist 3",
},
],
}),
}

const data = await runAuthenticatedQuery(query, context)

expect(data).toEqual(collectedArtistsConnectionData)

expect(data.me.myCollectionInfo.collectedArtistsConnection)
.toMatchInlineSnapshot(`
Object {
"edges": Array [
Object {
"node": Object {
"name": "Artist 1",
},
},
Object {
"node": Object {
"name": "Artist 2",
},
},
Object {
"node": Object {
"name": "Artist 3",
},
},
],
"totalCount": 3,
}
`)
})
})
})

const collectedArtistsConnectionData = {
me: {
myCollectionInfo: {
collectedArtistsConnection: {
totalCount: 3,
edges: [
{
node: {
name: "Artist 1",
},
},
{
node: {
name: "Artist 2",
},
},
{
node: {
name: "Artist 3",
},
},
],
},
},
},
}
28 changes: 28 additions & 0 deletions src/schema/v2/me/myCollectionInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
GraphQLFieldConfig,
GraphQLInt,
} from "graphql"
import { convertConnectionArgsToGravityArgs } from "lib/helpers"
import { pageable } from "relay-cursor-paging"
import { artistConnection } from "schema/v2/artist"
import { ResolverContext } from "types/graphql"
import { paginationResolver } from "../fields/pagination"

export const myCollectionInfoFields = {
description: {
Expand All @@ -33,6 +37,30 @@ export const myCollectionInfoFields = {
type: new GraphQLNonNull(GraphQLInt),
resolve: ({ artists_count }) => artists_count,
},
collectedArtistsConnection: {
description: "A connection of artists in the users' collection",
type: artistConnection.connectionType,
args: pageable({
page: { type: GraphQLInt },
size: { type: GraphQLInt },
}),
resolve: async (_root, args, context) => {
const { collectionArtistsLoader } = context

if (!collectionArtistsLoader) return

const { page, offset, size } = convertConnectionArgsToGravityArgs(args)

const { body, headers } = await collectionArtistsLoader("my-collection", {
size,
page,
total_count: true,
})
const totalCount = parseInt(headers["x-total-count"] || "0", 10)

return paginationResolver({ totalCount, offset, size, page, body, args })
},
},
}

const MyCollectionInfoType = new GraphQLObjectType<any, ResolverContext>({
Expand Down

0 comments on commit fb379b0

Please sign in to comment.