Skip to content

Commit

Permalink
Merge pull request #4121 from artsy/staging
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
TanjieM authored Jun 6, 2022
2 parents 62097e5 + 5c128b1 commit dfad079
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 27 deletions.
7 changes: 3 additions & 4 deletions _schemaV2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1138,7 +1138,6 @@ type ArticleSponsor {
}

type Artist implements EntityWithFilterArtworksConnectionInterface & Node & Searchable {
activeSecondaryMarket: Boolean
alternateNames: [String]
articlesConnection(
after: String
Expand Down Expand Up @@ -1508,13 +1507,13 @@ type ArtistHighlights {

type ArtistInsight {
# List of entities relevant to the insight.
entities: [String]
entities: [String!]!

# Label to use when displaying the insight.
label: String
label: String!

# The type of insight.
type: String
type: String!
}

type ArtistMeta {
Expand Down
27 changes: 27 additions & 0 deletions src/schema/v2/artist/__tests__/insights.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe("ArtistInsights type", () => {
artist.collections = "Museum of Modern Art (MoMA)"
artist.review_sources = "Artforum International Magazine"
artist.biennials = "frieze"
artist.active_secondary_market = true

const query = `
{
Expand Down Expand Up @@ -102,6 +103,11 @@ describe("ArtistInsights type", () => {
label: "Included in a major biennial",
entities: ["frieze"],
},
{
type: "ACTIVE_SECONDARY_MARKET",
label: "Recent auction results in the Artsy Price Database",
entities: [],
},
])
})
})
Expand Down Expand Up @@ -132,4 +138,25 @@ describe("ArtistInsights type", () => {
])
})
})

it("does not build an insight if boolean value is false", () => {
artist.active_secondary_market = false

const query = `
{
artist(id: "foo-bar") {
id
insights {
type
label
entities
}
}
}
`

return runQuery(query, context).then((data) => {
expect(data!.artist.insights).toEqual([])
})
})
})
4 changes: 0 additions & 4 deletions src/schema/v2/artist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,10 +624,6 @@ export const ArtistType = new GraphQLObjectType<any, ResolverContext>({
},
initials: initials("name"),
insights: ArtistInsights,
activeSecondaryMarket: {
type: GraphQLBoolean,
resolve: ({ active_secondary_market }) => active_secondary_market,
},
isConsignable: {
type: GraphQLBoolean,
resolve: ({ consignable }) => consignable,
Expand Down
63 changes: 44 additions & 19 deletions src/schema/v2/artist/insights.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { compact } from "lodash"

import {
GraphQLObjectType,
GraphQLList,
GraphQLEnumType,
GraphQLString,
GraphQLFieldConfig,
GraphQLNonNull,
} from "graphql"
import { ResolverContext } from "types/graphql"

Expand All @@ -27,10 +27,13 @@ export const ArtistInsightType = new GraphQLEnumType({
BIENNIAL: {
value: "Included in a major biennial",
},
ACTIVE_SECONDARY_MARKET: {
value: "Recent auction results in the Artsy Price Database",
},
},
})

const ArtistInsightTypeMapping = {
const ARTIST_INSIGHT_TYPES = {
solo_show_institutions: { type: ArtistInsightType.getValue("SOLO_SHOW") },
group_show_institutions: { type: ArtistInsightType.getValue("GROUP_SHOW") },
collections: {
Expand All @@ -39,30 +42,52 @@ const ArtistInsightTypeMapping = {
},
review_sources: { type: ArtistInsightType.getValue("REVIEWED") },
biennials: { type: ArtistInsightType.getValue("BIENNIAL") },
active_secondary_market: {
type: ArtistInsightType.getValue("ACTIVE_SECONDARY_MARKET"),
},
}

const buildInsights = (artist) => {
const splitEntities = (entitiesString: string, delimiter): Array<string> => {
const splitEntities = (
entitiesString: string,
delimiter: string
): Array<string> => {
return entitiesString.split(delimiter).map((entity) => {
return entity.trim()
})
}

const buildInsight = (mapping, entitiesString: string) => {
return {
type: mapping.type.name,
entities: splitEntities(entitiesString, mapping.delimiter || "|"),
label: mapping.type.value,
}
}

return compact(
// eslint-disable-next-line array-callback-return
Object.keys(ArtistInsightTypeMapping).map((key) => {
const entitiesString = artist[key] && artist[key].trim()
Object.keys(ARTIST_INSIGHT_TYPES).map((key) => {
const value = artist[key]

if (!value) {
return
}

const mapping = ARTIST_INSIGHT_TYPES[key]

switch (typeof value) {
case "string":
const trimmed = value.trim()

if (!trimmed) return null

return {
entities: splitEntities(trimmed, mapping.delimiter || "|"),
label: mapping.type.value,
type: mapping.type.name,
}

case "boolean":
return {
entities: [],
label: mapping.type.value,
type: mapping.type.name,
}

if (entitiesString) {
return buildInsight(ArtistInsightTypeMapping[key], entitiesString)
default:
return null
}
})
)
Expand All @@ -72,15 +97,15 @@ const ArtistInsight = new GraphQLObjectType<any, ResolverContext>({
name: "ArtistInsight",
fields: {
type: {
type: GraphQLString,
type: new GraphQLNonNull(GraphQLString),
description: "The type of insight.",
},
label: {
type: GraphQLString,
type: new GraphQLNonNull(GraphQLString),
description: "Label to use when displaying the insight.",
},
entities: {
type: new GraphQLList(GraphQLString),
type: new GraphQLNonNull(GraphQLList(new GraphQLNonNull(GraphQLString))),
description: "List of entities relevant to the insight.",
},
},
Expand Down

0 comments on commit dfad079

Please sign in to comment.