簡體   English   中英

發送 IRI 數組時,Symfony ApiPlatform GraphQl 突變失敗

[英]Symfony ApiPlatform GraphQl mutation fails when array of IRI is sent

在 Symfony 5 + ApiPlatform v2.6 中使用 GraphQl 突變和 Angular 11 客戶端(Apollo)進行翻譯

  1. 需要保存RecipeStepsTranslation 實體來獲取IRI
  2. 然后需要保存RecipeStepEntity

執行步驟 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', array(3), 'graphql', array(8))"
        },
        {
          "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', array(3), 'graphql', array(8))"
        },
        {
          "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, 'translations', array(3), 'graphql', array(8))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\Serializer\\AbstractItemNormalizer.php",
          "line": 250,
          "call": "Symfony\\Component\\Serializer\\Normalizer\\AbstractObjectNormalizer::denormalize(array(2), 'App\\Entity\\RecipeSteps', 'graphql', array(8))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\api-platform\\core\\src\\Serializer\\ItemNormalizer.php",
          "line": 70,
          "call": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer::denormalize(array(2), 'App\\Entity\\RecipeSteps', 'graphql', array(7))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\symfony\\serializer\\Serializer.php",
          "line": 208,
          "call": "ApiPlatform\\Core\\Serializer\\ItemNormalizer::denormalize(array(2), 'App\\Entity\\RecipeSteps', 'graphql', 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), 'App\\Entity\\RecipeSteps', 'graphql', array(6))"
        },
        {
          "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, 'App\\Entity\\RecipeSteps', 'create', array(6))"
        },
        {
          "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)"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "line": 550,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveFieldValueOrError(instance of GraphQL\\Type\\Definition\\FieldDefinition, instance of GraphQL\\Language\\AST\\FieldNode, instance of Closure, null, instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "line": 474,
          "call": "GraphQL\\Executor\\ReferenceExecutor::resolveField(GraphQLType: Mutation, null, instance of ArrayObject(1), array(1))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "line": 858,
          "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0), 'createRecipeSteps')"
        },
        {
          "call": "GraphQL\\Executor\\ReferenceExecutor::GraphQL\\Executor\\{closure}(array(0), 'createRecipeSteps')"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "line": 860,
          "function": "array_reduce(array(1), instance of Closure, array(0))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "line": 490,
          "call": "GraphQL\\Executor\\ReferenceExecutor::promiseReduce(array(1), instance of Closure, array(0))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "line": 263,
          "call": "GraphQL\\Executor\\ReferenceExecutor::executeFieldsSerially(GraphQLType: Mutation, null, array(0), instance of ArrayObject(1))"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\Executor\\ReferenceExecutor.php",
          "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, null, null, array(2), 'createRecipeSteps', null)"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\webonyx\\graphql-php\\src\\GraphQL.php",
          "line": 94,
          "call": "GraphQL\\GraphQL::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of GraphQL\\Type\\Schema, 'mutation createRecipeSteps($cookOrder: Int!, $translations: [String]) {\n  createRecipeSteps(input: {cookOrder: $cookOrder, translations: $translations}) {\n    recipeSteps {\n      id\n    }\n  }\n}\n', null, null, array(2), 'createRecipeSteps', null, null)"
        },
        {
          "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, 'mutation createRecipeSteps($cookOrder: Int!, $translations: [String]) {\n  createRecipeSteps(input: {cookOrder: $cookOrder, translations: $translations}) {\n    recipeSteps {\n      id\n    }\n  }\n}\n', null, null, array(2), 'createRecipeSteps', null, null)"
        },
        {
          "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, 'mutation createRecipeSteps($cookOrder: Int!, $translations: [String]) {\n  createRecipeSteps(input: {cookOrder: $cookOrder, translations: $translations}) {\n    recipeSteps {\n      id\n    }\n  }\n}\n', null, null, array(2), 'createRecipeSteps')"
        },
        {
          "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)"
        },
        {
          "file": "W:\\projects\\apps\\sample-api\\vendor\\symfony\\http-kernel\\HttpKernel.php",
          "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
  }
}

正如建議的那樣,我嘗試使用 object 更改語言環境表示(es|en)的鍵,但我得到了相同的異常。

如果我按要求傳遞一個字符串數組,為什么會給我這個錯誤。 提前致謝!

我只是從您處理翻譯和錯誤的事實中猜測,語言環境字符串應該是數組鍵,您示例中的第一個鍵將是int 0 ,它會引發錯誤。

因此,在 php 中,它可能期望類似這樣的字符串關聯數組:

[
  "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\"}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM