Skip to main content

makeAddPgTableConditionPluginmakeAddPgTableConditionPlugin 迁移

The (simplified) signature for makeAddPgTableConditionPlugin in V4 was:

V4 中 makeAddPgTableConditionPlugin 的(简化)函数签名如下:

// V4 signature
function makeAddPgTableConditionPlugin(
schemaName: string,
tableName: string,
conditionFieldName: string,
fieldSpecGenerator: (build: Build) => GraphQLInputFieldConfig,
conditionGenerator: (
value: unknown,
helpers: { queryBuilder: QueryBuilder; sql: PgSQL; sqlTableAlias: SQL },
build: Build,
) => SQL,
): Plugin;

In V5, the signature has changed a little.

到了 V5,该函数的签名发生了一些变化。

The first change is trivial: we've combined the first two arguments into a "match" object which also optionally accepts the serviceName.

第一个变化很直观:原来的前两个参数被合并成了一个“匹配(match)”对象,且该对象还支持可选的 serviceName 属性。

The second change may be more involved depending on your previous code; the conditionGenerator signature has changed and rather than using the queryBuilder there's a condition to write to. You can still use sql.value(value) for embedding values as before, but it's recommended that instead you use sqlValueWithCodec(value, codec) as this will take care of casting the value via the relevant codec so it's the correct shape when it reaches the database. This matters more with more complex types.

第二个变化的影响取决于你原有的代码逻辑:conditionGenerator 的签名有所调整,不再提供 queryBuilder,而是改为传入一个可供写入条件的 condition 对象。你依然可以像以前一样用 sql.value(value) 嵌入值,但更推荐改用 sqlValueWithCodec(value, codec)——它会通过对应的 codec 自动处理类型转换,确保传入数据库的值格式准确,这在处理复杂类型时尤为关键。

The (simplified) new signature is:

V5 中的(简化)新签名如下:

// V5 signature
function addPgTableCondition(
match: { serviceName?: string; schemaName: string; tableName: string },
conditionFieldName: string,
fieldSpecGenerator: (build: GraphileBuild.Build) => GrafastInputFieldConfig,

// OPTIONAL:
conditionGenerator?: (
value: unknown,
helpers: {
sql: typeof sql;
sqlTableAlias: SQL;
sqlValueWithCodec: typeof sqlValueWithCodec;
build: ReturnType<typeof pruneBuild>;
condition: PgCondition;
},
) => SQL | null | undefined,
): GraphileConfig.Plugin;

Note that the conditionGenerator is now optional because you can choose to instead include an apply (or extensions.grafast.apply) entry in the result of fieldSpecGenerator.

需要注意的是,conditionGenerator 现在变为了可选参数,因为你也可以选择在 fieldSpecGenerator 的返回对象中直接提供 apply(或 extensions.grafast.apply)方法。

Here's an example:

下面来看一个具体示例:

Example 1示例 1​

V4:

import { makeAddPgTableConditionPlugin } from "graphile-utils";

const PetsCountPlugin = makeAddPgTableConditionPlugin(
"graphile_utils",
"users",
"petCountAtLeast",
(build) => ({
description: "Filters users to those that have at least this many pets",
type: build.graphql.GraphQLInt,
}),
(value, helpers, build) => {
const { sqlTableAlias, sql } = helpers;
return sql.fragment`(select count(*) from graphile_utils.pets where pets.user_id = ${sqlTableAlias}.id) >= ${sql.value(
value,
)}`;
},
);

V5:

import { addPgTableCondition } from "postgraphile/utils";
import { TYPES } from "postgraphile/@dataplan/pg";

const PetsCountPlugin = addPgTableCondition(
{ schemaName: "graphile_utils", tableName: "users" },
"petCountAtLeast",
(build) => ({
description: "Filters users to those that have at least this many pets",
type: build.graphql.GraphQLInt,
}),
(value, helpers) => {
const { sqlTableAlias, sql, sqlValueWithCodec } = helpers;
return sql.fragment`(select count(*) from graphile_utils.pets where pets.user_id = ${sqlTableAlias}.id) >= ${sqlValueWithCodec(
value,
TYPES.int,
)}`;
},
);