模式必须包含唯一的命名类型,但必须包含多个名为“ Page”的类型

时间:2018-11-16 07:17:49

标签: javascript graphql graphql-js

我创建了一个GraphQL 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 });
      }
    },
  })
});

1 个答案:

答案 0 :(得分: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 });
      }
    },
  })
});