-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip - schema returned, delegateToSchema undefined
- Loading branch information
Showing
11 changed files
with
613 additions
and
18 deletions.
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,215 @@ | ||
import { GraphQLSchema, GraphQLFieldConfigArgumentMap } from "graphql" | ||
import { | ||
pageableFilterArtworksArgsWithInput, | ||
filterArtworksArgs, | ||
} from "schema/v2/filterArtworksConnection" | ||
import gql from "lib/gql" | ||
import { printType } from "lib/stitching/lib/printType" | ||
|
||
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: { | ||
fragment: ` | ||
... on Artist { | ||
internalID | ||
} | ||
`, | ||
resolve: ({ internalID: artistID }, args, context, info) => { | ||
return info.mergeInfo.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.mergeInfo.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.