在响应本机应用程序中使用AWS Amplify在GraphQL突变中遇到错误

时间:2018-10-13 13:27:31

标签: javascript react-native graphql aws-amplify

尝试执行graphql突变,该突变应采用自定义类型。在App Sync模式中,我定义了以下自定义类型:

架构:

  input CreateConversationInput {
        user: UserInput
        doctor: DoctorInput
        questionsAndAnsers: [ConversationQAInput]
        pet: UpdatePetInput
    }

反应本机代码:

  const createConversationInput = {

        user: {
            username: "deep",
            userType: "Patient",
            fullName: "Deep A"
        },
        doctor: {
            name: "Raman",
            speciality: "dog",
            doctorId: "0bd9855e-a3f2-4616-8132-aed490973bf7"
        },
        questionsAndAnswers: [{ question: "Question 1", answer: "Answer 1" }, { question: "Question 2", answer: "Answer 2" }],
        pet: { username: "deep39303903", petId: "238280340932", category: "Canine" }


    }

    API.graphql(graphqlOperation(CreateConversation, createConversationInput)).then(response => {

        console.log(response);

    }).catch(err => {
        console.log(err);
    });

我已经定义了这样的突变:

export const CreateConversation = `mutation CreateConversation( $user: Object, 
    $doctor: Object, $questionsAndAnswers: Object, $pet: Object ) {

        createConversation(

             input : {

                user: $user
                doctor: $doctor
                questionsAndAnswers: $questionsAndAnswers
                pet: $pet
            }
        ){
    username
    createdAt

  }

  }`;

该突变在AWS GraphQL控制台中正常运行。但是,在react native应用程序中,出现错误。

错误:

  

“类型为UnknownType的验证错误:未知类型为Object”。

我相信该错误是因为我将类型定义为“突变型”对象,而不是在AWS Schema中定义的实际类型。如果出现问题,如何在React Native代码中定义自定义类型?

1 个答案:

答案 0 :(得分:3)

您只需要更改定义变量的方式即可:

mutation CreateConversation(
  $user: UserInput,
  $doctor: DoctorInput,
  $questionsAndAnswers: [ConversationQAInput],
  $pet: UpdatePetInput
) {
  createConversation(input: {
    user: $user
    doctor: $doctor
    questionsAndAnswers: $questionsAndAnswers
    pet: $pet
  }) {
    username
    createdAt
  }
}

在GraphQL中使用变量时,请为该变量指定输入类型(或标量)。然后将其与使用变量的任何参数所期望的类型进行比较。这里的输入类型是架构中的一种类型,与JS没有任何关系,因此您的代码中不需要任何特殊的内容。