changeNullability
Use this plugin generator to easily change the nullability of fields and arguments in your GraphQL schema.
使用该插件生成器,可以方便地修改 GraphQL schema 中字段和参数的可空性(nullability)。
For more information about nullability in PostGraphile in general (and to help determine if the change that you want to make is wise or misguided), see the FAQ question “Why is it nullable?”
关于 PostGraphile 中可空性设计的背景说明(以及帮助你判断某项改动是否合理),请参阅常见问题“为什么字段是可空的?”。
Function signature函数签名
The changeNullability function accepts one parameter, a rules object:
changeNullability 函数接收一个规则对象作为参数:
function changeNullability(
rules: ChangeNullabilityRules,
): GraphileConfig.Plugin;
interface ChangeNullabilityRules {
[typeName: string]: {
[fieldName: string]:
| NullabilitySpecString
| {
type?: NullabilitySpecString;
args?: {
[argName: string]: NullabilitySpecString;
};
};
};
}
Note that the format allows you to indicate the nullability for a field directly, or to pass an object in which you can indicate the field nullability and the nullability of that field’s arguments at the same time.
如上所示,该格式既支持直接为字段指定可空性,也允许传入一个配置对象,从而同时声明字段本身以及该字段各参数的可空性。
The NullabilitySpecString uses syntax similar to GraphQL’s SDL to define the
nullability of a field, including control over lists and nested lists:
NullabilitySpecString 借鉴了 GraphQL SDL 的语法来定义字段的可空性,支持对列表及嵌套列表进行精细控制:
export type NullabilitySpecString =
| "" // nullable
| "!" // non-nullable
| "[]" // nullable list of nullables
| "[]!" // non-nullable list of nullables
| "[!]" // nullable list of non-nullables
| "[!]!"
| "[[]]"
| "[[]]!"
| "[[]!]"
| "[[]!]!"
| "[[!]]"
| "[[!]]!"
| "[[!]!]"
| "[[!]!]!";
Example使用示例
To indicate that UsersConnection.nodes should be a non-nullable list of
non-nullables you would do:
例如,若要将 UsersConnection.nodes 设置为“元素非空且列表本身也非空”的类型,可以这样写:
import { changeNullability } from "postgraphile/utils";
const MyNullabilityPlugin = changeNullability({
UsersConnection: {
nodes: "[!]!",
},
});