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

[experiment] Attempt to upgrade stitching to latest #3375

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
"@babel/preset-env": "7.4.5",
"@babel/preset-typescript": "7.3.3",
"@babel/register": "7.4.4",
"@graphql-tools/delegate": "6.0.10",
"@graphql-tools/delegate": "^8.4.3",
"@graphql-tools/graphql-file-loader": "^7.3.3",
"@graphql-tools/load": "^7.5.0",
"@graphql-tools/schema": "^8.3.1",
"@graphql-tools/stitch": "^8.4.3",
"@graphql-tools/wrap": "^8.3.2",
"@heroku/foreman": "2.0.2",
"@sentry/node": "5.18.1",
"accounting": "0.4.1",
Expand Down
5 changes: 5 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
ENABLE_QUERY_TRACING,
ENABLE_REQUEST_LOGGING,
DISABLE_SCHEMA_STITCHING,
ENABLE_EXPERIMENTAL_STITCHING_MIGRATION,
ENABLE_RESOLVER_BATCHING,
EXCHANGE_API_BASE,
EXCHANGE_APP_ID,
Expand All @@ -61,6 +62,7 @@ const {
IMPULSE_APPLICATION_ID,
INTROSPECT_TOKEN,
IP_BLACKLIST,
LOG_HTTP_LINKS,
LOG_QUERY_DETAILS_THRESHOLD,
MEMCACHED_MAX_POOL,
MEMCACHED_URL,
Expand Down Expand Up @@ -162,6 +164,8 @@ export default {
EMBEDLY_KEY,
ENABLE_APOLLO: ENABLE_APOLLO === "true",
ENABLE_ASYNC_STACK_TRACES,
ENABLE_EXPERIMENTAL_STITCHING_MIGRATION:
ENABLE_EXPERIMENTAL_STITCHING_MIGRATION === "true",
ENABLE_METRICS,
ENABLE_QUERY_TRACING,
ENABLE_REQUEST_LOGGING,
Expand Down Expand Up @@ -190,6 +194,7 @@ export default {
IMPULSE_APPLICATION_ID,
INTROSPECT_TOKEN,
IP_BLACKLIST: IP_BLACKLIST || "",
LOG_HTTP_LINKS,
LOG_QUERY_DETAILS_THRESHOLD,
MEMCACHED_MAX_POOL: Number(MEMCACHED_MAX_POOL) || 10,
MEMCACHED_URL: MEMCACHED_URL || "localhost:11211",
Expand Down
15 changes: 11 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createLoaders } from "./lib/loaders"
import depthLimit from "graphql-depth-limit"
import express from "express"
import { schema as schemaV1 } from "./schema/v1"
import { schema as schemaV2 } from "./schema/v2"
import { getSchema as getSchemaV2 } from "./schema/v2"
import moment from "moment-timezone"
import morgan from "artsy-morgan"
import { fetchPersistedQuery } from "./lib/fetchPersistedQuery"
Expand Down Expand Up @@ -267,8 +267,15 @@ function startApp(appSchema, path: string) {

const app = express()

// This order is important for dd-trace to be able to find the nested routes.
app.use("/v2", startApp(schemaV2, "/"))
app.use("/", startApp(schemaV1, "/"))
;(async () => {
try {
const schemaV2 = await getSchemaV2()
// This order is important for dd-trace to be able to find the nested routes.
app.use("/v2", startApp(schemaV2, "/"))
app.use("/", startApp(schemaV1, "/"))
} catch (error) {
console.log(error)
}
})()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where the double page refresh is required. Below we're returning export default app, which is mounted into the main express app but here we need to wait for getSchemaV2 to return before the routes are actually mounted -- hence the hiccup.


export default app
108 changes: 108 additions & 0 deletions src/lib/stitching2/kaws/__tests__/__snapshots__/schema.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`creates an SDL 1`] = `
"type Collection {
id: ID!
slug: String!
title: String!
description: String
descriptionMarkdown: String
headerImage: String
thumbnail: String
keywords: String!
credit: String
category: String!
query: CollectionQuery!
createdAt: DateTime!
updatedAt: DateTime!
price_guidance: Float @deprecated(reason: \\"Prefer priceGuidance\\")
priceGuidance: Float
show_on_editorial: Boolean! @deprecated(reason: \\"Prefer showOnEditorial\\")
showOnEditorial: Boolean!
is_featured_artist_content: Boolean! @deprecated(reason: \\"Prefer isFeaturedArtistContent\\")
isFeaturedArtistContent: Boolean!
linkedCollections: [CollectionGroup!]!
featuredArtistExclusionIds: [String!]
relatedCollections(size: Int = 10): [Collection!]!
isDepartment: Boolean!
}

type CollectionCategory {
name: String!
collections: [Collection!]!
}

type CollectionGroup {
internalID: ID
groupType: GroupTypes!
name: String!
members: [Collection!]!
}

type CollectionQuery {
id: ID
internalID: ID
acquireable: Boolean
aggregations: [String!]
artist_ids: [String!] @deprecated(reason: \\"Prefer artistIDs\\")
artistIDs: [String!]
artist_id: String @deprecated(reason: \\"Prefer artistID\\")
artistID: String
at_auction: Boolean @deprecated(reason: \\"Prefer atAuction\\")
atAuction: Boolean
color: String
dimension_range: String @deprecated(reason: \\"Prefer dimensionRange\\")
dimensionRange: String
extra_aggregation_gene_ids: [String!] @deprecated(reason: \\"prefer extraAggregationGeneIDs\\")
extraAggregationGeneIDs: [String!]
include_artworks_by_followed_artists: Boolean @deprecated(reason: \\"Prefer includeArtworksByFollowedArtists\\")
includeArtworksByFollowedArtists: Boolean
include_medium_filter_in_aggregation: Boolean @deprecated(reason: \\"Prefer includeMediumFilterInAggregation\\")
includeMediumFilterInAggregation: Boolean
inquireable_only: Boolean @deprecated(reason: \\"Prefer inquireableOnly\\")
inquireableOnly: Boolean
for_sale: Boolean @deprecated(reason: \\"Prefer forSale\\")
forSale: Boolean
gene_id: String @deprecated(reason: \\"Prefer geneID\\")
geneID: String
gene_ids: [String!] @deprecated(reason: \\"Prefer geneIDs\\")
geneIDs: [String!]
height: String
width: String
medium: String
period: String
periods: [String!]
major_periods: [String!] @deprecated(reason: \\"Prefer majorPeriods\\")
majorPeriods: [String!]
partner_id: ID @deprecated(reason: \\"Prefer partnerID\\")
partnerID: ID
partner_cities: [String!] @deprecated(reason: \\"Prefer partnerCities\\")
partnerCities: [String!]
price_range: String @deprecated(reason: \\"Prefer priceRange\\")
priceRange: String
page: Int
sale_id: ID @deprecated(reason: \\"Prefer saleID\\")
saleID: ID
size: Int
sort: String
tag_id: String @deprecated(reason: \\"Prefer tagID\\")
tagID: String
keyword: String
}

scalar DateTime

enum GroupTypes {
ArtistSeries
FeaturedCollections
OtherCollections
}

type Query {
collections(slugs: [String!], category: String, randomizationSeed: String, size: Int, isFeaturedArtistContent: Boolean, showOnEditorial: Boolean, artistID: String): [Collection!]!
categories: [CollectionCategory!]!
collection(slug: String!): Collection
hubCollections: [Collection!]!
}
"
`;
40 changes: 40 additions & 0 deletions src/lib/stitching2/kaws/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { executableKawsSchema } from "../schema"
import {
getTypesFromSchema,
getRootFieldsFromSchema,
} from "lib/stitching/lib/getTypesFromSchema"
import { printSchema } from "graphql"
import { stitchSchemas } from "@graphql-tools/stitch"
import { incrementalMergeSchemas2 } from "lib/stitching2/mergeSchemas"

it("Does not include kaws core types", async () => {
const stitchedSchema = incrementalMergeSchemas2({})
const kawsSchema = await executableKawsSchema()
const schema = stitchSchemas({
subschemas: [kawsSchema],
})
debugger
const kawsTypes = await getTypesFromSchema(schema)

expect(kawsTypes).not.toContain("Collection")
expect(kawsTypes).not.toContain("CollectionCategory")
expect(kawsTypes).not.toContain("CollectionQuery")

expect(kawsTypes).toContain("MarketingCollection")
})

it("Does not include the root query fields", async () => {
const kawsSchema = await executableKawsSchema()
const rootFields = await getRootFieldsFromSchema(kawsSchema.schema)

expect(rootFields).not.toContain("categories")
expect(rootFields).not.toContain("collection")
expect(rootFields).not.toContain("collections")
})

it("creates an SDL", async () => {
const kawsSchema = await executableKawsSchema()
expect(
printSchema(kawsSchema.schema, { commentDescriptions: true })
).toMatchSnapshot()
})
118 changes: 118 additions & 0 deletions src/lib/stitching2/kaws/__tests__/stitching.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import {
getKawsMergedSchema,
getKawsStitchedSchema,
} from "lib/stitching/kaws/__tests__/testingUtils"
import { getFieldsForTypeFromSchema } from "lib/stitching/lib/getTypesFromSchema"

describe("KAWS Stitching", () => {
it("extends the Viewer object", async () => {
const mergedSchema = await getKawsMergedSchema()
const viewerFields = await getFieldsForTypeFromSchema(
"Viewer",
mergedSchema
)
expect(viewerFields).toContain("marketingCollections")
})

describe("HomePageMarketingCollectionsModule", () => {
it("extends the HomePageMarketingCollectionsModule object", async () => {
const mergedSchema = await getKawsMergedSchema()
const homePageMarketingCollectionsModuleFields = await getFieldsForTypeFromSchema(
"HomePageMarketingCollectionsModule",
mergedSchema
)
expect(homePageMarketingCollectionsModuleFields).toContain("results")
})

it("returns an array even if the kaws request fails", async () => {
const { resolvers } = await getKawsStitchedSchema()
const resultsResolver =
resolvers.HomePageMarketingCollectionsModule.results.resolve
const delegateToSchemaMock = jest.fn()
delegateToSchemaMock.mockRejectedValue(
"simulating a kaws request failure"
)
const mergeInfo = { delegateToSchema: delegateToSchemaMock }
const results = await resultsResolver({}, {}, {}, { mergeInfo })
expect(results).toEqual([])
})
})

describe("MarketingCollection", () => {
it("extends the HomePageMarketingCollectionsModule object", async () => {
const mergedSchema = await getKawsMergedSchema()
const fields = await getFieldsForTypeFromSchema(
"MarketingCollection",
mergedSchema
)
expect(fields).toContain("internalID")
})

it("returns the id", async () => {
const { resolvers } = await getKawsStitchedSchema()
const resolver = resolvers.MarketingCollection.internalID.resolve
const result = resolver({ id: "percy" }, {}, {}, {})
expect(result).toEqual("percy")
})
})

describe("marketingCollections", () => {
it("passes artist internalID to kaws' artistID arg when querying `... on Artist`", async () => {
const { resolvers } = await getKawsStitchedSchema()
const marketingCollectionsResolver =
resolvers.Artist.marketingCollections.resolve
const mergeInfo = { delegateToSchema: jest.fn() }

await marketingCollectionsResolver(
{ internalID: "artist-internal-id" },
{},
{},
{ mergeInfo }
)

expect(mergeInfo.delegateToSchema).toHaveBeenCalledWith(
expect.objectContaining({
args: { artistID: "artist-internal-id" },
fieldName: "marketingCollections",
})
)
})

it("passes through slugs when stitched under a fair", async () => {
const { resolvers } = await getKawsStitchedSchema()
const marketingCollectionsResolver =
resolvers.Fair.marketingCollections.resolve
const mergeInfo = { delegateToSchema: jest.fn() }

await marketingCollectionsResolver(
{ kawsCollectionSlugs: ["catty-collection"] },
{},
{},
{ mergeInfo }
)

expect(mergeInfo.delegateToSchema).toHaveBeenCalledWith(
expect.objectContaining({
args: { slugs: ["catty-collection"] },
fieldName: "marketingCollections",
})
)
})

it("returns an empty list when there are no kawsCollectionSlugs", async () => {
const { resolvers } = await getKawsStitchedSchema()
const marketingCollectionsResolver =
resolvers.Fair.marketingCollections.resolve
const mergeInfo = { delegateToSchema: jest.fn() }

const result = await marketingCollectionsResolver(
{ kawsCollectionSlugs: [] },
{},
{},
{ mergeInfo }
)

expect(result).toEqual([])
})
})
})
45 changes: 45 additions & 0 deletions src/lib/stitching2/kaws/__tests__/testingUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { mergeSchemas } from "graphql-tools"
import { kawsStitchingEnvironmentV2 } from "../v2/stitching"
import { GraphQLSchema } from "graphql"
import { executableKawsSchema } from "../schema"
import localSchema from "schema/v2/schema"

let cachedSchema: GraphQLSchema & { transforms: any }
let stitchedSchema: ReturnType<typeof kawsStitchingEnvironmentV2>
let mergedSchema: GraphQLSchema & { transforms: any }

/** Gets a cached copy of the transformed kaws schema */
export const getKawsTransformedSchema = async () => {
if (!cachedSchema) {
cachedSchema = await executableKawsSchema()
}
return cachedSchema
}

/** Gets a cached copy of the stitched schema, independent of being merged into the local schema */
export const getKawsStitchedSchema = async () => {
if (!stitchedSchema) {
const cachedSchema = await getKawsTransformedSchema()
stitchedSchema = kawsStitchingEnvironmentV2(localSchema, cachedSchema)
}
return stitchedSchema
}

/** Gets a cached fully setup schema with kaws and the localSchema set up */
export const getKawsMergedSchema = async () => {
if (!stitchedSchema) {
const cachedSchema = await getKawsTransformedSchema()
const { extensionSchema, resolvers } = await getKawsStitchedSchema()

// The order should only matter in that extension schemas come after the
// objects that they are expected to build upon
mergedSchema = mergeSchemas({
schemas: [localSchema, cachedSchema, extensionSchema],
resolvers: resolvers,
}) as GraphQLSchema & { transforms: any }

const anyMergedSchema = mergedSchema as any
anyMergedSchema.__allowedLegacyNames = ["__id"]
}
return mergedSchema
}
Loading