Browse Source

feat(core): Support "defaultValue" field in ConfigArgs

Relates to #643
Michael Bromley 5 years ago
parent
commit
92ae819c58

+ 2 - 0
packages/asset-server-plugin/e2e/graphql/generated-e2e-asset-server-plugin-types.ts

@@ -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']>;
@@ -2490,6 +2491,7 @@ export type DeletionResponse = {
 
 export type ConfigArgInput = {
     name: Scalars['String'];
+    /** A JSON stringified representation of the actual value */
     value: Scalars['String'];
 };
 

+ 2 - 0
packages/common/src/generated-shop-types.ts

@@ -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']>;
@@ -674,6 +675,7 @@ export type DeletionResponse = {
 
 export type ConfigArgInput = {
     name: Scalars['String'];
+    /** A JSON stringified representation of the actual value */
     value: Scalars['String'];
 };
 

+ 2 - 0
packages/common/src/generated-types.ts

@@ -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']>;
@@ -2678,6 +2679,7 @@ export type DeletionResponse = {
 
 export type ConfigArgInput = {
   name: Scalars['String'];
+  /** A JSON stringified representation of the actual value */
   value: Scalars['String'];
 };
 

+ 38 - 2
packages/core/e2e/configurable-operation.e2e-spec.ts

@@ -1,12 +1,14 @@
+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({
@@ -14,12 +16,21 @@ const testShippingEligibilityChecker = new ShippingEligibilityChecker({
     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,
@@ -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 - 0
packages/core/e2e/graphql/generated-e2e-admin-types.ts

@@ -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']>;
@@ -2490,6 +2491,7 @@ export type DeletionResponse = {
 
 export type ConfigArgInput = {
     name: Scalars['String'];
+    /** A JSON stringified representation of the actual value */
     value: Scalars['String'];
 };
 
@@ -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'];
 }>;
@@ -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;

+ 2 - 0
packages/core/e2e/graphql/generated-e2e-shop-types.ts

@@ -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']>;
@@ -646,6 +647,7 @@ export type DeletionResponse = {
 
 export type ConfigArgInput = {
     name: Scalars['String'];
+    /** A JSON stringified representation of the actual value */
     value: Scalars['String'];
 };
 

+ 2 - 0
packages/core/src/api/schema/common/common-types.graphql

@@ -39,6 +39,7 @@ type ConfigArgDefinition {
     type: String!
     list: Boolean!
     required: Boolean!
+    defaultValue: String
     label: String
     description: String
     ui: JSON
@@ -63,6 +64,7 @@ type DeletionResponse {
 
 input ConfigArgInput {
     name: String!
+    "A JSON stringified representation of the actual value"
     value: String!
 }
 

+ 19 - 0
packages/core/src/common/configurable-operation.ts

@@ -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;
@@ -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:
  *
@@ -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),

+ 2 - 2
packages/core/src/config/payment-method/example-payment-method-handler.ts

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

+ 4 - 1
packages/core/src/config/promotion/conditions/contains-products-condition.ts

@@ -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,

+ 1 - 1
packages/core/src/config/promotion/conditions/has-facet-values-condition.ts

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

+ 2 - 1
packages/core/src/config/promotion/conditions/min-order-amount-condition.ts

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

+ 3 - 0
packages/core/src/config/shipping-method/default-shipping-calculator.ts

@@ -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: [
@@ -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' }],
         },

+ 1 - 0
packages/core/src/config/shipping-method/default-shipping-eligibility-checker.ts

@@ -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: [

+ 2 - 0
packages/elasticsearch-plugin/e2e/graphql/generated-e2e-elasticsearch-plugin-types.ts

@@ -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']>;
@@ -2490,6 +2491,7 @@ export type DeletionResponse = {
 
 export type ConfigArgInput = {
     name: Scalars['String'];
+    /** A JSON stringified representation of the actual value */
     value: Scalars['String'];
 };
 

File diff suppressed because it is too large
+ 0 - 0
schema-admin.json


File diff suppressed because it is too large
+ 0 - 0
schema-shop.json


Some files were not shown because too many files changed in this diff