private generateFieldSchema()

in packages/appsync-modelgen-plugin/src/visitors/appsync-swift-visitor.ts [346:405]


  private generateFieldSchema(field: CodeGenField, modelKeysName: string): string {
    if (field.type === 'ID' && field.name === 'id') {
      return `.id()`;
    }
    let ofType;
    let isReadOnly: string = '';
    const isEnumType = this.isEnumType(field);
    const isModelType = this.isModelType(field);
    const isNonModelType = this.isNonModelType(field);
    const name = `${modelKeysName}.${this.getFieldName(field)}`;
    const typeName = this.getSwiftModelTypeName(field);
    const { connectionInfo } = field;
    const isRequiredField =
      !this.isHasManyConnectionField(field) && this.config.handleListNullabilityTransparently
        ? this.isRequiredField(field)
        : this.isFieldRequired(field);
    const isRequired = isRequiredField ? '.required' : '.optional';
    // connected field
    if (connectionInfo) {
      if (connectionInfo.kind === CodeGenConnectionType.HAS_MANY) {
        return `.hasMany(${name}, is: ${isRequired}, ofType: ${typeName}, associatedWith: ${this.getModelName(
          connectionInfo.connectedModel,
          )}.keys.${this.getFieldName(connectionInfo.associatedWith)})`;
        }
      if (connectionInfo.kind === CodeGenConnectionType.HAS_ONE) {
        return `.hasOne(${name}, is: ${isRequired}, ofType: ${typeName}, associatedWith: ${this.getModelName(
          connectionInfo.connectedModel,
        )}.keys.${this.getFieldName(connectionInfo.associatedWith)}, targetName: "${connectionInfo.targetName}")`;
      }
      if (connectionInfo.kind === CodeGenConnectionType.BELONGS_TO) {
        return `.belongsTo(${name}, is: ${isRequired}, ofType: ${typeName}, targetName: "${connectionInfo.targetName}")`;
      }
    }

    if (field.isList) {
      if (isModelType) {
        ofType = `.collection(of: ${this.getSwiftModelTypeName(field)})`;
      } else {
        ofType = `.embeddedCollection(of: ${this.getSwiftModelTypeName(field)})`;
      }
    } else {
      if (isEnumType) {
        ofType = `.enum(type: ${typeName})`;
      } else if (isModelType) {
        ofType = `.model(${typeName})`;
      } else if (isNonModelType) {
        ofType = `.embedded(type: ${typeName})`;
      } else {
        ofType = typeName;
      }
    }

    //read-only fields
    if (field.isReadOnly) {
      isReadOnly = 'isReadOnly: true';
    }

    const args = [`${name}`, `is: ${isRequired}`, isReadOnly, `ofType: ${ofType}`].filter(arg => arg).join(', ');
    return `.field(${args})`;
  }