You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TypeScript+Prisma开发的Azure Function无法加载求助

TypeScript Yoga-GraphQL API部署到Azure Function后无法加载(Prisma相关问题)

环境信息

  • Node版本:22.8.0
  • npm版本:10.8.2
  • Azure Function版本:4.x

问题描述

用VS Code开发了一个基于TypeScript的Yoga-GraphQL API服务器应用,本地运行完全正常,但部署到Azure Function(已测试消耗计划和弹性消耗计划)后,函数无法加载。

经排查确认,只要代码中包含数据库调用逻辑就会报错,已通过硬编码数据库变量验证该问题。尝试过Azure Function门户和Application Insights排查,未找到有效线索,怀疑部署到Azure时未执行prisma generate命令。

已尝试操作

  • 检查主入口点、文件夹结构,均无异常
  • 使用简单的hello world schema时,应用可正常运行

package.json 部分内容

{
  "$schema": "https://json.schemastore.org/package",
  "name": "api-gateway",
  "version": "1.0.0",
  "description": "GraphQL API Gateway",
  "main": "dist/src/functions/*.js",
  "scripts": {
      "build": "npm run compile && npm run prisma:generate",
      "check:all": "concurrently npm:check:pretty npm:check:lint npm:check:type",
      "check:lint": "eslint .",
      "check:pretty": "prettier --check .",
      "check:type": "tsc --noEmit",
      "clean": "rimraf dist",
      "compile": "tsc",
      "dev": "NODE_ENV=development tsx watch --clear-screen=false src/main.ts",
      "doc:graphql:generate": "magidoc generate",
      "doc:graphql:preview": "magidoc preview --port 4100",
      "lint-staged": "lint-staged",
      "local": "tsx src/main.ts",
      "prepare": "husky",
      "prisma:db:pull": "prisma db pull",
      "prisma:db:seed": "prisma db seed",
      "prisma:generate": "prisma generate",
      "prisma:migrate:dev": "prisma migrate dev --name",
      "prestart": "npm run clean && npm run build",
      "start": "func start",
      "start-v": "npm run clean && npm run build && func start --verbose",
      "pretest": "npm run check:all && npm run build",
      "test": "echo \"Tests are not implemented yet\"",
      "tscc": "tsc --showConfig",
      "watch": "tsc -w",
      "postinstall": "prisma generate" 
  },

更新内容

在弹性消耗计划(Linux)中可看到函数已加载,但出现错误(附错误截图)

更新2:相关代码及结构说明

schema.prisma 内容

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["multiSchema"]
}

generator pothos {
  provider = "prisma-pothos-types"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
  schemas  = ["product", "matr"]
}

model Address {
  id              Int               @id @default(autoincrement())
  addressLine1    String            @db.Text
  ...

  @@schema("product")
}

model Contact {
  id                     Int               @id @default(autoincrement())
  contactId           String?
  accountId           String?
...
  @@schema("product")
}

Function App(版本4)结构说明

  • index.ts位于schema文件夹中(附结构截图)
  • src目录下无index.ts
  • 该结构在本地运行正常
  • 若移除index.ts中所有与数据库调用相关的.ts导入,或硬编码数据库相关值,该结构在本地和Azure均可正常运行(附验证截图)

HTTP触发器函数代码

import { createYoga } from 'graphql-yoga'
import { InvocationContext, HttpRequest, HttpResponseInit, app } from '@azure/functions'
import { schema } from '../schema'

/**
 * Create the Graphql-Yoga server
 */
const yoga = createYoga({
  graphqlEndpoint: '/graphql',
  schema: schema,
  graphiql: true,
})

/**
 * Create the handler for Azure function
 *
 * @param req The request object
 * @param context The context object
 *
 * @returns The response object
 */
const httpTrigger1 = async function (
  req: HttpRequest,
  context: InvocationContext,
): Promise<HttpResponseInit> {
  //  Invoke the Graphql server
  const response = await yoga.fetch(req.url, {
    method: req.method?.toString(),
    body: req.body,
    headers: req.headers as HeadersInit,
  })

  const responseHeaders = Object.fromEntries(response.headers.entries())

  // Log the response details
  context.log('Response Headers:', responseHeaders)

  const responseText = await response.text()

  //  Prepare the response object and send back
  return {
    status: response.status,
    body: responseText,
    headers: responseHeaders,
  }
}

/**
 * Set the configuration for the Azure function
 */
app.http('graphql', {
  methods: ['POST', 'GET'],
  authLevel: 'anonymous',
  handler: httpTrigger1,
})

export { httpTrigger1 }

内容的提问来源于stack exchange,提问作者jAntoni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 16:41:15