本文介绍了阿波罗反应钩阵列对象突变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用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

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

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!!!

这篇关于阿波罗反应钩阵列对象突变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:59