React Native,GraphQL,Apollo-如何创建批处理插入突变

时间:2018-11-12 07:15:07

标签: react-native graphql aws-appsync appsync-apollo-client

我需要在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 Sh​​ema:

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,
  }
}`;

1 个答案:

答案 0 :(得分:0)

客户端突变似乎出了点问题。试试这个。

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.

尝试使用此代码,您将在下面遇到问题。