如何解决Symfony ApiPlatform GraphQl 突变在发送 IRI 数组时失败
在 Symfony 5 + ApiPlatform v2.6 中使用 GraphQl 突变与 Angular 11 客户端 (Apollo) 进行翻译,我正在展示以下工作流程:
执行第 1 步时一切正常,使用 Promise.all() 获取 IRI 并将其作为数组传递(这是 GraphQl 突变请求的参数)。
这是接收一些必需参数和 IRI 数组(之前创建的翻译)的突变。请注意,没有在格式、拼写错误或参数名称/类型方面引发错误或异常。
mutation createRecipesteps($cookOrder: Int!,$translations: [String]) {
createRecipesteps(input: {cookOrder: $cookOrder,translations: $translations}) {
recipesteps {
id
}
}
}
// set of variables used
{
"translations": [
"/api/recipe_steps_translations/1863","/api/recipe_steps_translations/1864","/api/recipe_steps_translations/1865"
],"cookOrder": 0
}
发送突变后,我收到以下异常:
{
"errors": [
{
"debugMessage": "The type of the key \"0\" must be \"string\",\"integer\" given.","message": "Internal server error","extensions": {
"category": "internal"
},"locations": [
{
"line": 2,"column": 3
}
],"path": [
"createRecipesteps"
],"trace": [
{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\Serializer\\AbstractItemnormalizer.PHP","line": 737,"call": "ApiPlatform\\Core\\Serializer\\AbstractItemnormalizer::denormalizeCollection('translations',instance of ApiPlatform\\Core\\Metadata\\Property\\PropertyMetadata,instance of Symfony\\Component\\PropertyInfo\\Type,'App\\Entity\\RecipestepsTranslation',array(3),'graphql',array(8))"
},{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\Serializer\\AbstractItemnormalizer.PHP","line": 403,"call": "ApiPlatform\\Core\\Serializer\\AbstractItemnormalizer::createAttributeValue('translations',{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\GraphQl\\Serializer\\Itemnormalizer.PHP","line": 128,"call": "ApiPlatform\\Core\\Serializer\\AbstractItemnormalizer::setAttributeValue(instance of App\\Entity\\Recipesteps,'translations',{
"file": "W:\\projects\\apps\\sample-api\\vendor\\symfony\\serializer\\normalizer\\AbstractObjectnormalizer.PHP","line": 336,"call": "ApiPlatform\\Core\\GraphQl\\Serializer\\Itemnormalizer::setAttributeValue(instance of App\\Entity\\Recipesteps,"line": 250,"call": "Symfony\\Component\\Serializer\\normalizer\\AbstractObjectnormalizer::denormalize(array(2),'App\\Entity\\Recipesteps',{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\Serializer\\Itemnormalizer.PHP","line": 70,"call": "ApiPlatform\\Core\\Serializer\\AbstractItemnormalizer::denormalize(array(2),array(7))"
},{
"file": "W:\\projects\\apps\\sample-api\\vendor\\symfony\\serializer\\Serializer.PHP","line": 208,"call": "ApiPlatform\\Core\\Serializer\\Itemnormalizer::denormalize(array(2),array(6))"
},{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\GraphQl\\Resolver\\Stage\\DeserializeStage.PHP","line": 57,"call": "Symfony\\Component\\Serializer\\Serializer::denormalize(array(2),{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\GraphQl\\Resolver\\Factory\\ItemmutationResolverFactory.PHP","line": 98,"call": "ApiPlatform\\Core\\GraphQl\\Resolver\\Stage\\DeserializeStage::__invoke(null,'create',{
"file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-PHP\\src\\Executor\\ReferenceExecutor.PHP","line": 624,"call": "ApiPlatform\\Core\\GraphQl\\Resolver\\Factory\\ItemmutationResolverFactory::ApiPlatform\\Core\\GraphQl\\Resolver\\Factory\\{closure}(null,array(1),null,instance of GraphQL\\Type\\DeFinition\\ResolveInfo)"
},"line": 550,"call": "GraphQL\\Executor\\ReferenceExecutor::resolveFieldValueOrError(instance of GraphQL\\Type\\DeFinition\\FieldDeFinition,instance of GraphQL\\Language\\AST\\FieldNode,instance of Closure,"line": 474,"call": "GraphQL\\Executor\\ReferenceExecutor::resolveField(GraphQLType: Mutation,instance of ArrayObject(1),array(1))"
},"line": 858,"call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0),'createRecipesteps')"
},{
"call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0),"line": 860,"function": "array_reduce(array(1),array(0))"
},"line": 490,"call": "GraphQL\\Executor\\ReferenceExecutor::promiseReduce(array(1),"line": 263,"call": "GraphQL\\Executor\\ReferenceExecutor::executeFieldsSerially(GraphQLType: Mutation,array(0),instance of ArrayObject(1))"
},"line": 215,"call": "GraphQL\\Executor\\ReferenceExecutor::executeOperation(instance of GraphQL\\Language\\AST\\OperationDeFinitionNode,null)"
},{
"file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-PHP\\src\\Executor\\Executor.PHP","line": 156,"call": "GraphQL\\Executor\\ReferenceExecutor::doExecute()"
},{
"file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-PHP\\src\\GraphQL.PHP","line": 162,"call": "GraphQL\\Executor\\Executor::promisetoExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter,instance of GraphQL\\Type\\Schema,instance of GraphQL\\Language\\AST\\DocumentNode,array(2),'createRecipesteps',"line": 94,"call": "GraphQL\\GraphQL::promisetoExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter,'mutation createRecipesteps($cookOrder: Int!,$translations: [String]) {\n createRecipesteps(input: {cookOrder: $cookOrder,translations: $translations}) {\n recipesteps {\n id\n }\n }\n}\n',{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\GraphQl\\Executor.PHP","line": 34,"call": "GraphQL\\GraphQL::executeQuery(instance of GraphQL\\Type\\Schema,{
"file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\GraphQl\\Action\\EntrypointAction.PHP","line": 86,"call": "ApiPlatform\\Core\\GraphQl\\Executor::executeQuery(instance of GraphQL\\Type\\Schema,{
"file": "W:\\projects\\apps\\sample-api\\vendor\\symfony\\http-kernel\\HttpKernel.PHP","line": 157,"call": "ApiPlatform\\Core\\GraphQl\\Action\\EntrypointAction::__invoke(instance of Symfony\\Component\\HttpFoundation\\Request)"
},"line": 79,"call": "Symfony\\Component\\HttpKernel\\HttpKernel::handleRaw(instance of Symfony\\Component\\HttpFoundation\\Request,1)"
},{
"file": "W:\\projects\\apps\\sample-api\\vendor\\symfony\\http-kernel\\Kernel.PHP","line": 195,"call": "Symfony\\Component\\HttpKernel\\HttpKernel::handle(instance of Symfony\\Component\\HttpFoundation\\Request,1,true)"
},{
"file": "W:\\projects\\apps\\sample-api\\public\\index.PHP","line": 20,"call": "Symfony\\Component\\HttpKernel\\Kernel::handle(instance of Symfony\\Component\\HttpFoundation\\Request)"
}
]
}
],"data": {
"createRecipesteps": null
}
}
正如我所建议的,我试图用一个对象更改语言环境表示(es|en)的键,但我得到了同样的异常。
如果我按要求传递字符串数组,为什么会出现此错误。 提前致谢!
解决方法
我只是从你处理翻译和错误的事实中猜测,语言环境字符串应该是数组键,你的例子中的第一个键是 int 0
,它会抛出错误。
所以在 php 中,它可能会期待这样的 associative 字符串数组:
[
"en" => "/api/recipe_steps_translations/1863","es" => "/api/recipe_steps_translations/1864","fr" => "/api/recipe_steps_translations/1865"
]
在 js 中定义该数据如下所示:
let translations = {
en: "/api/recipe_steps_translations/1863",es: "/api/recipe_steps_translations/1864",fr: "/api/recipe_steps_translations/1865"
}
JSON 表示将是:
JSON.stringify(translations)
// or
"{\"en\":\"/api/recipe_steps_translations/1863\",\"es\":\"/api/recipe_steps_translations/1864\",\"fr\":\"/api/recipe_steps_translations/1865\"}"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。