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

在Supabase Edge Functions中调用GraphQL失败问题排查

问题

在Supabase Edge Functions中通过GraphQL查询数据库时失败,报错信息:Could not find the function public.graphql.resolve(query) in the schema cache。但在Supabase GUI中执行相同GraphQL查询可正常获取数据,使用的代码如下:

import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { createClient } from "jsr:@supabase/supabase-js@2";
Deno.serve(async (req)=>{
  if (req.method === 'OPTIONS') {
    return new Response('ok', {
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET, POST, OPTIONS'
      }
    });
  }
  try {
    const supabaseClient = createClient(Deno.env.get('SUPABASE_URL'), Deno.env.get('SUPABASE_ANON_KEY'));
    const query = `
      {
        clientsCollection {
          edges {
            node {
              client_id
              first_name
              email
              phone_number
            }
          }
        }
      }
    `;
    const { data, error } = await supabaseClient.rpc('graphql.resolve', {
      query: query
    });
    if (error) throw error;
    return new Response(JSON.stringify(data), {
      headers: {
        'Content-Type': 'application/json',
        'Connection': 'keep-alive'
      }
    });
  } catch (error) {
    return new Response(JSON.stringify({
      error: error.message
    }), {
      headers: {
        'Content-Type': 'application/json'
      },
      status: 400
    });
  }
});
解决方法
  • 确认GraphQL扩展已启用:进入Supabase控制台「Database」→「Extensions」,找到graphql扩展并确认状态为启用。即使GUI能正常查询,Edge Functions调用可能依赖扩展的完整初始化。
  • 修正函数权限与schema归属:graphql.resolve属于graphql schema而非public,先给anon用户赋予执行权限,在SQL编辑器运行:
    GRANT EXECUTE ON FUNCTION graphql.resolve(text) TO anon;
    
  • 改用官方GraphQL客户端方法:Supabase JS客户端提供了直接的graphql()方法,无需通过rpc调用,修改查询代码:
    const { data, error } = await supabaseClient.graphql(query);
    
    这种方式会自动对接GraphQL端点,避免schema和权限的额外问题。
  • 刷新schema缓存:若上述操作无效,进入Supabase控制台「Database」→「Schema」刷新缓存,或重新部署Edge Functions,确保最新的schema被加载。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 06:51:11