Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/appsync-modelgen-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"extract-api": "ts-node ../../scripts/extract-api.ts"
},
"dependencies": {
"@graphql-codegen/plugin-helpers": "^3.1.1",
"@graphql-codegen/visitor-plugin-common": "^1.22.0",
"@graphql-tools/utils": "^6.0.18",
"@graphql-codegen/plugin-helpers": "^6.1.1",
"@graphql-codegen/visitor-plugin-common": "^6.2.4",
"@graphql-tools/utils": "^11.0.0",
"chalk": "^3.0.0",
"change-case": "^4.1.1",
"graphql-transformer-common": "^4.25.1",
Expand Down
31 changes: 21 additions & 10 deletions packages/appsync-modelgen-plugin/src/scalars/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import { NormalizedScalarsMap } from '@graphql-codegen/visitor-plugin-common';
import { AMPLIFY_CORE_PREFIX } from '../configs/dart-config';

export const JAVA_SCALAR_MAP: NormalizedScalarsMap = {
const createScalarMap = (scalarTypes: Record<string, string>): NormalizedScalarsMap => {
const normalizedScalars: NormalizedScalarsMap = {};
Object.entries(scalarTypes).forEach(([scalarName, scalarType]) => {
normalizedScalars[scalarName] = {
input: scalarType,
output: scalarType,
};
});
return normalizedScalars;
};

export const JAVA_SCALAR_MAP: NormalizedScalarsMap = createScalarMap({
ID: 'String',
String: 'String',
Int: 'Integer',
Expand All @@ -16,7 +27,7 @@ export const JAVA_SCALAR_MAP: NormalizedScalarsMap = {
AWSURL: 'String',
AWSPhone: 'String',
AWSIPAddress: 'String',
};
});

// Package that needs to be imported when using the types
export const JAVA_TYPE_IMPORT_MAP: Record<string, string> = {
Expand All @@ -26,7 +37,7 @@ export const JAVA_TYPE_IMPORT_MAP: Record<string, string> = {
'Temporal.Timestamp': 'com.amplifyframework.core.model.temporal.Temporal',
};

export const SWIFT_SCALAR_MAP: NormalizedScalarsMap = {
export const SWIFT_SCALAR_MAP: NormalizedScalarsMap = createScalarMap({
ID: 'String',
String: 'String',
Int: 'Int',
Expand All @@ -41,9 +52,9 @@ export const SWIFT_SCALAR_MAP: NormalizedScalarsMap = {
AWSURL: 'String',
AWSPhone: 'String',
AWSIPAddress: 'String',
};
});

export const TYPESCRIPT_SCALAR_MAP: NormalizedScalarsMap = {
export const TYPESCRIPT_SCALAR_MAP: NormalizedScalarsMap = createScalarMap({
ID: 'string',
String: 'string',
Int: 'number',
Expand All @@ -58,9 +69,9 @@ export const TYPESCRIPT_SCALAR_MAP: NormalizedScalarsMap = {
AWSURL: 'string',
AWSPhone: 'string',
AWSIPAddress: 'string',
};
});

export const METADATA_SCALAR_MAP: NormalizedScalarsMap = {
export const METADATA_SCALAR_MAP: NormalizedScalarsMap = createScalarMap({
ID: 'ID',
Boolean: 'Boolean',
String: 'String',
Expand All @@ -75,9 +86,9 @@ export const METADATA_SCALAR_MAP: NormalizedScalarsMap = {
Int: 'Int',
Float: 'Float',
AWSTimestamp: 'AWSTimestamp',
};
});

export const DART_SCALAR_MAP: NormalizedScalarsMap = {
export const DART_SCALAR_MAP: NormalizedScalarsMap = createScalarMap({
ID: 'String',
String: 'String',
Int: 'int',
Expand All @@ -92,4 +103,4 @@ export const DART_SCALAR_MAP: NormalizedScalarsMap = {
AWSURL: 'String',
AWSPhone: 'String',
AWSIPAddress: 'String',
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// TypeScript 4.7 does not ship the standard Disposable globals that newer
// graphql-tools type declarations reference. This package does not use those
// contracts directly, so lightweight ambient declarations keep the package
// buildable until the repo's TypeScript floor moves forward.
interface Disposable {}

interface AsyncDisposable {}
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ export class AppSyncModelDartVisitor<
case 'String':
toStringVal = `"$${fieldName}"`;
break;
case this.scalars['AWSDate']:
case this.scalars['AWSTime']:
case this.scalars['AWSDateTime']:
case this.getScalarType('AWSDate'):
case this.getScalarType('AWSTime'):
case this.getScalarType('AWSDateTime'):
toStringVal = `(${fieldName} != null ? ${fieldName}!.format() : "null")`;
break;
default:
Expand Down Expand Up @@ -768,25 +768,25 @@ export class AppSyncModelDartVisitor<
//regular type
const fieldNativeType = this.getNativeType({ ...field, isList: false });
switch (fieldNativeType) {
case this.scalars['AWSDate']:
case this.scalars['AWSTime']:
case this.scalars['AWSDateTime']:
case this.getScalarType('AWSDate'):
case this.getScalarType('AWSTime'):
case this.getScalarType('AWSDateTime'):
return field.isList
? `${fieldName} = (json['${varName}'] as ${this.getNullableTypeStr(
'List',
)})?.map((e) => ${fieldNativeType}.fromString(e)).toList()`
: `${fieldName} = json['${varName}'] != null ? ${fieldNativeType}.fromString(json['${varName}']) : null`;
case this.scalars['AWSTimestamp']:
case this.getScalarType('AWSTimestamp'):
return field.isList
? `${fieldName} = (json['${varName}'] as ${this.getNullableTypeStr(
'List',
)})?.map((e) => ${fieldNativeType}.fromSeconds(e)).toList()`
: `${fieldName} = json['${varName}'] != null ? ${fieldNativeType}.fromSeconds(json['${varName}']) : null`;
case this.scalars['Int']:
case this.getScalarType('Int'):
return field.isList
? `${fieldName} = (json['${varName}'] as ${this.getNullableTypeStr('List')})?.map((e) => (e as num).toInt()).toList()`
: `${fieldName} = (json['${varName}'] as ${this.getNullableTypeStr('num')})?.toInt()`;
case this.scalars['Float']:
case this.getScalarType('Float'):
return field.isList
? `${fieldName} = (json['${varName}'] as ${this.getNullableTypeStr('List')})?.map((e) => (e as num).toDouble()).toList()`
: `${fieldName} = (json['${varName}'] as ${this.getNullableTypeStr('num')})?.toDouble()`;
Expand Down Expand Up @@ -825,11 +825,11 @@ export class AppSyncModelDartVisitor<
}
const fieldNativeType = this.getNativeType({ ...field, isList: false });
switch (fieldNativeType) {
case this.scalars['AWSDate']:
case this.scalars['AWSTime']:
case this.scalars['AWSDateTime']:
case this.getScalarType('AWSDate'):
case this.getScalarType('AWSTime'):
case this.getScalarType('AWSDateTime'):
return field.isList ? `'${varName}': ${fieldName}?.map((e) => e.format()).toList()` : `'${varName}': ${fieldName}?.format()`;
case this.scalars['AWSTimestamp']:
case this.getScalarType('AWSTimestamp'):
return field.isList
? `'${varName}': ${fieldName}?.map((e) => e.toSeconds()).toList()`
: `'${varName}': ${fieldName}?.toSeconds()`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export class AppSyncJSONVisitor<
private getType(gqlType: string): JSONModelFieldType {
// Todo: Handle unlisted scalars
if (gqlType in METADATA_SCALAR_MAP) {
return METADATA_SCALAR_MAP[gqlType as keyof typeof METADATA_SCALAR_MAP];
return METADATA_SCALAR_MAP[gqlType as keyof typeof METADATA_SCALAR_MAP].output as JSONModelFieldType;
}
if (gqlType in this.enumMap) {
return { enum: this.enumMap[gqlType].name };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export class AppSyncModelIntrospectionVisitor<
protected getType(gqlType: string): FieldType | InputFieldType | UnionFieldType | InterfaceFieldType {
// Todo: Handle unlisted scalars
if (gqlType in METADATA_SCALAR_MAP) {
return METADATA_SCALAR_MAP[gqlType] as FieldType;
return METADATA_SCALAR_MAP[gqlType].output as FieldType;
}
if (gqlType in this.enumMap) {
return { enum: this.enumMap[gqlType].name };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export interface RawAppSyncModelConfig extends RawConfig {
* @type string
* @description semantic version of amplify-codegen package
*/
codegenVersion: string;
codegenVersion?: string;
}

// Todo: need to figure out how to share config
Expand Down Expand Up @@ -584,7 +584,7 @@ export class AppSyncModelVisitor<
const typeName = field.type;
let typeNameStr: string = '';
if (typeName in this.scalars) {
typeNameStr = this.scalars[typeName];
typeNameStr = this.getScalarType(typeName);
} else if (this.isModelType(field)) {
typeNameStr = this.getModelName(this.modelMap[typeName]);
} else if (this.isEnumType(field)) {
Expand All @@ -598,6 +598,14 @@ export class AppSyncModelVisitor<
return field.isList ? this.getListType(typeNameStr, field) : typeNameStr;
}

protected getScalarType(typeName: string, kind: 'input' | 'output' = 'output'): string {
const scalar = this.scalars[typeName];
if (!scalar) {
throw new Error(`Unknown scalar ${typeName}`);
}
return scalar[kind];
}

protected getListType(typeStr: string, field: CodeGenField): string {
return `List<${typeStr}>`;
}
Expand Down
Loading