-
Notifications
You must be signed in to change notification settings - Fork 89
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
damassi
wants to merge
6
commits into
main
Choose a base branch
from
damassi/upgrade-stitching-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import urljoin from "url-join" | ||
import config from "config" | ||
import { createRemoteExecutor } from "../lib/createRemoteExecutor" | ||
import { responseLoggerMiddleware } from "../middleware/responseLoggerMiddleware" | ||
|
||
const { KAWS_API_BASE } = config | ||
|
||
export const createKawsExecutor = () => { | ||
return createRemoteExecutor(urljoin(KAWS_API_BASE, "graphql"), { | ||
middleware: [responseLoggerMiddleware("Kaws")], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createKawsExecutor } from "./link" | ||
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader" | ||
import { RenameTypes, RenameRootFields } from "@graphql-tools/wrap" | ||
import { loadSchema } from "@graphql-tools/load" | ||
import { GraphQLSchema } from "graphql" | ||
|
||
export const executableKawsSchema = async () => { | ||
const kawsExecutor = createKawsExecutor() | ||
const kawsSchema: GraphQLSchema = await loadSchema("src/data/kaws.graphql", { | ||
loaders: [new GraphQLFileLoader()], | ||
}) | ||
|
||
const schema = { | ||
schema: kawsSchema, | ||
executor: kawsExecutor, | ||
transforms: [ | ||
new RenameTypes((name) => { | ||
return `Marketing${name}` | ||
}), | ||
new RenameRootFields( | ||
(_operation, name) => | ||
`marketing${name.charAt(0).toUpperCase() + name.slice(1)}` | ||
), | ||
], | ||
} | ||
|
||
return schema | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
import { GraphQLSchema, GraphQLFieldConfigArgumentMap } from "graphql" | ||
import { | ||
pageableFilterArtworksArgsWithInput, | ||
filterArtworksArgs, | ||
} from "schema/v2/filterArtworksConnection" | ||
import gql from "lib/gql" | ||
import { printType } from "lib/stitching/lib/printType" | ||
import { delegateToSchema } from "@graphql-tools/delegate" | ||
|
||
export const kawsStitchingEnvironmentV2 = ( | ||
localSchema: GraphQLSchema, | ||
kawsSchema: GraphQLSchema & { transforms?: any } | ||
) => { | ||
return { | ||
// The SDL used to declare how to stitch an object | ||
extensionSchema: gql` | ||
extend type Artist { | ||
marketingCollections(slugs: [String!], category: String, randomizationSeed: String, size: Int, isFeaturedArtistContent: Boolean, showOnEditorial: Boolean): [MarketingCollection] | ||
} | ||
extend type Fair { | ||
marketingCollections(size: Int): [MarketingCollection]! | ||
} | ||
extend type Viewer { | ||
marketingCollections(slugs: [String!], category: String, randomizationSeed: String, size: Int, isFeaturedArtistContent: Boolean, showOnEditorial: Boolean, artistID: String): [MarketingCollection] | ||
} | ||
extend type MarketingCollection { | ||
internalID: ID! | ||
artworksConnection(${argsToSDL( | ||
pageableFilterArtworksArgsWithInput | ||
).join("\n")}): FilterArtworksConnection | ||
} | ||
type HomePageMarketingCollectionsModule { | ||
results: [MarketingCollection]! | ||
} | ||
extend type HomePage { | ||
marketingCollectionsModule: HomePageMarketingCollectionsModule | ||
} | ||
`, | ||
// Resolvers for the above, this passes in ALL potential parameters | ||
// from KAWS into filter_artworks to allow end users to dynamically | ||
// modify query filters using an admin tool | ||
resolvers: { | ||
Artist: { | ||
marketingCollections: { | ||
selectionSet: `{ internalID }`, | ||
resolve: ({ internalID: artistID }, args, context, info) => { | ||
return delegateToSchema({ | ||
schema: kawsSchema, | ||
operation: "query", | ||
fieldName: "marketingCollections", | ||
args: { | ||
artistID, | ||
...args, | ||
}, | ||
context, | ||
info, | ||
}) | ||
}, | ||
}, | ||
}, | ||
Fair: { | ||
marketingCollections: { | ||
fragment: ` | ||
... on Fair { | ||
kawsCollectionSlugs | ||
} | ||
`, | ||
resolve: ({ kawsCollectionSlugs: slugs }, args, context, info) => { | ||
if (slugs.length === 0) return [] | ||
return info.stitchingInfo.delegateToSchema({ | ||
schema: kawsSchema, | ||
operation: "query", | ||
fieldName: "marketingCollections", | ||
|
||
args: { | ||
slugs, | ||
...args, | ||
}, | ||
context, | ||
info, | ||
}) | ||
}, | ||
}, | ||
}, | ||
HomePage: { | ||
marketingCollectionsModule: { | ||
fragment: gql` | ||
... on HomePage { | ||
__typename | ||
} | ||
`, | ||
resolve: () => { | ||
return {} | ||
}, | ||
}, | ||
}, | ||
HomePageMarketingCollectionsModule: { | ||
results: { | ||
fragment: gql` | ||
... on HomePageMarketingCollectionsModule { | ||
__typename | ||
} | ||
`, | ||
resolve: async (_source, _args, context, info) => { | ||
try { | ||
// We hard-code the collections slugs here in MP so that the app | ||
// can display different collections based only on an MP change | ||
// (and not an app deploy). | ||
return await info.mergeInfo.delegateToSchema({ | ||
schema: kawsSchema, | ||
operation: "query", | ||
fieldName: "marketingCollections", | ||
args: { | ||
slugs: [ | ||
"new-this-week", | ||
"auction-highlights", | ||
"trending-emerging-artists", | ||
], | ||
}, | ||
context, | ||
info, | ||
}) | ||
} catch (error) { | ||
// The schema guarantees a present array for results, so fall back | ||
// to an empty one if the request to kaws fails. Note that we | ||
// still bubble-up any errors in the GraphQL response. | ||
return [] | ||
} | ||
}, | ||
}, | ||
}, | ||
Viewer: { | ||
marketingCollections: { | ||
fragment: gql` | ||
...on Viewer { | ||
__typename | ||
} | ||
`, | ||
resolve: async (_source, args, context, info) => { | ||
return await info.mergeInfo.delegateToSchema({ | ||
schema: kawsSchema, | ||
operation: "query", | ||
fieldName: "marketingCollections", | ||
|
||
args, | ||
context, | ||
info, | ||
}) | ||
}, | ||
}, | ||
}, | ||
MarketingCollection: { | ||
artworksConnection: { | ||
fragment: ` | ||
fragment MarketingCollectionQuery on MarketingCollection { | ||
query { | ||
${Object.keys(filterArtworksArgs).join("\n")} | ||
} | ||
} | ||
`, | ||
resolve: (parent, _args, context, info) => { | ||
const query = parent.query | ||
const hasKeyword = Boolean(parent.query.keyword) | ||
|
||
const existingLoader = | ||
context.unauthenticatedLoaders.filterArtworksLoader | ||
const newLoader = (loaderParams) => { | ||
return existingLoader.call(null, loaderParams, { | ||
requestThrottleMs: 1000 * 60 * 60, | ||
}) | ||
} | ||
|
||
// TODO: Should this really modify the context in place? | ||
context.unauthenticatedLoaders.filterArtworksLoader = newLoader | ||
|
||
return info.mergeInfo.delegateToSchema({ | ||
schema: localSchema, | ||
operation: "query", | ||
fieldName: "artworksConnection", | ||
args: { | ||
...query, | ||
keywordMatchExact: hasKeyword, | ||
..._args, | ||
}, | ||
context, | ||
info, | ||
}) | ||
}, | ||
}, | ||
internalID: { | ||
fragment: ` | ||
fragment MarketingCollectionIDQuery on MarketingCollection { | ||
id | ||
} | ||
`, | ||
resolve: ({ id }, _args, _context, _info) => id, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
// Very contrived version of what exists in graphql-js but isn’t exported. | ||
// https://github.com/graphql/graphql-js/blob/master/src/utilities/schemaPrinter.js | ||
function argsToSDL(args: GraphQLFieldConfigArgumentMap) { | ||
const result: string[] = [] | ||
Object.keys(args).forEach((argName) => { | ||
result.push(`${argName}: ${printType(args[argName].type)}`) | ||
}) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import fetch from "node-fetch" | ||
import { print } from "graphql" | ||
import { ExecutionParams, Executor } from "@graphql-tools/delegate" | ||
import { ResolverContext } from "types/graphql" | ||
|
||
/** | ||
* The parameter that's passed down to an executor's middleware | ||
*/ | ||
interface ExecutorMiddlewareOperationParameter | ||
extends ExecutionParams<unknown, ResolverContext> { | ||
/** The operation's parsed result payload */ | ||
result: unknown | ||
/** A stringified representation of the operation */ | ||
text: string | ||
} | ||
|
||
export type ExecutorMiddleware = ( | ||
operation: ExecutorMiddlewareOperationParameter | ||
) => ExecutorMiddlewareOperationParameter | ||
|
||
interface ExecutorOptions { | ||
/** Middleware runs at the end of the operation execution */ | ||
middleware?: ExecutorMiddleware[] | ||
} | ||
|
||
/** | ||
* | ||
* @param graphqlURI URI to the remote graphql service | ||
* @param options Object used to specify middleware or other configuration for the executor | ||
*/ | ||
export const createRemoteExecutor = ( | ||
graphqlURI: string, | ||
options: ExecutorOptions = {} | ||
) => { | ||
const { middleware = [] } = options | ||
|
||
return async ({ | ||
document, | ||
variables, | ||
...otherOptions | ||
}): Promise<Executor> => { | ||
const query = print(document) | ||
const fetchResult = await fetch(graphqlURI, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ query, variables }), | ||
}) | ||
const result = await fetchResult.json() | ||
if (middleware.length) { | ||
return middleware.reduce( | ||
(acc, middleware) => | ||
middleware({ | ||
document, | ||
variables, | ||
text: query, | ||
result: acc, | ||
...otherOptions, | ||
}), | ||
result | ||
).result | ||
} | ||
return result | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 forgetSchemaV2
to return before the routes are actually mounted -- hence the hiccup.