Apollo react hooks数组对象突变

问题描述 投票:0回答:2

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

[尽管有这么小的障碍,我得说阿波罗是蜜蜂的膝盖!绝对很棒!

graphql apollo react-apollo apollo-client
2个回答
0
投票

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

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

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

太疼了!这么多变量在起作用。吸取教训!


0
投票

类似于错误状态,变量的类型需要为[ShotInput]!,而不是[ShotInput]。具有可为空类型的变量的值可以为null或完全省略,因此它们不能与类型为非可为空(即必需)的参数一起使用。

有关变量验证的详细说明,请参见规范验证部分的this part

© www.soinside.com 2019 - 2024. All rights reserved.