【问题标题】:Schema must contain unique named types but contains multiple types named "Page"Schema 必须包含唯一的命名类型,但包含多个名为“Page”的类型
【发布时间】:2019-04-19 08:43:06
【问题描述】:

我创建了一个 GraphQL 页面函数。但是,当我尝试重用它时,我得到了错误

架构必须包含唯一的命名类型,但包含多种类型 命名为“页面”

我知道我可以将Page 更改为两个相同的函数,并将它们命名为JobsPagePrintsPage

但是,有没有一种简单的方法可以重复使用它?谢谢

function Page(itemType) {
  return new GraphQLObjectType({
    name: 'Page',
    fields: () => ({
      totalCount: { type: GraphQLInt },
      edges: { type: new GraphQLList(Edge(itemType)) },
      pageInfo: { type: PageInfo }
    })
  });
}


const PrinterType = new GraphQLObjectType({
  name: 'Printer',
  fields: () => ({
    id: { type: GraphQLString },

    jobs: {
      type: Page(JobType),     // here I use Page first time
      args: {
        first: {
          type: GraphQLInt
        },
        after: {
          type: GraphQLString
        }
      },
      resolve(parentValue, args) {
        const { id } = parentValue;
        const { first, after } = args;

        return getPageJobs({ id, first, after });
      }
    },

    prints: {
      type: Page(PrintType),   // here I use Page again
      args: {
        first: {
          type: GraphQLInt
        },
        after: {
          type: GraphQLString
        }
      },
      resolve(parentValue, args) {
        const { id } = parentValue;
        const { first, after } = args;

        return getPagePrints({ id, first, after });
      }
    },
  })
});

【问题讨论】:

  • FWIW,如果您的架构包含 Relay 使用的连接或其他功能,那么 Relay 实际上有一个库,其中包含不同的实用程序来在 GraphQL.js 服务器中实现这些功能:github.com/graphql/graphql-relay-js

标签: javascript graphql graphql-js


【解决方案1】:

我错过了它只是一个纯函数,我可以传递第二个值...

这是一种解决方案。

function Page(itemType, name) {
  return new GraphQLObjectType({
    name: `${name}Page`,
    fields: () => ({
      totalCount: { type: GraphQLInt },
      edges: { type: new GraphQLList(Edge(itemType, name)) },
      pageInfo: { type: PageInfo }
    })
  });
}


const PrinterType = new GraphQLObjectType({
  name: 'Printer',
  fields: () => ({
    id: { type: GraphQLString },

    jobs: {
      type: Page(JobType, 'Jobs'),     // here I use Page first time
      args: {
        first: {
          type: GraphQLInt
        },
        after: {
          type: GraphQLString
        }
      },
      resolve(parentValue, args) {
        const { id } = parentValue;
        const { first, after } = args;

        return getPageJobs({ id, first, after });
      }
    },

    prints: {
      type: Page(PrintType, 'Prints'),   // here I use Page again
      args: {
        first: {
          type: GraphQLInt
        },
        after: {
          type: GraphQLString
        }
      },
      resolve(parentValue, args) {
        const { id } = parentValue;
        const { first, after } = args;

        return getPagePrints({ id, first, after });
      }
    },
  })
});

【讨论】:

    猜你喜欢
    • 2021-05-08
    • 2023-01-01
    • 2020-06-28
    • 2022-12-22
    • 2021-08-24
    • 2019-03-26
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多