问题描述
我被困试图弄清楚如何上传一个对象数组. i.E有一系列镜头的帖子
它在Apollo Playground中都有正常工作:
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! }
现在这是我的反应变异,我陷入了困境的部分.因为它产生错误,我不知道如何解决它. 如果我用静态对象数组替换$镜头,它可以工作!我需要使用一些花哨的@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
无论这个小的障碍如何,我得说阿波罗是蜜蜂的膝盖!绝对令人敬畏!!!
推荐答案
我认为它.我这么近一段时间!
我缺少的只是一个感叹号"!"在createpost()
const CREATE_POST = gql` mutation createPost($shots: [ShotInput!]! <===== Right here) { createPost(input: { shots: $shots }) { id shots { id } } } `;
哎哟伤害!游戏中的许多变量.经验教训!!!
问题描述
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 } } }
And this is my server schema:
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! }
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' }] }, }); }} />
And this is the error I can't shake
[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!!!
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!!!