【发布时间】:2019-07-20 06:46:07
【问题描述】:
我是 graphQL 和 mongoDB 的新手,我正在尝试让它在我的项目中工作。问题在于 GraphiQL 中来自查询的数据与来自客户端内部相同查询的数据完全不同。这是我的架构设置:
const graphql = require('graphql');
const _ = require('lodash');
const Item = require('../models/item');
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList
} = graphql;
const ItemType = new GraphQLObjectType({
name: 'Item',
fields: () => ({
name: {
type: GraphQLString
},
id: {
type: GraphQLID
},
description: {
type: GraphQLString
},
price: {
type: GraphQLInt
},
image: {
type: GraphQLString
},
category: {
type: GraphQLString
}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
item: {
type: ItemType,
args: {
id: {
type: GraphQLID
}
},
resolve(parent, args) {
// code to get data from db / other source
return Item.findById(args.id);
}
},
items: {
type: new GraphQLList(ItemType),
resolve(parent, args) {
return Item.find({})
}
}
}
});
当我从 graphiQL 对我收到的所有项目和数据进行查询时,是“正确的”。看起来像这样:
当我像这样从前端执行相同的确切查询时: 从“apollo-boost”导入 { gql };
const getItemsQuery = gql`
{
items {
name
id
description
price
image
category
}
}
`;
export { getItemsQuery };
看起来它一遍又一遍地重复第一项,我不明白为什么。 DB 也显示正确的项目。我的服务器端代码可以在这里找到:https://github.com/KamilStaszewski/shoppy/tree/adding_graphQL/server
【问题讨论】:
-
确实很奇怪。应该是客户端的问题。也许这与您在 React 应用程序中使用查询的方式有关,您可以链接我在您的 github 中发生的文件或将其包含在您的帖子中吗?找不到(可能是你没推送)
-
这看起来像是一个缓存问题。您使用的是 ApolloCache 的自定义实例吗?您可以发布您的客户端配置吗?
-
我是新手,所以我不认为我正在使用 apollocache 的自定义实例。一切都是默认的。如果您的意思是我的客户端配置,那么 create-react-app 和 mongodbatlast 具有默认配置。
-
能否显示您对
items进行查询调用的位置并设置性能