【问题标题】:How to create object array in react for graphql如何为graphql创建对象数组
【发布时间】:2020-11-26 15:45:35
【问题描述】:

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

反应函数:

    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)
      }
    }

错误信息:

“变量输入包含一个字段名称'sdkConfigs',该字段名称未为输入对象类型'CreateGameConfigInput'定义”

我想在对象中创建一个对象数组。如何修复 graphql 的输入对象?

【问题讨论】:

  • @xadm 我试过了。但不工作。返回相同的错误
  • 检查 >>>输入类型
  • 如果未定义,则看起来不支持这种“嵌套突变”

标签: javascript reactjs graphql aws-amplify


【解决方案1】:

您应该运行两种不同的突变,一种用于创建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的这一部分中扩展这些信息

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多