簡體   English   中英

如何創建 object 數組以響應 graphql

[英]How to create object array in react for graphql

Graphql 架構:

type SDKConfig @model 
  @key(name: "byPublisher", fields: ["publisher_id", "id"]){
  id: ID!
  publisher_id: ID!
  facebook_app_id: String

  adjust_app_token: String
}

type GameConfig @model
  @auth(rules: [
    {allow: owner},
    {allow: groups, groupsField: "groups"}]){
  id: ID!
  game_name: String!
  bundle_identifier: String!

  sdkConfigs: [SDKConfig] @connection(keyName: "byPublisher", fields: ["id"])
  groups: [String]
}

突變:

export const createGameConfig = /* GraphQL */ `
  mutation CreateGameConfig(
    $input: CreateGameConfigInput!
    $condition: ModelGameConfigConditionInput
  ) {
    createGameConfig(input: $input, condition: $condition) {
      id
      game_name
      bundle_identifier
      sdkConfigs {
        items {
          id
          publisher_id
          facebook_app_id
          adjust_app_token
          createdAt
          updatedAt
        }
        nextToken
      }
      groups
      createdAt
      updatedAt
      owner
    }
  }
`;

反應 function:

    async function createGame() {
      try {
        const newgame = { 
            "game_name": "deneme",
            "bundle_identifier": "com.magiclab.deneme",
            sdkConfigs: [
                {   "publisher_id": 5,
                    "facebook_app_id": "fb12313",
                    "adjust_app_token": "adjusttoken123123",
                }
            ] 
        }
        await API.graphql(graphqlOperation(createGameConfig, {input: newgame}))
      } catch (err) {
        console.log('error creating game sdk config:', err)
      }
    }

錯誤信息:

“變量輸入包含未為輸入 object 類型‘CreateGameConfigInput’定義的字段名稱‘sdkConfigs’”

我想在 object 中創建一個對象數組。 如何修復 graphql 的輸入 object?

您應該運行兩種不同的突變,一種用於創建GameConfig ,另一種用於創建SDKConfig ,它將是這樣的

 async function createGame() { try { const newgame = { game_name: 'deneme', bundle_identifier: 'com.magiclab.deneme', }; const sdk = { publisher_id: null, facebook_app_id: 'fb12313', adjust_app_token: 'adjusttoken123123', }; const { data: { createGameConfig: { id: publisher_id }, }, } = await API.graphql( graphqlOperation(createGameConfig, { input: newgame }) ); sdk.publisher_id = publisher_id; await API.graphql(graphqlOperation(createSDKConfig, { input: sdk })); } catch (err) { console.log('error creating game sdk config:', err); } }

然后您將使用第一個突變返回的 id 作為第二個突變的輸入,此標識符將綁定這兩個條目,並且當您查詢任何 gameConfig 時,它將拉入一個數組,任何 SDKConfig 的 publisher_id 與 gameConfig 匹配。

您可以在官方文檔https://docs.amplify.aws/cli/graphql-transformer/directives#belongs-to的這一部分中擴展這些信息

暫無
暫無

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

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