简体   繁体   English

如何创建 object 数组以响应 graphql

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

Graphql schema: 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]
}

Mutations:突变:

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
    }
  }
`;

React function:反应 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)
      }
    }

Error message:错误信息:

"The variables input contains a field name 'sdkConfigs' that is not defined for input object type 'CreateGameConfigInput' " “变量输入包含未为输入 object 类型‘CreateGameConfigInput’定义的字段名称‘sdkConfigs’”

I want to create an array of objects within the object.我想在 object 中创建一个对象数组。 How to fix input object for graphql?如何修复 graphql 的输入 object?

You should run two different mutations, one for creating the GameConfig and anorther one for create the SDKConfig it will be something like this您应该运行两种不同的突变,一种用于创建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); } }

then you will use the id return by the first mutation as an input for the second mutation, this identifier will bound these two entries and when you query any gameConfig it will pull in an array any SDKConfig that their publisher_id matches with the gameConfig.然后您将使用第一个突变返回的 id 作为第二个突变的输入,此标识符将绑定这两个条目,并且当您查询任何 gameConfig 时,它将拉入一个数组,任何 SDKConfig 的 publisher_id 与 gameConfig 匹配。

You could expand these information in this section of the official documentation https://docs.amplify.aws/cli/graphql-transformer/directives#belongs-to您可以在官方文档https://docs.amplify.aws/cli/graphql-transformer/directives#belongs-to的这一部分中扩展这些信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM