KeystoneJS 'resolveInput' 字段挂钩:从字段挂钩返回 'resolvedData' 会产生 GraphQL 转换错误
KeystoneJS 'resolveInput' field hook: returning 'resolvedData' from field hook produces GraphQL cast error
我正在尝试修改 KeystoneJS 5 中特定字段的保存数据。resolveInput
field hook 似乎是执行此操作的好方法。这是上面文档中给出的示例:
const resolveInput = ({
operation,
existingItem,
originalInput,
resolvedData,
context,
actions,
}) => {
// Input resolution logic
// Object returned is used in place of resolvedData
return resolvedData;
};
The return of resolveInput can be a Promise or an Object. It should
resolve to the same structure as the resolvedData. The result is
passed to the next function in the execution order.
因此,我列表中字段的定义是
text: {
type: Wysiwyg,
hooks: {
async resolveInput({ originalInput,resolvedData }) {
console.log('resolvedData');
console.log(resolvedData);
return resolvedData;
}
}
开始,我只是 returning 传入的 resolvedData
参数,根据文档,它是
resolvedData Object The data received by the GraphQL mutation plus defaults values
但是,我每次都得到以下错误
GraphQLError
Cast to string failed for value "{ text: '<p><img src="static/blop.jpg" alt="" width="580" height="388" /><img src="static/blop.jpg" alt="" /></p>' }" at path "text"
我不确定问题是什么或如何解决它 -- 我只是 returning 传入的相同数据。当我删除挂钩时,一切正常。
编辑
我相信答案是 不 return 整个 resolvedData
对象,而是 字段 在你所在的那个对象上。因此,在下面的例子中,一个人会 return resolvedData.text
(或 resolvedData.title
)。我相信文档可以改进,因为看起来 return 是整个 resolvedData
对象。
你是对的:根据@keystonejs/keystone/lib/List/index.js 中的代码,应该 return 从 resolveInput 挂钩 field 对象而不是整个 resolvedData 对象:
// Run the schema-level field hooks, passing in the results from the field
// type hooks
resolvedData = {
...resolvedData,
...(await this._mapToFields(
this.fields.filter(field => field.hooks.resolveInput),
field => field.hooks.resolveInput({ ...args, resolvedData })
)),
};
returning 值分配给 field 元素 resolvedData 定义 resolveInput 挂钩的对象。
我正在尝试修改 KeystoneJS 5 中特定字段的保存数据。resolveInput
field hook 似乎是执行此操作的好方法。这是上面文档中给出的示例:
const resolveInput = ({
operation,
existingItem,
originalInput,
resolvedData,
context,
actions,
}) => {
// Input resolution logic
// Object returned is used in place of resolvedData
return resolvedData;
};
The return of resolveInput can be a Promise or an Object. It should resolve to the same structure as the resolvedData. The result is passed to the next function in the execution order.
因此,我列表中字段的定义是
text: {
type: Wysiwyg,
hooks: {
async resolveInput({ originalInput,resolvedData }) {
console.log('resolvedData');
console.log(resolvedData);
return resolvedData;
}
}
开始,我只是 returning 传入的 resolvedData
参数,根据文档,它是
resolvedData Object The data received by the GraphQL mutation plus defaults values
但是,我每次都得到以下错误
GraphQLError
Cast to string failed for value "{ text: '<p><img src="static/blop.jpg" alt="" width="580" height="388" /><img src="static/blop.jpg" alt="" /></p>' }" at path "text"
我不确定问题是什么或如何解决它 -- 我只是 returning 传入的相同数据。当我删除挂钩时,一切正常。
编辑
我相信答案是 不 return 整个 resolvedData
对象,而是 字段 在你所在的那个对象上。因此,在下面的例子中,一个人会 return resolvedData.text
(或 resolvedData.title
)。我相信文档可以改进,因为看起来 return 是整个 resolvedData
对象。
你是对的:根据@keystonejs/keystone/lib/List/index.js 中的代码,应该 return 从 resolveInput 挂钩 field 对象而不是整个 resolvedData 对象:
// Run the schema-level field hooks, passing in the results from the field
// type hooks
resolvedData = {
...resolvedData,
...(await this._mapToFields(
this.fields.filter(field => field.hooks.resolveInput),
field => field.hooks.resolveInput({ ...args, resolvedData })
)),
};
returning 值分配给 field 元素 resolvedData 定义 resolveInput 挂钩的对象。