Postgraphile:只需要函数中的一些参数

Postgraphile: Only some parameters in a function required

当我在 Postgresql 中编写函数时,我可以添加 strict 导致在 Graphql (!) 中所有参数都是必需的,没有 strict 所有参数都是可选的。

在非严格函数中的必填字段上,我可以抛出异常或简单地 return null 而不执行函数,就像参数为 null 时严格函数所做的那样。但是我怎么能告诉 Postgraphile 需要哪些参数呢?通过评论或其他方式,或将具有默认值的参数标记为可选。

在我的搜索中,我找到了这个问题 here, and especially this 的答案。 我在引用中找到的答案:

This functionality is implemented in graphile-build-pg via the pgStrictFunctions setting, but this isn't currently exposed via postgraphile-core to PostGraphQL itself. What it does is treats all functions as strict, requiring all arguments to be marked as required unless they have defaults.

It is possible to mark an argument as default null, but of course a strict function with a null default will automatically return null without being called.

create function a(b int, c int default null)
returns int as $$
  select b;
$$ language sql stable;

By default neither b nor c will be required, but with pgStrictFunctions set b will be marked as required (but c will not).

虽然它提到了一个解决方案,但没有说明如何实施它。在整个项目(包括 node_modules)中搜索 'pgStrictFunctions' 也没有给出任何线索。如何实现我终于在 discord 上找到了,这就是我最终让它工作的方式:

@Module({
  controllers: [],
  imports: [
    PostGraphileModule.forRoot({
      ......
      graphileBuildOptions: {
        pgStrictFunctions: true,
      },
      ......
    }),
  ],
  providers: [GraphQLService],
})