No makeAddInflectorsPlugin不再提供 makeAddInflectorsPlugin
makeAddInflectorsPlugin no longer exists; the new plugin system makes it a
little easier to write it yourself so the abstraction is no longer required.
makeAddInflectorsPlugin 在 V5 中已被移除;新版插件系统让自行实现命名推导 inflection 变得更加简单,因此不再需要这层封装。
Replacing an inflector替换已有 inflector
Set inflection.replace.inflectorName in a plugin to a new inflector function;
the function will be passed the previous inflector function as the first
argument (null if there wasn't one), the resolved GraphileConfig preset as the
second argument, and any data passed to it as the remaining arguments.
在插件中将 inflection.replace.inflectorName 设为新的 inflector 函数即可完成替换。该函数依次接收以下参数:第一个参数为上一个版本的 inflector 函数(如果此前不存在则为 null),第二个参数为已解析的 GraphileConfig preset,其余参数则是调用该 inflector 时传入的数据。
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");
Adding a new inflector新增 inflector
New inflectors accept the current preset as the first argument and any number of additional arguments after that.
新增的 inflector 函数以当前 preset 作为第一个参数,后面可以接收任意数量的附加参数。
In JS, adding a new inflector is straightforward:
在 JavaScript 中,新增 inflector 非常直接:
export const MyNewInflectorPlugin: GraphileConfig.Plugin = {
name: "MyNewInflectorPlugin",
version: "0.0.0",
inflection: {
add: {
myNewInflector(preset, columnName) {
return columnName + "Something";
},
},
},
};
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");