Skip to content

Commit

Permalink
fix(core): Correctly parse fragments defined before operations
Browse files Browse the repository at this point in the history
Fixes #459
  • Loading branch information
michaelbromley committed Sep 8, 2020
1 parent 5f47773 commit 44a9ab9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
30 changes: 29 additions & 1 deletion packages/core/e2e/asset.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ 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 {
CreateAssets,
DeleteAsset,
DeletionResult,
GetAsset,
GetAssetFragmentFirst,
GetAssetList,
GetProductWithVariants,
SortOrder,
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3744,6 +3744,16 @@ export type GetAssetQuery = { __typename?: 'Query' } & {
asset?: Maybe<{ __typename?: 'Asset' } & Pick<Asset, 'width' | 'height'> & AssetFragment>;
};

export type AssetFragFirstFragment = { __typename?: 'Asset' } & Pick<Asset, 'id' | 'preview'>;

export type GetAssetFragmentFirstQueryVariables = {
id: Scalars['ID'];
};

export type GetAssetFragmentFirstQuery = { __typename?: 'Query' } & {
asset?: Maybe<{ __typename?: 'Asset' } & AssetFragFirstFragment>;
};

export type CreateAssetsMutationVariables = {
input: Array<CreateAssetInput>;
};
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/api/common/graphql-value-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ export class GraphqlValueTransformer {
currentNode = currentNode.parent;
}
}
if (node.kind === 'FragmentDefinition') {
currentNode = rootNode;
}
},
};
for (const operation of document.definitions) {
Expand Down

0 comments on commit 44a9ab9

Please sign in to comment.