1
votes

Trying to execute a graphql mutation, where the mutation should take custom type. In the App Sync schema, I have defined these custom Types:

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. However, in the react native app, I get error.

Error:

"Validation error of type UnknownType: Unknown type Object".

I believe the error is because I defined the type as Object in mutations instead of the actual type defined in the AWS Schema. How can I define custom types in the React Native code if this is the issue?

1

1 Answers

3
votes

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. 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.