【发布时间】:2019-03-18 01:55:30
【问题描述】:
我尝试使用apollo graphQl client 进行 GraphQl 突变。
当变异变量包含___typename 属性时,这会生成error 500(这显然不存在于 graphQl 架构中)。
要解决可以在 graphQl 客户端配置中设置 addTypename: false 的问题:
const graphqlClient = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache({
addTypename: false
})
})
现在突变几乎起作用了……
但是有一个新的错误:You're using fragments in your queries, but either don't have the addTypename: true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename. Please turn on the addTypename option and include __typename when writing fragments so that Apollo Client can accurately match fragments.
那么应该如何配置 graphQl 客户端来处理突变呢?
现在我使用here找到的清理功能:
const removeTypename = (value) => {
if (value === null || value === undefined) {
return value;
} else if (Array.isArray(value)) {
return value.map(v => removeTypename(v));
} else if (typeof value === 'object') {
const newObj = {};
Object.keys(value).forEach(key => {
if (key !== '__typename') {
newObj[key] = removeTypename(value[key]);
}
});
return newObj;
}
return value;
};
但感觉很hacky。有没有内置的graphql客户端?
【问题讨论】:
-
您是否尝试将另一个查询返回的数据用作您的变异变量之一?否则,不清楚传递给突变的变量何时包含
__typename字段。 -
是的。
___typename是从之前的查询中添加的
标签: javascript graphql graphql-js apollo-client