{"version":3,"file":"populateTypeDefs.cjs","names":[],"sources":["../src/populateTypeDefs.ts"],"sourcesContent":["import { isScalarType, type GraphQLSchema } from 'graphql'\nimport {\n  getDefaultTypeDefLinesObject,\n  getDefaultFieldLinesObject,\n  generateDefaultQueryFilterTypeDefs,\n  populateArgsDefForDefaultResolver,\n  type GenePlugin,\n  type TypeDefLines,\n} from 'graphql-gene'\nimport { DataTypes } from 'sequelize'\nimport {\n  DATE_SCALAR,\n  DATE_TIME_SCALAR,\n  GeneModel,\n  JSON_SCALAR,\n  SEQUELIZE_TYPE_TO_GRAPHQL,\n} from './constants'\nimport { markFieldAsAssociation } from './utils/associationMap'\n\nconst BELONGS_TO_MANY = 'BelongsToMany'\n\ntype PopulateTypeDefs = GenePlugin<typeof GeneModel>['populateTypeDefs']\ntype PopulateTypeDefsOptions = Parameters<PopulateTypeDefs>[0]\n\nfunction hasScalarInSchema(schema: GraphQLSchema | undefined, scalar: string) {\n  return schema ? isScalarType(schema.getType(scalar)) : false\n}\n\nexport const populateTypeDefs: PopulateTypeDefs = options => {\n  options.typeDefLines[options.typeName] = {\n    ...getDefaultTypeDefLinesObject(),\n    ...options.typeDefLines[options.typeName],\n  }\n\n  const mainTypeDef = options.typeDefLines[options.typeName]\n  const attributes = options.model.getAttributes()\n\n  Object.entries(attributes).forEach(([attributeKey, attributeValue]) => {\n    const isIncluded = options.isFieldIncluded(attributeKey)\n    if (!isIncluded) return\n\n    const dataType = attributeValue.type.constructor.name\n    let realDataType = dataType\n\n    // Skip id field of associations\n    if (attributeValue.type instanceof DataTypes.INTEGER && 'references' in attributeValue) return\n\n    // Get return type of virtual attribute\n    if (attributeValue.type instanceof DataTypes.VIRTUAL) {\n      if (!('returnType' in attributeValue.type)) {\n        throw new Error(\n          `Virtual attribute \"${attributeKey}\" of \"${options.typeName}\" must have a \"returnType\" defined. Example using decorators: @Column(DataType.VIRTUAL(DataType.BOOLEAN))`\n        )\n      }\n      realDataType = attributeValue.type.returnType.constructor.name\n    }\n\n    const { schemaOptions } = options\n    const typeMap = { ...SEQUELIZE_TYPE_TO_GRAPHQL, ...schemaOptions.dataTypeMap }\n\n    if (realDataType in typeMap && typeMap[realDataType as keyof typeof typeMap]) {\n      let graphqlType = typeMap[realDataType as keyof typeof typeMap]\n\n      if (graphqlType === 'String' && attributeValue.primaryKey) graphqlType = 'ID'\n\n      if (\n        graphqlType === DATE_SCALAR ||\n        graphqlType === DATE_TIME_SCALAR ||\n        graphqlType === JSON_SCALAR\n      ) {\n        if (!hasScalarInSchema(schemaOptions.schema, graphqlType)) graphqlType = 'String'\n      }\n\n      if (attributeValue.allowNull === false) graphqlType += '!'\n\n      mainTypeDef.lines[attributeKey] = {\n        ...getDefaultFieldLinesObject(),\n        ...mainTypeDef.lines[attributeKey],\n      }\n      mainTypeDef.lines[attributeKey].typeDef = graphqlType\n    }\n  })\n\n  const { afterTypeDefHooks } = generateAssociationFields({\n    typeDefLines: options.typeDefLines,\n    model: options.model,\n    isFieldIncluded: options.isFieldIncluded,\n    typeName: options.typeName,\n  })\n\n  return { afterTypeDefHooks }\n}\n\nfunction generateAssociationFields(\n  options: Pick<PopulateTypeDefsOptions, 'model' | 'isFieldIncluded' | 'typeName'> & {\n    typeDefLines: TypeDefLines\n  }\n) {\n  const lines = options.typeDefLines[options.typeName].lines\n  const afterTypeDefHooks: (() => void)[] = []\n\n  Object.entries(options.model.associations).forEach(([attributeKey, association]) => {\n    if (\n      !options.isFieldIncluded(attributeKey) ||\n      // Eager loading doesn't support BelongsToMany associations\n      association.associationType === BELONGS_TO_MANY\n    ) {\n      return\n    }\n\n    const associationModelName = association.target.name\n    let returnType = associationModelName\n    let isList = false\n\n    if (association.isMultiAssociation) {\n      returnType = `[${returnType}!]`\n      isList = true\n    }\n\n    lines[attributeKey] = { ...getDefaultFieldLinesObject(), ...lines[attributeKey] }\n    lines[attributeKey].typeDef = returnType\n\n    markFieldAsAssociation(options.typeName, attributeKey)\n\n    if (isList) {\n      populateArgsDefForDefaultResolver({\n        fieldLineConfig: lines[attributeKey],\n        graphqlType: options.typeName,\n        fieldKey: attributeKey,\n        isList,\n      })\n\n      // Each association field is filterable (default resolver arguments)\n      afterTypeDefHooks.push(() => {\n        generateDefaultQueryFilterTypeDefs({\n          typeDefLines: options.typeDefLines,\n          graphqlType: options.typeName,\n          fieldKey: attributeKey,\n          fieldType: associationModelName,\n        })\n      })\n    }\n  })\n  return { afterTypeDefHooks }\n}\n"],"mappings":"wNAmBA,IAAM,EAAkB,gBAKxB,SAAS,EAAkB,EAAmC,EAAgB,CAC5E,OAAO,GAAA,EAAA,EAAA,cAAsB,EAAO,QAAQ,EAAO,CAAC,CAAG,GAGzD,IAAa,EAAqC,GAAW,CAC3D,EAAQ,aAAa,EAAQ,UAAY,CACvC,IAAA,EAAA,EAAA,+BAAiC,CACjC,GAAG,EAAQ,aAAa,EAAQ,UACjC,CAED,IAAM,EAAc,EAAQ,aAAa,EAAQ,UAC3C,EAAa,EAAQ,MAAM,eAAe,CAEhD,OAAO,QAAQ,EAAW,CAAC,SAAS,CAAC,EAAc,KAAoB,CAErE,GAAI,CADe,EAAQ,gBAAgB,EACtC,CAAY,OAGjB,IAAI,EADa,EAAe,KAAK,YAAY,KAIjD,GAAI,EAAe,gBAAgB,EAAA,UAAU,SAAW,eAAgB,EAAgB,OAGxF,GAAI,EAAe,gBAAgB,EAAA,UAAU,QAAS,CACpD,GAAI,EAAE,eAAgB,EAAe,MACnC,MAAU,MACR,sBAAsB,EAAa,QAAQ,EAAQ,SAAS,2GAC7D,CAEH,EAAe,EAAe,KAAK,WAAW,YAAY,KAG5D,GAAM,CAAE,iBAAkB,EACpB,EAAU,CAAE,GAAG,EAAA,0BAA2B,GAAG,EAAc,YAAa,CAE9E,GAAI,KAAgB,GAAW,EAAQ,GAAuC,CAC5E,IAAI,EAAc,EAAQ,GAEtB,IAAgB,UAAY,EAAe,aAAY,EAAc,OAGvE,IAAA,QACA,IAAA,YACA,IAAA,UAEK,EAAkB,EAAc,OAAQ,E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