React Native、GraphQL、Apollo - 如何创建批量插入突变
React Native, GraphQL, Apollo - how to create batch insert mutation
我需要在本机反应中创建一个批量插入突变调用这是我的代码。你能解决我的问题吗?我不知道我哪里做错了。而 onHandleSubmit 数据未插入 table。
仅在提交时将对象数组传递给批量变更调用函数。
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,
}
}`;
客户端突变似乎有问题。试试这个。
export const CreateStudentTestTrack = gql`
mutation abc ( // could be anyname
$studentTest: [CreateStudentTestTrackInput] // Type of input mentioned in graphql definition
) {
batchcreateStudentTestTrack(studentTest: $studentTest) {
id,
userId,
testId,
itemId,
attempt,
}
}`;
如果不需要更新 ui 缓存并且您的突变调用仅用于插入,也请尝试删除 graphql 参数中的乐观响应。我还根据我的解决方案更新了 mutate 对象。也请看看。
graphql(CreateStudentTestTrack, {
props: props => ({
ResponseStudentTestTrack: TestResponse => props.mutate({
variables: {
studentTest: TestResponse
},
}),
}),
}),
同时检查你的解构代码。
const { ResponseStudentTestTrack } = this.props; // don't set default value as array for a function prop.
试试这个代码,你会遇到下面评论的问题。
我需要在本机反应中创建一个批量插入突变调用这是我的代码。你能解决我的问题吗?我不知道我哪里做错了。而 onHandleSubmit 数据未插入 table。
仅在提交时将对象数组传递给批量变更调用函数。
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,
}
}`;
客户端突变似乎有问题。试试这个。
export const CreateStudentTestTrack = gql`
mutation abc ( // could be anyname
$studentTest: [CreateStudentTestTrackInput] // Type of input mentioned in graphql definition
) {
batchcreateStudentTestTrack(studentTest: $studentTest) {
id,
userId,
testId,
itemId,
attempt,
}
}`;
如果不需要更新 ui 缓存并且您的突变调用仅用于插入,也请尝试删除 graphql 参数中的乐观响应。我还根据我的解决方案更新了 mutate 对象。也请看看。
graphql(CreateStudentTestTrack, {
props: props => ({
ResponseStudentTestTrack: TestResponse => props.mutate({
variables: {
studentTest: TestResponse
},
}),
}),
}),
同时检查你的解构代码。
const { ResponseStudentTestTrack } = this.props; // don't set default value as array for a function prop.
试试这个代码,你会遇到下面评论的问题。