Browse Source

fix(core): Correctly handle aliases when transforming Asset urls

Fixes #417
Michael Bromley 5 years ago
parent
commit
18bbeeeef4

+ 15 - 18
packages/core/src/api/common/graphql-value-transformer.ts

@@ -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') {
@@ -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 {

+ 4 - 12
packages/core/src/api/middleware/asset-interceptor-plugin.ts

@@ -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) {
@@ -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);
                     }