架构必须包含唯一的命名类型,但包含多个名为 "Page" 的类型

Schema must contain unique named types but contains multiple types named "Page"

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

Schema must contain unique named types but contains multiple types named "Page"

我知道我可以将 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 });
      }
    },
  })
});

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

这是一种解决方案。

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 });
      }
    },
  })
});