我有两个资源:Purchase
和Category
,其中购买属于某个类别,类别有很多购买。我正在尝试允许查询,例如:
query {
purchases {
description,
amount,
category {
name
}
}
}
或
query {
categories {
name,
purchases {
description,
amount
}
}
}
设置我的类型时,出现以下错误:
Schema must contain unique named types but contains multiple types named "Category".
我很确定这是一个循环参考问题,但我已经使用了thunk,因为我看到其他答案提示:
// Category/type.js
const {GraphQLObjectType, GraphQLInt, GraphQLString, GraphQLList} = require('graphql/type');
const {purchaseType} = require('../Purchase');
const categoryType = new GraphQLObjectType({
name: 'Category',
description: 'a purchasing category',
fields: () => ({
id: {
type: GraphQLInt
},
name: {
type: GraphQLString
},
purchases: {
type: new GraphQLList(purchaseType),
resolve (category) {
return category.getPurchases();
}
}
})
});
module.exports = categoryType;
// Category/index.js
const categoryType = require('./type');
const categoryQuery = require('./query');
module.exports = {
categoryType,
categoryQuery
};
// Purchase/type.js
const {GraphQLObjectType, GraphQLInt, GraphQLString, GraphQLFloat} = require('graphql/type');
const {categoryType} = require('../Category');
const purchaseType = new GraphQLObjectType({
name: 'Purchase',
description: 'a specific purchase',
fields: () => ({
id: {
type: GraphQLInt,
},
description: {
type: GraphQLString,
},
amount: {
type: GraphQLFloat,
},
category: {
type: categoryType,
resolve (purchase) {
return purchase.getCategory();
}
}
})
});
module.exports = purchaseType;
// Purchase/index.js
const purchaseType = require('./type');
const purchaseQuery = require('./query');
module.exports = {
purchaseType,
purchaseQuery
};
我在这里搞砸了什么或是我想要做的事情吗?
编辑#1
是什么让我相信这是一个循环引用问题,如果我将category
上的字段purchaseType
更改为以下内容:
category: {
// type: categoryType,
// resolve (purchase) {
// return purchase.getCategory();
// }
type: GraphQLInt,
resolve (purchase) {
return 10;
}
}
并执行与categoryType
purchases
字段类似的操作,它可以正常运行。
答案 0 :(得分:0)
Schema must contain unique named types but contains multiple types named "Category".
此错误很明显,这意味着您声明的各种GraphQLObjectType
多次包含名称Category
,请检查GraphQLObjectType
的所有名称,并更改它,不应该&# 39;相同或请更新问题中的所有Type
。
注意: GraphQLType
的所有名称必须是唯一的,它不应该相互匹配
const categoryType = new GraphQLObjectType({
name: 'Category', -------> others GraphQLObjectType may have same name Category
description: 'a purchasing category',
fields: () => ({
.....
})
});
答案 1 :(得分:0)
我的问题最终成了大写问题。
在schema.js
我正在做的事情:
const {purchaseType, purchaseQuery} = require('./resources/purchase/');
在Category/type.js
我正在做的事情:
const {purchaseType} = require('../Purchase');
Node以区分大小写的方式缓存模块,因此它正在加载purchaseTyp
模块两次。