Skip to content

Commit

Permalink
fix(core): Correctly handle aliases when transforming Asset urls
Browse files Browse the repository at this point in the history
Fixes #417
  • Loading branch information
michaelbromley committed Aug 17, 2020
1 parent c4ca2d0 commit 18bbeee
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 30 deletions.
33 changes: 15 additions & 18 deletions packages/core/src/api/common/graphql-value-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class GraphqlValueTransformer {
parent: currentNode,
children: {},
};
currentNode.children[fieldDef.name] = newNode;
currentNode.children[node.alias?.value ?? node.name.value] = newNode;
currentNode = newNode;
}
if (node.kind === 'FragmentSpread') {
Expand Down Expand Up @@ -186,23 +186,20 @@ export class GraphqlValueTransformer {
inputType: GraphQLInputObjectType,
parent: TypeTreeNode,
): { [name: string]: TypeTreeNode } {
return Object.entries(inputType.getFields()).reduce(
(result, [key, field]) => {
const namedType = getNamedType(field.type);
const child: TypeTreeNode = {
type: namedType,
isList: this.isList(field.type),
parent,
fragmentRefs: [],
children: {},
};
if (isInputObjectType(namedType)) {
child.children = this.getChildrenTreeNodes(namedType, child);
}
return { ...result, [key]: child };
},
{} as { [name: string]: TypeTreeNode },
);
return Object.entries(inputType.getFields()).reduce((result, [key, field]) => {
const namedType = getNamedType(field.type);
const child: TypeTreeNode = {
type: namedType,
isList: this.isList(field.type),
parent,
fragmentRefs: [],
children: {},
};
if (isInputObjectType(namedType)) {
child.children = this.getChildrenTreeNodes(namedType, child);
}
return { ...result, [key]: child };
}, {} as { [name: string]: TypeTreeNode });
}

private isList(t: any): boolean {
Expand Down
16 changes: 4 additions & 12 deletions packages/core/src/api/middleware/asset-interceptor-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class AssetInterceptorPlugin implements ApolloServerPlugin {
return;
}
this.graphqlValueTransformer.transformValues(typeTree, data, (value, type) => {
const isAssetType = type && type.name === 'Asset';
const isAssetType = type && (type.name === 'Asset' || type.name === 'SearchResultAsset');
if (isAssetType) {
if (value && !Array.isArray(value)) {
if (value.preview) {
Expand All @@ -60,20 +60,12 @@ export class AssetInterceptorPlugin implements ApolloServerPlugin {
}
}
}

// TODO: This path is deprecated and should be removed in a future version
// once the fields are removed from the GraphQL API
const isSearchResultType = type && type.name === 'SearchResult';
if (isSearchResultType) {
if (value && !Array.isArray(value)) {
if (value.productAsset) {
value.productAsset.preview = toAbsoluteUrl(request, value.productAsset.preview);
}
if (value.productVariantAsset) {
value.productVariantAsset.preview = toAbsoluteUrl(
request,
value.productVariantAsset.preview,
);
}
// TODO: This path is deprecated and should be removed in a future version
// once the fields are removed from the GraphQL API
if (value.productPreview) {
value.productPreview = toAbsoluteUrl(request, value.productPreview);
}
Expand Down

0 comments on commit 18bbeee

Please sign in to comment.