【问题标题】:Postgraphile -- makeExtendSchemaPlugin to add Viewer Type to schema -- Relay/PostgresPostgraphile -- makeExtendSchemaPlugin 将查看器类型添加到模式 -- Relay/Postgres
【发布时间】:2021-03-22 14:04:29
【问题描述】:

我是 Relay、GraphQL 和 Postgres 的新手,我正在尝试扩展 PostGraphile 创建的自动生成的模式,以包含作为 Relay 的主要内容的查看器类型。

他们文档中的示例可能足以让大多数人使用,但我在充实它时遇到了麻烦。我基本上希望它为每个类型添加一个字段viewer: Viewer!

任何帮助都会很棒。

const { makeExtendSchemaPlugin, gql } = require('graphile-utils');

const AddViewerPlugin = makeExtendSchemaPlugin(build => {
  // Get any helpers we need from `build`
  const { pgSql: sql, inflection } = build;

  return {
    typeDefs: gql`...`,
    resolvers: {
      /*...*/ 
    },
  };
});

module.exports = AddViewerPlugin;

我的数据库有一个 public.person 表和一个 private.person_account 表,但我不愿重命名其中一个查看器。

【问题讨论】:

    标签: postgresql graphql relay postgraphile


    【解决方案1】:

    您使用“postgraphile”对象进行自动模式检测。 并使用“makeExtendSchemaPlugin”为其添加额外的解析器。

    makeExtendSchemaPlugin 创建一个插件对象,您可以在 postgrahile 对象构造中传递该对象。

    以下是 postgraphile 文档中 makeExtendSchemaPlugin page 的摘录:

    const { postgraphile } = require("postgraphile");
    const { makeExtendSchemaPlugin, gql } = require("graphile-utils");const express = require("express");
    const { convertUsdToAud } = require("ficticious-npm-library");
    const MyForeignExchangePlugin = makeExtendSchemaPlugin(build => {  return {    typeDefs: gql`      extend type Product {        priceInAuCents: Int! @requires(columns: ["price_in_us_cents"])      }    `,    resolvers: {      Product: {        priceInAuCents: async product => {          // Note that the columns are converted to fields, so the case changes          // from `price_in_us_cents` to `priceInUsCents`          const { priceInUsCents } = product;          return await convertUsdToAud(priceInUsCents);        },      },    },  };});
    
    const app = express();
    app.use(
      postgraphile(process.env.DATABASE_URL, ["app_public"], {
        graphiql: true,
        appendPlugins: [MyForeignExchangePlugin],  })
    );
    app.listen(3030);
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2021-10-14
      • 2016-08-15
      • 2021-11-26
      • 1970-01-01
      • 2021-05-04
      • 2017-03-28
      • 1970-01-01
      • 2019-02-12
      相关资源
      最近更新 更多