Apollo React 钩子数组对象突变 [英] Apollo react hooks array object mutation

查看:44
本文介绍了Apollo React 钩子数组对象突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用 MongoDB、Apollo、React Native (Expo) 和 Node 做一个 MARN 堆栈

So I am doing a MARN stack using MongoDB, Apollo, React Native (Expo) and Node

我一直在想如何上传对象数组.即带有一系列镜头的帖子

I am stuck trying to figure out how to upload an array of object. i.e A post with an array of shots

在 Apollo 游乐场中一切正常:

It is all working fine in the Apollo playground with this:

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 标签吗?

Now this is my react mutation, the part I am stuck on. Because it is generating an error and I have no idea how to fix it. If I replace $shots with a static array of objects, it works! Do I need to use some fancy @relation tag or something?

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

这是我触发错误的方式:

This is how I am triggering the error:

<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

不管这个小障碍,我得说阿波罗是蜜蜂的膝盖!绝对的赞!!!

Regardless of this little hurdle, I gotta say that Apollo is the bees knees! Absolute awesomeness!!!

推荐答案

我想通了.我一直很近!!!

I figured it out. I was so close the whole time!!!

我所缺少的只是一个感叹号!"在 createPost()

All I was missing was an exclamation "!" at createPost()

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

哎呀,好痛!这么多变数在起作用.吸取教训!!!

Ouch that hurt! So many variables at play. Lesson learned!!!

这篇关于Apollo React 钩子数组对象突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆