【发布时间】:2018-01-05 03:40:39
【问题描述】:
我正在为 GraphQL API 模块化我的架构,并尝试在不使用任何 3rd 方库的情况下合并解析器。
如果没有Lodash.merge() 或等效项,有没有一种简单的方法可以做到这一点?
Apollo 文档说要使用 Lodash 之类的库来 merge() 模块化解析器。 (http://dev.apollodata.com/tools/graphql-tools/generate-schema.html#modularizing)
问题似乎在于,解析器本质上包含作为属性的函数,因此当我通过Object.assign() 甚至JSON.stringify() 访问它们时,它们似乎被省略了。
如果我控制台记录它们,我会看到:{"Query":{},"Mutation":{}}
这是其中一个解析器的样子:
const productResolvers = {
Query: {
myProducts: (root, { userId }, context) => {
return [
{ id: 1, amount: 100, expiry: '12625383984343', created: '12625383984343' },
{ id: 2, amount: 200, expiry: '12561351347311', created: '12625383984343' },
{ id: 3, amount: 200, expiry: '11346347378333', created: '12625383984343' },
{ id: 4, amount: 350, expiry: '23456234523453', created: '12625383984343' },
];
},
},
Mutation: {
addProduct: (root, { userId }, context) => {
return { id: 350, amount: 100, expiry: '12625383984343', created: '12625383984343' };
},
}
};
让我们假设有另一个几乎相同的名称widgetResolvers。
这是一个功能齐全的代码块:
export const schema = makeExecutableSchema({
typeDefs: [queries, mutations, productSchema, widgetSchema],
resolvers
});
这是我想要实现的目标:
export const schema = makeExecutableSchema({
typeDefs: [queries, mutations, productSchema, widgetSchema],
resolvers: Object.assign({}, productResolvers, widgetResolvers)
});
我还没有加载使用 rest spread 的能力 (https://babeljs.io/docs/plugins/transform-object-rest-spread/)。我怀疑它不会因为 Object.assign() 不起作用的相同原因而起作用。
哦,这就是我怀疑此合并不起作用的原因:Why doesn't JSON.stringify display object properties that are functions?
【问题讨论】:
-
我正在使用 Lodash
merge(),直至另行通知。效果很好。
标签: node.js schema graphql resolver apollo-server