diff --git a/packages/core/e2e/asset.e2e-spec.ts b/packages/core/e2e/asset.e2e-spec.ts index 9b480ad5f8..4b5e0e9c87 100644 --- a/packages/core/e2e/asset.e2e-spec.ts +++ b/packages/core/e2e/asset.e2e-spec.ts @@ -6,7 +6,7 @@ import gql from 'graphql-tag'; import path from 'path'; import { initialData } from '../../../e2e-common/e2e-initial-data'; -import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config'; +import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config'; import { ASSET_FRAGMENT } from './graphql/fragments'; import { @@ -14,6 +14,7 @@ import { DeleteAsset, DeletionResult, GetAsset, + GetAssetFragmentFirst, GetAssetList, GetProductWithVariants, SortOrder, @@ -121,6 +122,20 @@ describe('Asset resolver', () => { }); }); + /** + * https://github.com/vendure-ecommerce/vendure/issues/459 + */ + it('transforms URL when fragment defined before query (GH issue #459)', async () => { + const { asset } = await adminClient.query< + GetAssetFragmentFirst.Query, + GetAssetFragmentFirst.Variables + >(GET_ASSET_FRAGMENT_FIRST, { + id: firstAssetId, + }); + + expect(asset?.preview).toBe('test-url/test-assets/alexandru-acea-686569-unsplash__preview.jpg'); + }); + describe('createAssets', () => { it('permitted types by mime type', async () => { const filesToUpload = [ @@ -343,6 +358,19 @@ export const GET_ASSET = gql` ${ASSET_FRAGMENT} `; +export const GET_ASSET_FRAGMENT_FIRST = gql` + fragment AssetFragFirst on Asset { + id + preview + } + + query GetAssetFragmentFirst($id: ID!) { + asset(id: $id) { + ...AssetFragFirst + } + } +`; + export const CREATE_ASSETS = gql` mutation CreateAssets($input: [CreateAssetInput!]!) { createAssets(input: $input) { diff --git a/packages/core/e2e/graphql/generated-e2e-admin-types.ts b/packages/core/e2e/graphql/generated-e2e-admin-types.ts index bdf145d238..6189594089 100644 --- a/packages/core/e2e/graphql/generated-e2e-admin-types.ts +++ b/packages/core/e2e/graphql/generated-e2e-admin-types.ts @@ -3744,6 +3744,16 @@ export type GetAssetQuery = { __typename?: 'Query' } & { asset?: Maybe<{ __typename?: 'Asset' } & Pick & AssetFragment>; }; +export type AssetFragFirstFragment = { __typename?: 'Asset' } & Pick; + +export type GetAssetFragmentFirstQueryVariables = { + id: Scalars['ID']; +}; + +export type GetAssetFragmentFirstQuery = { __typename?: 'Query' } & { + asset?: Maybe<{ __typename?: 'Asset' } & AssetFragFirstFragment>; +}; + export type CreateAssetsMutationVariables = { input: Array; }; @@ -5881,6 +5891,16 @@ export namespace GetAsset { export type Asset = AssetFragment; } +export namespace AssetFragFirst { + export type Fragment = AssetFragFirstFragment; +} + +export namespace GetAssetFragmentFirst { + export type Variables = GetAssetFragmentFirstQueryVariables; + export type Query = GetAssetFragmentFirstQuery; + export type Asset = AssetFragFirstFragment; +} + export namespace CreateAssets { export type Variables = CreateAssetsMutationVariables; export type Mutation = CreateAssetsMutation; diff --git a/packages/core/src/api/common/graphql-value-transformer.ts b/packages/core/src/api/common/graphql-value-transformer.ts index 2e1cb1dd95..5ba6b23789 100644 --- a/packages/core/src/api/common/graphql-value-transformer.ts +++ b/packages/core/src/api/common/graphql-value-transformer.ts @@ -114,6 +114,9 @@ export class GraphqlValueTransformer { currentNode = currentNode.parent; } } + if (node.kind === 'FragmentDefinition') { + currentNode = rootNode; + } }, }; for (const operation of document.definitions) {