Skip to content

Commit

Permalink
feat(core): Support "defaultValue" field in ConfigArgs
Browse files Browse the repository at this point in the history
Relates to #643
  • Loading branch information
michaelbromley committed Jan 27, 2021
1 parent 9940385 commit 92ae819
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,7 @@ export type ConfigArgDefinition = {
type: Scalars['String'];
list: Scalars['Boolean'];
required: Scalars['Boolean'];
defaultValue?: Maybe<Scalars['String']>;
label?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
ui?: Maybe<Scalars['JSON']>;
Expand All @@ -2490,6 +2491,7 @@ export type DeletionResponse = {

export type ConfigArgInput = {
name: Scalars['String'];
/** A JSON stringified representation of the actual value */
value: Scalars['String'];
};

Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/generated-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ export type ConfigArgDefinition = {
type: Scalars['String'];
list: Scalars['Boolean'];
required: Scalars['Boolean'];
defaultValue?: Maybe<Scalars['String']>;
label?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
ui?: Maybe<Scalars['JSON']>;
Expand All @@ -674,6 +675,7 @@ export type DeletionResponse = {

export type ConfigArgInput = {
name: Scalars['String'];
/** A JSON stringified representation of the actual value */
value: Scalars['String'];
};

Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/generated-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2652,6 +2652,7 @@ export type ConfigArgDefinition = {
type: Scalars['String'];
list: Scalars['Boolean'];
required: Scalars['Boolean'];
defaultValue?: Maybe<Scalars['String']>;
label?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
ui?: Maybe<Scalars['JSON']>;
Expand All @@ -2678,6 +2679,7 @@ export type DeletionResponse = {

export type ConfigArgInput = {
name: Scalars['String'];
/** A JSON stringified representation of the actual value */
value: Scalars['String'];
};

Expand Down
40 changes: 38 additions & 2 deletions packages/core/e2e/configurable-operation.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
import { pick } from '@vendure/common/lib/pick';
import { LanguageCode, mergeConfig, ShippingEligibilityChecker } from '@vendure/core';
import { createTestEnvironment } from '@vendure/testing';
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 { UPDATE_SHIPPING_METHOD } from '../../admin-ui/src/lib/core/src/data/definitions/shipping-definitions';

import { UpdateShippingMethod } from './graphql/generated-e2e-admin-types';
import { GetCheckers, UpdateShippingMethod } from './graphql/generated-e2e-admin-types';
import { UPDATE_SHIPPING_METHOD } from './graphql/shared-definitions';
import { assertThrowsWithMessage } from './utils/assert-throws-with-message';

const testShippingEligibilityChecker = new ShippingEligibilityChecker({
code: 'test-checker',
description: [{ languageCode: LanguageCode.en, value: 'test checker' }],
args: {
optional: {
label: [
{ languageCode: LanguageCode.en, value: 'Optional argument' },
{ languageCode: LanguageCode.de, value: 'Optional eingabe' },
],
description: [
{ languageCode: LanguageCode.en, value: 'This is an optional argument' },
{ languageCode: LanguageCode.de, value: 'Das ist eine optionale eingabe' },
],
required: false,
type: 'string',
},
required: {
required: true,
type: 'string',
defaultValue: 'hello',
},
},
check: ctx => true,
Expand Down Expand Up @@ -100,4 +111,29 @@ describe('Configurable operations', () => {
}, "The argument 'required' is required, but the value is [null]"),
);
});

it('defaultValue', async () => {
const { shippingEligibilityCheckers } = await adminClient.query<GetCheckers.Query>(GET_CHECKERS);
expect(shippingEligibilityCheckers[0].args.map(pick(['name', 'defaultValue']))).toEqual([
{ name: 'optional', defaultValue: null },
{ name: 'required', defaultValue: 'hello' },
]);
});
});

export const GET_CHECKERS = gql`
query GetCheckers {
shippingEligibilityCheckers {
code
args {
defaultValue
description
label
list
name
required
type
}
}
}
`;
30 changes: 30 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-admin-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,7 @@ export type ConfigArgDefinition = {
type: Scalars['String'];
list: Scalars['Boolean'];
required: Scalars['Boolean'];
defaultValue?: Maybe<Scalars['String']>;
label?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
ui?: Maybe<Scalars['JSON']>;
Expand All @@ -2490,6 +2491,7 @@ export type DeletionResponse = {

export type ConfigArgInput = {
name: Scalars['String'];
/** A JSON stringified representation of the actual value */
value: Scalars['String'];
};

Expand Down Expand Up @@ -4604,6 +4606,21 @@ export type GetProductCollectionsWithParentQuery = {
>;
};

export type GetCheckersQueryVariables = Exact<{ [key: string]: never }>;

export type GetCheckersQuery = {
shippingEligibilityCheckers: Array<
Pick<ConfigurableOperationDefinition, 'code'> & {
args: Array<
Pick<
ConfigArgDefinition,
'defaultValue' | 'description' | 'label' | 'list' | 'name' | 'required' | 'type'
>
>;
}
>;
};

export type DeleteCountryMutationVariables = Exact<{
id: Scalars['ID'];
}>;
Expand Down Expand Up @@ -6551,6 +6568,19 @@ export namespace GetProductCollectionsWithParent {
>;
}

export namespace GetCheckers {
export type Variables = GetCheckersQueryVariables;
export type Query = GetCheckersQuery;
export type ShippingEligibilityCheckers = NonNullable<
NonNullable<GetCheckersQuery['shippingEligibilityCheckers']>[number]
>;
export type Args = NonNullable<
NonNullable<
NonNullable<NonNullable<GetCheckersQuery['shippingEligibilityCheckers']>[number]>['args']
>[number]
>;
}

export namespace DeleteCountry {
export type Variables = DeleteCountryMutationVariables;
export type Mutation = DeleteCountryMutation;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/e2e/graphql/generated-e2e-shop-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,7 @@ export type ConfigArgDefinition = {
type: Scalars['String'];
list: Scalars['Boolean'];
required: Scalars['Boolean'];
defaultValue?: Maybe<Scalars['String']>;
label?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
ui?: Maybe<Scalars['JSON']>;
Expand All @@ -646,6 +647,7 @@ export type DeletionResponse = {

export type ConfigArgInput = {
name: Scalars['String'];
/** A JSON stringified representation of the actual value */
value: Scalars['String'];
};

Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/api/schema/common/common-types.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ConfigArgDefinition {
type: String!
list: Boolean!
required: Boolean!
defaultValue: String
label: String
description: String
ui: JSON
Expand All @@ -63,6 +64,7 @@ type DeletionResponse {

input ConfigArgInput {
name: String!
"A JSON stringified representation of the actual value"
value: String!
}

Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/common/configurable-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type UiComponentConfig =
export interface ConfigArgCommonDef<T extends ConfigArgType> {
type: T;
required?: boolean;
defaultValue?: ConfigArgTypeToTsType<T>;
list?: boolean;
label?: LocalizedStringArray;
description?: LocalizedStringArray;
Expand Down Expand Up @@ -185,6 +186,23 @@ export type ConfigArgDefToType<D extends ConfigArgDef<ConfigArgType>> = D extend
? string[]
: string;

/**
* Converts a ConfigArgType to a TypeScript type
*
* ConfigArgTypeToTsType<'int'> -> number
*/
export type ConfigArgTypeToTsType<T extends ConfigArgType> = T extends 'string'
? string
: T extends 'int'
? number
: T extends 'float'
? number
: T extends 'boolean'
? boolean
: T extends 'datetime'
? Date
: ID;

/**
* Converts a TS type to a ConfigArgDef, e.g:
*
Expand Down Expand Up @@ -362,6 +380,7 @@ export class ConfigurableOperationDef<T extends ConfigArgs = ConfigArgs> {
type: arg.type,
list: arg.list ?? false,
required: arg.required ?? true,
defaultValue: arg.defaultValue,
ui: arg.ui,
label: arg.label && localizeString(arg.label, ctx.languageCode),
description: arg.description && localizeString(arg.description, ctx.languageCode),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const examplePaymentHandler = new PaymentMethodHandler({
code: 'example-payment-provider',
description: [{ languageCode: LanguageCode.en, value: 'Example Payment Provider' }],
args: {
automaticCapture: { type: 'boolean' },
apiKey: { type: 'string' },
automaticCapture: { type: 'boolean', required: false },
apiKey: { type: 'string', required: false },
},
createPayment: async (ctx, order, amount, args, metadata): Promise<CreatePaymentResult> => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const containsProducts = new PromotionCondition({
{ languageCode: LanguageCode.en, value: 'Buy at least { minimum } of the specified products' },
],
args: {
minimum: { type: 'int' },
minimum: {
type: 'int',
defaultValue: 1,
},
productVariantIds: {
type: 'ID',
list: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const hasFacetValues = new PromotionCondition({
{ languageCode: LanguageCode.en, value: 'Buy at least { minimum } products with the given facets' },
],
args: {
minimum: { type: 'int' },
minimum: { type: 'int', defaultValue: 1 },
facets: { type: 'ID', list: true, ui: { component: 'facet-value-form-input' } },
},
init(injector) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export const minimumOrderAmount = new PromotionCondition({
args: {
amount: {
type: 'int',
defaultValue: 100,
ui: { component: 'currency-form-input' },
},
taxInclusive: { type: 'boolean' },
taxInclusive: { type: 'boolean', defaultValue: false },
},
check(ctx, order, args) {
if (args.taxInclusive) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export const defaultShippingCalculator = new ShippingCalculator({
args: {
rate: {
type: 'int',
defaultValue: 0,
ui: { component: 'currency-form-input' },
label: [{ languageCode: LanguageCode.en, value: 'Shipping price' }],
},
includesTax: {
type: 'string',
defaultValue: TaxSetting.auto,
ui: {
component: 'select-form-input',
options: [
Expand All @@ -42,6 +44,7 @@ export const defaultShippingCalculator = new ShippingCalculator({
},
taxRate: {
type: 'int',
defaultValue: 0,
ui: { component: 'number-form-input', suffix: '%' },
label: [{ languageCode: LanguageCode.en, value: 'Tax rate' }],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const defaultShippingEligibilityChecker = new ShippingEligibilityChecker(
args: {
orderMinimum: {
type: 'int',
defaultValue: 0,
ui: { component: 'currency-form-input' },
label: [{ languageCode: LanguageCode.en, value: 'Minimum order value' }],
description: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2467,6 +2467,7 @@ export type ConfigArgDefinition = {
type: Scalars['String'];
list: Scalars['Boolean'];
required: Scalars['Boolean'];
defaultValue?: Maybe<Scalars['String']>;
label?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
ui?: Maybe<Scalars['JSON']>;
Expand All @@ -2490,6 +2491,7 @@ export type DeletionResponse = {

export type ConfigArgInput = {
name: Scalars['String'];
/** A JSON stringified representation of the actual value */
value: Scalars['String'];
};

Expand Down
2 changes: 1 addition & 1 deletion schema-admin.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion schema-shop.json

Large diffs are not rendered by default.

0 comments on commit 92ae819

Please sign in to comment.