Adding and replacing inflectors
Inflection relates to naming things; please see the inflection article for more information on inflection in PostGraphile.
命名推导 inflection 负责控制各类 GraphQL 名称的生成规则。关于 PostGraphile 中 inflection 的更多细节,请参阅Inflection 命名推导一文。
In V4 of PostGraphile we had makeAddInflectorsPlugin, but in V5 this is no
longer needed since inflection is now a first class feature of the plugin
system. Writing a plugin to add or replace inflectors is relatively
straightforward:
在 PostGraphile V4 中,我们曾使用 makeAddInflectorsPlugin;而在 V5 中,由于 inflection 已成为插件系统的一等公民特性,不再需要该辅助函数。编写插件来新增或替换 inflector 非常直观:
Replacing an inflector替换已有 inflector
Replacing an inflector is slightly easier than adding a new one because there’s no need to define the TypeScript types (you just use the existing types); so we’ll start there.
替换已有 inflector 比新增一个稍微简单一些,因为无需额外声明 TypeScript 类型(直接复用现有类型即可),所以我们先从这里开始。
Replacement inflectors are added via a plugin, using the
inflection.replace.<inflectorName> property, which should be set to a
function. This function accepts three or more parameters:
要替换 inflector,可在插件中配置 inflection.replace.<inflectorName> 属性,并将其值设为一个函数。该函数接收三个或更多参数:
-
The previous version of the inflector, for delegation, or
nullif there wasn’t previously an inflector with this name该
inflector的上一个版本(便于委托调用),如果此前不存在同名inflector则为null -
The resolved
graphile-configpreset that the user is using用户当前使用的已解析
graphile-configpreset -
All remaining parameters are the inflector’s inputs, from which a name should be derived
其余所有参数均为该
inflector接收的输入参数,用于从中推导出最终名称
Example 1示例 1
This following plugin replaces the builtin inflector with one that returns
RootQuery instead of Query for the root query type:
以下插件替换了 builtin inflector,使根 Query 类型的名称返回 RootQuery 而非默认的 Query:
// Import types for TypeScript, no need in JS
import "postgraphile";
export const ReplaceInflectorPlugin: GraphileConfig.Plugin = {
// Unique name for your plugin:
name: "ReplaceInflectorPlugin",
version: "0.0.0",
inflection: {
replace: {
builtin(
// The previous version of this inflector, the one you're replacing
previous,
// The resolved configuration
preset,
// Everything from the 3rd paramater onward are the arguments to this inflector
text,
) {
if (name === "Query") return "RootQuery";
return previous(text);
},
},
},
};
The first two arguments to your replace inflector definition are supplied by the
Graphile Build system and hidden from calling code, so only arguments from the
third onward are supplied by the calling code. For example, a replacement for
the builtin inflector could be defined as above, but calling code would only
supply the third argument:
替换 inflector 定义中的前两个参数由 Graphile Build 系统自动提供,对调用方代码是透明的,因此调用方只需传入从第三个开始的参数即可。例如,即使 builtin inflector 按上述方式定义,实际调用时也只需要传递第三个参数:
const text = inflection.builtin("Query");
Example 2示例 2
If you want *Patch types to instead be called *ChangeSet you could make a
plugin such as this one:
如果你想把所有的 *Patch 类型改名为 *ChangeSet,可以编写如下插件:
// Import types for TypeScript, no need in JS
import "postgraphile";
export const ReplaceInflectorPlugin: GraphileConfig.Plugin = {
// Unique name for your plugin:
name: "ReplaceInflectorPlugin",
version: "0.0.0",
inflection: {
replace: {
patchType(previous, preset, typeName) {
return this.upperCamelCase(`${typeName}-change-set`);
},
},
},
};
Adding a new inflector新增 inflector
Adding a new inflector is very much like adding a replacement inflector, with the following differences:
新增 inflector 的写法与替换 inflector 非常相似,主要区别在于以下两点:
-
No
previousinflector existed, so the first argument is omitted由于此前不存在
previousinflector,因此省略了第一个参数 -
The types won’t already exist, so you must declare them yourself, via the
global.GraphileBuild.Inflectioninterface系统中尚无对应类型定义,因此必须通过
global.GraphileBuild.Inflection接口自行声明类型
In JS, adding a new inflector is straightforward:
在 JavaScript 中,新增 inflector 非常直接:
export const MyNewInflectorPlugin: GraphileConfig.Plugin = {
name: "MyNewInflectorPlugin",
version: "0.0.0",
inflection: {
add: {
enhanced(preset, columnName) {
return columnName + "Enhanced";
},
},
},
};
In TypeScript, it’s somewhat more verbose as we use declaration merging to make other plugins aware of the new inflector:
而在 TypeScript 中会稍微繁琐一些,需要借助声明合并(Declaration Merging)来让其他插件识别这个新的 inflector:
// Import types for TypeScript, no need in JS
import "postgraphile";
declare global {
namespace GraphileBuild {
interface Inflection {
/**
* Add documentation for your inflector here.
*/
enhanced(this: Inflection, columnName: string): string;
}
}
}
export const MyNewInflectorPlugin: GraphileConfig.Plugin = {
name: "MyNewInflectorPlugin",
version: "0.0.0",
inflection: {
add: {
enhanced(preset, columnName) {
return columnName + "Enhanced";
},
},
},
};
The first argument to your add inflector definition is supplied by the Graphile
Build system and hidden from calling code, so only arguments from the second
onward are supplied by the calling code. For example, the enhanced inflector
could be defined as above, but calling code would only supply the second
argument:
新增 inflector 定义中的第一个参数同样由 Graphile Build 系统自动提供,对调用方代码隐藏,因此调用方只需传入从第二个开始的参数。例如,按上述方式定义 enhanced inflector 后,调用时只需传入第二个参数:
const text = inflection.enhanced("avatarUrl");