函数中的 return 值可以用 Postgraphile 的特定名称命名吗?

Can a return value from a function be named with a specific name for Postgraphile?

我在 PostgreSQL 中有这个函数:

CREATE FUNCTION it_exists(
  email text,
  name text
) RETURNS boolean AS $$
DECLARE
  _eva1 boolean;
  _eva2 boolean;
BEGIN
  _eva1 := EXISTS(SELECT * FROM tableA AS A WHERE A.email = ::citext);
  _eva2 := EXISTS(SELECT * FROM tableB AS A WHERE A.name::citext = ::citext);
  RETURN _eva1 OR _eva2;
END;
$$ LANGUAGE plpgsql STRICT SECURITY DEFINER;

翻译成Postgraphile是这样的:

mutation MyMutation($email: String!, $name: String!) {
  itExists(
    input: { email: $email, name: $name }
  ) {
    boolean
  }
}

我想将“布尔”名称更改为类似“结果”的名称,有什么建议吗?假设我有许多具有不同 return 值的函数。

尝试返回 table:

CREATE FUNCTION it_exists(
  email text,
  name text
) RETURNS TABLE (result boolean) AS $$ -- return TABLE type
DECLARE
  _eva1 boolean;
  _eva2 boolean;
BEGIN
  _eva1 := EXISTS(SELECT * FROM tableA AS A WHERE A.email = ::citext);
  _eva2 := EXISTS(SELECT * FROM tableB AS A WHERE A.name::citext = ::citext);
  RETURN QUERY SELECT _eva1 OR _eva2; -- modify RETURN to suit TABLE type
END;
$$ LANGUAGE plpgsql STRICT SECURITY DEFINER;

我认为 Postgraphile 这样做是为了让 its custom mutations follow the Relay specification

By convention, mutations are named as verbs, their inputs are the name with "Input" appended at the end, and they return an object that is the name with "Payload" appended.

因此您的自定义突变创建了一个 ItExistsPayload 类型,其中只有一个字段,而不是返回 GraphQL Boolean。将来您可能希望使用更多字段扩展此有效负载对象。

可以使用 @resultFieldName smart tag 重命名该字段。你的情况:

COMMENT ON FUNCTION it_exists(text, text) IS '@resultFieldName result';