[英]how to have mutliple different instances of the same type in mock data
我有简单的架构和服务器; 架构.js:
const typeDefs = gql`
type Query {
people: [Person!]!
}
type Person {
name: String!
age: Int!
job: String
}
`;
还有我的服务器:
const mocks = {
Person: () => ({
name: () => "Henry Adams",
age: () => 21,
})
};
const server = new ApolloServer({ typeDefs, mocks: mocks });
server.listen().then(() => {
console.log(`
Listening on port 4000
Query at http://localhost:4000
`);
});
当我在 graphIQL 上查询时,上述内容按预期工作,如果我将 server.js 更改为以下内容,我知道如何拥有多个相同的确切实例:
const mocks = {
Query: () => ({
people: () => [...new Array(4)],
}),
Person: () => ({
name: () => "Henry Adams",
age: () => 21,
})
};
const server = new ApolloServer({ typeDefs, mocks: mocks });
server.listen().then(() => {
console.log(`
Listening on port 4000
Query at http://localhost:4000
`);
});
然而,这不是我想要做的,而是我想在我的模拟数据中做多个相同类型的不同实例,我尝试了两种不同的语法,令人惊讶的是无法通过谷歌搜索找到这个:语法一:
const mocks = {
Person: () => ({
name: () => "Henry Adams",
age: () => 21,
}),
Person: () => ({
name: () => "Berry, Lee",
age: () => 28,
})
};
当我使用上述语法查询时,只会出现第二个实例“Berry Lee”。 我假设的第二种语法会起作用:
const mocks = {
Person: () => ({
name: () => "Henry Adams",
age: () => 21,
},
{
name: () => "Berry, Lee",
age: () => 28,
}),
};
我对第二种语法有同样的问题,查询时只显示第二种类型。
我从apollo graphql 站点上的 apollo odyssey 教程 1 和 2 获得了原始代码,但做了一些进一步的挖掘,我发现了另一篇引起我注意的rootValue: root
(与原始帖子具有相同的架构):
const mocks= [
{
name: "Henry Adams",
age: 21,
},
{
name: "Benny Lee",
age: 29,
},
];
const root = {
people: () => mocks,
};
const server = new ApolloServer({ typeDefs, rootValue: root });
我不确定是否有办法使用mocks: true
或mocks: mocks
yet 所以我会在一段时间内不回答这个问题,以获得更详细的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.