简体   繁体   English

使用AWS Amplify在本机应用程序中获取GraphQL Mutation中的错误

[英]Getting Error in GraphQL Mutation using AWS Amplify in react native app

Trying to execute a graphql mutation, where the mutation should take custom type. 尝试执行graphql变异,其中变异应采用自定义类型。 In the App Sync schema, I have defined these custom Types: 在App Sync架构中,我定义了以下自定义类型:

Schema: 架构:

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

React Native Code: 反应本机代码:

  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);
    });

I have define mutation like this: 我已经定义了这样的变异:

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

  }

  }`;

The mutation is working properly from the AWS GraphQL console. 该变异在AWS GraphQL控制台中正常运行。 However, in the react native app, I get error. 但是,在本机应用程序中,我收到错误。

Error: 错误:

"Validation error of type UnknownType: Unknown type Object". “类型为UnknownType的验证错误:未知类型对象”。

I believe the error is because I defined the type as Object in mutations instead of the actual type defined in the AWS Schema. 我认为错误是因为我在突变中将类型定义为Object而不是AWS Schema中定义的实际类型。 How can I define custom types in the React Native code if this is the issue? 如果出现问题,如何在React Native代码中定义自定义类型?

You should just need to change the way you're defining your variables: 您应该只需要更改定义变量的方式:

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

When using variables in a GraphQL, you specify the input type (or scalar) for that variable. 在GraphQL中使用变量时,可以指定该变量的输入类型 (或标量)。 This is then compared against the type expected by whatever argument you use the variable for. 然后将其与您使用变量的任何参数所期望的类型进行比较。 The input type here refers to one of the types in your schema, and not anything related to JS, so you don't need to anything special in your code. 这里的输入类型是指模式中的一种类型,而不是与JS相关的任何类型,因此您不需要在代码中使用任何特殊内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM