【发布时间】:2019-04-14 21:25:48
【问题描述】:
我需要在 react native 中创建一个批量插入突变调用这是我的代码。请你解决我的问题。我不知道我在哪里做错了。而 onHandleSubmit 数据未插入表中。
仅在提交时将对象数组传递给批量突变调用函数。
onHandleSubmit = () => {
const TestResponse = [
{
id:1,
userId:123,
testId:4321,
itemId:43545,
attempt:true,
},
{
id:2,
userId:123,
testId:4321,
itemId:43546,
attempt:false,
}
];
const { ResponseStudentTestTrack = [] } = this.props;
ResponseStudentTestTrack(TestResponse);
}
Appsync Shema:
type StudentTestTrack {
id: ID!
userId: ID!
testId: ID!
itemId: ID!
attempt: String!
}
type StudentTestTrackConnection {
items: [StudentTestTrack]
nextToken: String
}
input CreateStudentTestTrackInput {
id: ID!
userId: ID!
testId: ID!
itemId: ID!
attempt: String!
}
type Mutation {
batchcreateStudentTestTrack(studentTest: [CreateStudentTestTrackInput]!): [StudentTestTrack]
}
阿波罗呼唤:
graphql(CreateStudentTestTrack, {
props: props => ({
ResponseStudentTestTrack: TestResponse => props.mutate({
variables: TestResponse,
optimisticResponse: {
__typename: 'Mutation',
batchcreateStudentTestTrack: { ...TestResponse, __typename: 'CoursePatternStatus' },
},
}),
}),
}),
突变:
export const CreateStudentTestTrack = gql`
mutation batchcreateStudentTestTrack{
batchcreateStudentTestTrack(
studentTest:[TestResponse]
) {
id,
userId,
testId,
itemId,
attempt,
}
}`;
【问题讨论】:
-
你的
ResponseStudentTestTrack是什么类型的,来自props? -
它是一个对象数组,如下所示 const TestResponse = [ { id:1, userId:123, testId:4321, itemId:43545, attempt:true, }, { id:2, userId: 123,testId:4321,itemId:43546,尝试:假,}
-
那我不明白。你怎么称呼它:
ResponseStudentTestTrack(TestResponse);如果它是一个数组,你需要使用concat -
是的,我们可以通过 props 调用 ResponseStudentTestTrack。只有 concat 值通过 ResponseStudentTestTrack(TestResponse) 传递。
-
只是我想知道如何在这个突变调用中传递对象值数组,而不是它对我来说工作正常。 export const CreateStudentTestTrack = gql` 突变 batchcreateStudentTestTrack{ batchcreateStudentTestTrack( studentTest:[TestResponse] ) { id, userId, testId, itemId, attempt, } }`;
标签: react-native graphql aws-appsync appsync-apollo-client