阿波罗反应钩阵列对象突变

时间:2020-05-31 09:10:54

标签: graphql apollo react-apollo apollo-client

所以我正在使用MongoDB,Apollo,React Native(Expo)和Node进行MARN堆栈

我一直在努力弄清楚如何上传对象数组。即包含一系列镜头的帖子

在阿波罗游乐场,一切正常:

mutation createPost {
  createPost(
    input: {
      shots: [
        {
          title: "Test test test"
          content: "Test test test"
          image: "https://source.unsplash.com/random/768x768"
        }
        {
          title: "Best best best"
          content: "Test test test"
          image: "https://source.unsplash.com/random/768x768"
        }
      ]
    }
  ) {
    id
    shots {
      id
    }
  }
}

这是我的服务器架构:

  type Post {
    id: ID!
    shots: [Shot]!
  }

  type Shot {
    id: ID!
    title: String
    content: String
    image: String
  }

  input CreatePostInput {
    shots: [ShotInput]!
  }

  input ShotInput {
    title: String!
    content: String!
    image: String!
  }

现在这是我的反应突变,这是我卡住的部分。因为它正在生成错误,所以我不知道如何解决。 如果我将$ shots替换为静态对象数组,它将起作用!我是否需要使用一些精美的@relation标签或其他内容?

const CREATE_POST = gql`
  mutation createPost($shots: [ShotInput]) {
    createPost(input: { shots: $shots }) {
      id
      shots {
        id
      }
    }
  }
`;

这是我触发错误的方式:

<Button
  title="Button"
  onPress={() => {
    createPost({
      variables: { shots: [{ title: 'test', content: 'test', image: 'test' }] },
    });
  }}
/>

这是我无法撼动的错误

[GraphQL error]: Message: Variable "$shots" of type "[ShotInput]" used in position expecting type "[ShotInput]!"., Location: [object Object],[object Object], Path: undefined

不管有多少小障碍,我都必须说阿波罗就是蜜蜂的膝盖!绝对很棒!!

1 个答案:

答案 0 :(得分:1)

我知道了。我一直都这么近!!

我所缺少的只是一个惊叹号“!”在createPost()

const CREATE_POST = gql`
  mutation createPost($shots: [ShotInput!]! <===== Right here) {
    createPost(input: { shots: $shots }) {
      id
      shots {
        id
      }
    }
  }
`;

好痛!这么多变量在起作用。吸取教训!

相关问题