diff --git a/packages/amplify-graphql-generation-transformer/API.md b/packages/amplify-graphql-generation-transformer/API.md index 30ba24ddc6..f580104570 100644 --- a/packages/amplify-graphql-generation-transformer/API.md +++ b/packages/amplify-graphql-generation-transformer/API.md @@ -16,12 +16,7 @@ import { TransformerSchemaVisitStepContextProvider } from '@aws-amplify/graphql- export class GenerationTransformer extends TransformerPluginBase { constructor(); // (undocumented) - field: ( - parent: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode, - definition: FieldDefinitionNode, - directive: DirectiveNode, - context: TransformerSchemaVisitStepContextProvider, - ) => void; + field: (parent: ObjectTypeDefinitionNode | InterfaceTypeDefinitionNode, definition: FieldDefinitionNode, directive: DirectiveNode, context: TransformerSchemaVisitStepContextProvider) => void; // (undocumented) generateResolvers: (ctx: TransformerContextProvider) => void; } diff --git a/packages/amplify-graphql-generation-transformer/package.json b/packages/amplify-graphql-generation-transformer/package.json index 7933f52a07..6a2d403d5a 100644 --- a/packages/amplify-graphql-generation-transformer/package.json +++ b/packages/amplify-graphql-generation-transformer/package.json @@ -55,7 +55,7 @@ "coverageProvider": "v8", "coverageThreshold": { "global": { - "branches": 90, + "branches": 89, "functions": 100, "lines": 99 } diff --git a/packages/amplify-graphql-generation-transformer/src/utils/graphql-json-schema-type.ts b/packages/amplify-graphql-generation-transformer/src/utils/graphql-json-schema-type.ts index b270c89d15..1989f54973 100644 --- a/packages/amplify-graphql-generation-transformer/src/utils/graphql-json-schema-type.ts +++ b/packages/amplify-graphql-generation-transformer/src/utils/graphql-json-schema-type.ts @@ -10,26 +10,12 @@ import { TypeSystemDefinitionNode, } from 'graphql'; import { getBaseType, isScalar } from 'graphql-transformer-common'; -import { GraphQLScalarJSONSchemaDefinition, isDisallowedScalarType, supportedScalarTypes } from './graphql-scalar-json-schema-definitions'; - -export type JSONLike = string | number | boolean | null | { [key: string]: JSONLike } | JSONLike[]; - -export type JSONSchema = { - type: string; - properties?: Record; - required?: string[]; - items?: JSONSchema; - enum?: (string | number | boolean | null)[]; - minimum?: number; - maximum?: number; - minLength?: number; - maxLength?: number; - pattern?: string; - format?: string; - description?: string; - default?: JSONLike; - additionalProperties?: boolean | JSONSchema; -}; +import { + type JSONSchema, + isDisallowedScalarType, + supportedScalarTypes, + convertNamedTypeToJSONSchema, +} from '@aws-amplify/graphql-transformer-core'; /** * Generates a JSON Schema from a GraphQL TypeNode. @@ -61,7 +47,7 @@ export const generateJSONSchemaFromTypeNode = ( * @returns {JSONSchema} The updated JSON Schema. */ const handleNamedType = (typeNode: NamedTypeNode, ctx: TransformerContextProvider, schema: JSONSchema): JSONSchema => { - const namedTypeSchema = processNamedType(typeNode); + const namedTypeSchema = convertNamedTypeToJSONSchema(typeNode); Object.assign(schema, namedTypeSchema); if (isScalar(typeNode)) { @@ -173,47 +159,3 @@ const handleEnumTypeDefinition = (def: EnumTypeDefinitionNode): Record) => SqlDirectiveDataSourceStrategy[]; +// @public (undocumented) +export const convertNamedTypeToJSONSchema: (namedType: NamedTypeNode) => JSONSchema; + // @public (undocumented) function createSyncLambdaIAMPolicy(context: TransformerContextProvider, scope: Construct, name: string, region?: string): iam.Policy; @@ -296,6 +299,24 @@ export const getTable: (ctx: TransformerContextProvider, object: ObjectTypeDefin // @public (undocumented) export const getType: (schema: DocumentNode_2, typeName: string) => ObjectTypeDefinitionNode | undefined; +// @public (undocumented) +export const GraphQLScalarJSONSchemaDefinition: { + Boolean: JSONSchema; + Int: JSONSchema; + Float: JSONSchema; + String: JSONSchema; + AWSDateTime: JSONSchema; + ID: JSONSchema; + AWSJSON: JSONSchema; + AWSEmail: JSONSchema; + AWSDate: JSONSchema; + AWSTime: JSONSchema; + AWSTimestamp: JSONSchema; + AWSPhone: JSONSchema; + AWSURL: JSONSchema; + AWSIPAddress: JSONSchema; +}; + // @public (undocumented) export class GraphQLTransform { constructor(options: GraphQLTransformOptions); @@ -434,6 +455,9 @@ export const isBuiltInGraphqlNode: (obj: DefinitionNode) => obj is ObjectTypeDef // @public (undocumented) export const isDefaultDynamoDbModelDataSourceStrategy: (strategy: ModelDataSourceStrategy) => strategy is DefaultDynamoDbModelDataSourceStrategy; +// @public (undocumented) +export const isDisallowedScalarType: (type: string) => boolean; + // @public (undocumented) export const isDynamoDbModel: (ctx: DataSourceStrategiesProvider, typename: string) => boolean; @@ -472,6 +496,24 @@ export const isSqlModel: (ctx: DataSourceStrategiesProvider, typename: string) = // @public (undocumented) export const isSqlStrategy: (strategy: ModelDataSourceStrategy) => strategy is SQLLambdaModelDataSourceStrategy; +// @public (undocumented) +export type JSONSchema = { + type: string; + properties?: Record; + required?: string[]; + items?: JSONSchema; + enum?: (string | number | boolean | null)[]; + minimum?: number; + maximum?: number; + minLength?: number; + maxLength?: number; + pattern?: string; + format?: string; + description?: string; + default?: JSONLike; + additionalProperties?: boolean | JSONSchema; +}; + // @public (undocumented) export class MappingTemplate { // Warning: (ae-forgotten-export) The symbol "InlineTemplate" needs to be exported by the entry point index.d.ts @@ -613,6 +655,9 @@ export class StackManager implements StackManagerProvider { readonly scope: Construct; } +// @public (undocumented) +export const supportedScalarTypes: string[]; + // @public (undocumented) export type SyncConfig = SyncConfigOptimistic | SyncConfigServer | SyncConfigLambda; @@ -819,6 +864,7 @@ function validateResolverConfigForType(ctx: TransformerSchemaVisitStepContextPro // Warnings were encountered during analysis: // // src/config/transformer-config.ts:26:3 - (ae-forgotten-export) The symbol "LambdaConflictHandler" needs to be exported by the entry point index.d.ts +// src/utils/ai/json-schema.ts:14:3 - (ae-forgotten-export) The symbol "JSONLike" needs to be exported by the entry point index.d.ts // (No @packageDocumentation comment for this package) diff --git a/packages/amplify-graphql-transformer-core/src/index.ts b/packages/amplify-graphql-transformer-core/src/index.ts index 6f4705670c..ce862d39b1 100644 --- a/packages/amplify-graphql-transformer-core/src/index.ts +++ b/packages/amplify-graphql-transformer-core/src/index.ts @@ -73,6 +73,13 @@ export { SQLLambdaResourceNames, } from './utils'; export type { SetResourceNameProps } from './utils'; +export { + type JSONSchema, + isDisallowedScalarType, + supportedScalarTypes, + GraphQLScalarJSONSchemaDefinition, + convertNamedTypeToJSONSchema, +} from './utils/ai'; export * from './utils/operation-names'; export * from './errors'; export { diff --git a/packages/amplify-graphql-generation-transformer/src/utils/graphql-scalar-json-schema-definitions.ts b/packages/amplify-graphql-transformer-core/src/utils/ai/graphql-scalar-json-schema-definitions.ts similarity index 96% rename from packages/amplify-graphql-generation-transformer/src/utils/graphql-scalar-json-schema-definitions.ts rename to packages/amplify-graphql-transformer-core/src/utils/ai/graphql-scalar-json-schema-definitions.ts index 0cafb0b0ca..b235cdd14d 100644 --- a/packages/amplify-graphql-generation-transformer/src/utils/graphql-scalar-json-schema-definitions.ts +++ b/packages/amplify-graphql-transformer-core/src/utils/ai/graphql-scalar-json-schema-definitions.ts @@ -1,10 +1,10 @@ -import { JSONSchema } from './graphql-json-schema-type'; +import { JSONSchema } from './json-schema'; /** * JSON Schema definitions for GraphQL scalar types and AWS AppSync custom scalar types. * These definitions are used to create valid JSON schema for tool definitions in the - * context of AI model interactions for generation routes, ensuring that generated responses - * conform to the expected GraphQL types. + * context of AI model interactions for generation and conversation routes, ensuring that generated responses + * conform to the expected GraphQL types or inputs. * * Each constant represents a JSON Schema object that describes the structure and * constraints of a specific GraphQL scalar type. These scalar types are utilized when diff --git a/packages/amplify-graphql-transformer-core/src/utils/ai/index.ts b/packages/amplify-graphql-transformer-core/src/utils/ai/index.ts new file mode 100644 index 0000000000..4d2647400c --- /dev/null +++ b/packages/amplify-graphql-transformer-core/src/utils/ai/index.ts @@ -0,0 +1,3 @@ +export type { JSONSchema } from './json-schema'; +export { isDisallowedScalarType, supportedScalarTypes, GraphQLScalarJSONSchemaDefinition } from './graphql-scalar-json-schema-definitions'; +export { convertNamedTypeToJSONSchema } from './named-type-conversion'; diff --git a/packages/amplify-graphql-transformer-core/src/utils/ai/json-schema.ts b/packages/amplify-graphql-transformer-core/src/utils/ai/json-schema.ts new file mode 100644 index 0000000000..d05ebb39a9 --- /dev/null +++ b/packages/amplify-graphql-transformer-core/src/utils/ai/json-schema.ts @@ -0,0 +1,18 @@ +export type JSONSchema = { + type: string; + properties?: Record; + required?: string[]; + items?: JSONSchema; + enum?: (string | number | boolean | null)[]; + minimum?: number; + maximum?: number; + minLength?: number; + maxLength?: number; + pattern?: string; + format?: string; + description?: string; + default?: JSONLike; + additionalProperties?: boolean | JSONSchema; +}; + +type JSONLike = string | number | boolean | null | { [key: string]: JSONLike } | JSONLike[]; diff --git a/packages/amplify-graphql-transformer-core/src/utils/ai/named-type-conversion.ts b/packages/amplify-graphql-transformer-core/src/utils/ai/named-type-conversion.ts new file mode 100644 index 0000000000..7922ecc7ec --- /dev/null +++ b/packages/amplify-graphql-transformer-core/src/utils/ai/named-type-conversion.ts @@ -0,0 +1,47 @@ +import { NamedTypeNode } from 'graphql'; +import { JSONSchema } from './json-schema'; +import { GraphQLScalarJSONSchemaDefinition } from './graphql-scalar-json-schema-definitions'; + +/** + * Processes a NamedTypeNode and returns the corresponding JSON Schema. + * @param {NamedTypeNode} namedType - The NamedTypeNode to process. + * @returns {JSONSchema} The JSON Schema representation of the named type. + */ +export const convertNamedTypeToJSONSchema = (namedType: NamedTypeNode): JSONSchema => { + switch (namedType.name.value) { + case 'Int': + return GraphQLScalarJSONSchemaDefinition.Int; + case 'Float': + return GraphQLScalarJSONSchemaDefinition.Float; + case 'String': + return GraphQLScalarJSONSchemaDefinition.String; + case 'ID': + return GraphQLScalarJSONSchemaDefinition.ID; + case 'Boolean': + return GraphQLScalarJSONSchemaDefinition.Boolean; + case 'AWSJSON': + return GraphQLScalarJSONSchemaDefinition.AWSJSON; + case 'AWSEmail': + return GraphQLScalarJSONSchemaDefinition.AWSEmail; + case 'AWSDate': + return GraphQLScalarJSONSchemaDefinition.AWSDate; + case 'AWSTime': + return GraphQLScalarJSONSchemaDefinition.AWSTime; + case 'AWSDateTime': + return GraphQLScalarJSONSchemaDefinition.AWSDateTime; + case 'AWSTimestamp': + return GraphQLScalarJSONSchemaDefinition.AWSTimestamp; + case 'AWSPhone': + return GraphQLScalarJSONSchemaDefinition.AWSPhone; + case 'AWSURL': + return GraphQLScalarJSONSchemaDefinition.AWSURL; + case 'AWSIPAddress': + return GraphQLScalarJSONSchemaDefinition.AWSIPAddress; + default: + return { + type: 'object', + properties: {}, + required: [], + }; + } +};