Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow CustomType and RefType in arguments #387

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
5 changes: 5 additions & 0 deletions .changeset/polite-crabs-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aws-amplify/data-schema": minor
---

Allow CustomType and RefType in arguments for custom operations
22 changes: 22 additions & 0 deletions packages/data-schema/__tests__/CustomOperations.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import type {
import { configure } from '../src/ModelSchema';
import { Nullable } from '../src/ModelField';
import { defineFunctionStub } from './utils';
import type {
CustomOperation,
} from '../src/CustomOperation';

describe('custom operations return types', () => {
describe('when .ref() a basic custom type', () => {
Expand Down Expand Up @@ -858,3 +861,22 @@ describe('.for() modifier', () => {
a.subscription().for(a.ref('Model'));
});
});

describe('.arguments() modifier', () => {
// Test to verify that CustomType can be used as an argument in custom operations
it('accepts CustomType in arguments', () => {
const operation: CustomOperation<any, "arguments" | "for", "queryCustomOperation"> = a.query().arguments({
customArg: a.customType({
field1: a.string(),
field2: a.integer()
})
});
});

// Test to verify that RefType can be used as an argument in custom operations
it('accepts RefType in arguments', () => {
const operation:CustomOperation<any, "arguments" | "for", "queryCustomOperation"> = a.query().arguments({
refArg: a.ref('SomeType')
});
});
});
2 changes: 1 addition & 1 deletion packages/data-schema/src/CustomOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type CustomOperationBrand =
| typeof subscriptionBrand
| typeof generationBrand;

type CustomArguments = Record<string, BaseModelField | EnumType>;
type CustomArguments = Record<string, BaseModelField | EnumType | CustomType<any> | RefType<any, any>>;
type SubscriptionSource = RefType<any, any>;
type InternalSubscriptionSource = InternalRef;
type CustomReturnType = RefType<any> | CustomType<any>;
Expand Down
6 changes: 4 additions & 2 deletions packages/data-schema/src/runtime/bridge-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { Observable } from 'rxjs';
import { CustomHeaders, ModelSortDirection } from './client';

import { AiAction, AiCategory } from './internals/ai/getCustomUserAgentDetails';
import { CustomType } from '../CustomType';
import { RefType } from '../RefType';

export declare namespace AmplifyServer {
export interface ContextToken {
Expand Down Expand Up @@ -165,7 +167,7 @@ export type CustomOperationArguments = Record<string, CustomOperationArgument>;

export interface CustomOperationArgument {
name: string;
type: InputFieldType;
type: InputFieldType | CustomType<any> | RefType<any, any>;
isArray: boolean;
isRequired: boolean;
isArrayNullable?: boolean;
Expand Down Expand Up @@ -240,7 +242,7 @@ export type FieldType =
| ModelFieldType
| NonModelFieldType;

export type InputFieldType = ScalarType | EnumType | InputType;
export type InputFieldType = ScalarType | EnumType | InputType | CustomType<any> | RefType<any, any>;

export type FieldAttribute = ModelAttribute;

Expand Down
Loading