0

I am new to the AWS world and have been working on an Amplify project for a few months. Project uses Amplify front-end with Appsync / GraphQL and dynamodb all set up via the console in Cloud9. Read access to the backend using the graphql queries works great but I simply cannot get any of the auto-generated mutations to work. All throw ""Request aborted" errors with ""Invalid URI format".

Here's a snippet of the error

Here's a simple example from my code:

import { createCategory} from './graphql/mutations';

//Here's the start of our Javascrpt App
const WriteTestPage = () => {

//define function to handle submitButton, which is used to create a new customer
async function handleSubmit ()  {
    try {
        //For Customers, the name is required
        console.log('Called our API - top');

        const categoryDetails = {
            description: 'My new category'
        }
        
        //Call GraphQL with correct parameters to create in backend 
        const returnedCategory = await API.graphql(graphqlOperation(createCategory, { input: categoryDetails}));
        
        
        console.log('Called our API');
    } catch (err) {
        console.log('error creating category', err);
    }
}

Here's the corresponding schema. I've opened up all access and have a user pool as well as an API key.

type Category 
  @model 
  @auth(
     rules: [
       { allow: groups, groups: ["superAdmin"]}  #allow the admin to crud the table
       { allow: private, operations: [read, update, create, delete]}   #allow the authenticated users to read data from the table
       { allow: public, operations: [read, update, create, delete]}                    #DEV only - allow the UNauthenticated users to read data from the table

   ]) 
  {
  id: ID!
  description: String!
    
  conectivity: [Conectivity] @connection(keyName: "byCategory", fields: ["id"])   #Every Category may have zero or more conectivities
  
}

Here's the network trace. Doesn't appear that the front-end is even issuing a POST to the backend. Network trace

First post on StackOverflow so appologies for any errors. Any help would be much appreciated as I'm beating my head against the wall here. I'm sure it's something simple that I'm overlooking.

Full code illustrating error:

 import './App.css';
    
    //context testing and dev
    import React, { useEffect, useState } from 'react';
    import Amplify, { AUTH, API, graphqlOperation } from 'aws-amplify';
    import { Link } from 'react-router-dom';
    
    //Queries and Mutations
    
    import { createCategory} from './graphql/mutations';
    import {AmplifyAuthenticator, AmplifySignOut } from '@aws-amplify/ui-react';
    
    
    //Here's the start of our Javascrpt App
    const WriteTestPage = () => {
        
        
      
        //define function to handle submitButton, which is used to create a new customer
        async function handleSubmit ()  {
            try {
                //For Customers, the name is required
                console.log('Called our API - top');
    
                const categoryDetails = {
                    description: 'My new catetory'
                }
                
                //Call GraphQL with correct parameters to create in backend 
                const returnedCategory = await API.graphql(graphqlOperation(createCategory, { input: categoryDetails}));
                
                
                console.log('Called our API');
            } catch (err) {
                console.log('error creating category', err);
            }
        }
      
      //Define HTML to return in order to create page
      
      return (
        <div className="App">
            <section>
       
            <AmplifyAuthenticator>
            
            <section>
                <h3 className="sectionHeader">Write Test</h3>
                
                  <center><form onSubmit={handleSubmit}>
                        <div >
                        <table className="tableStyle">
                        <thead>
                            <tr>
                                    <th className="tableHeaderStyle">Write Test</th>
                                    
                            </tr>
                        </thead>
                        <tbody>
                            <tr className="tableRowOddStyle">
                                
                            <td className="tableDataStyle">
                                <button className="btn" type="submit" className="buttonStyle">Add</button>
                            </td>
                                    </tr>
                            </tbody>
                            </table>
                        
                            </div>
                        </form></center>
                        
                            <p></p>
                        
                     
                 
            </section>
            
            </AmplifyAuthenticator>
                        
        </section>
        </div>
      );
    }


export default WriteTestPage;
Broom
  • 1
  • 2
  • how do you invoke `handleSubmit()` ? – sakhunzai Jul 09 '21 at 16:16
  • Thank you for responding! Added code above with the entire page for my simple write test. Using a form with a submit button to test a write to the DB. – Broom Jul 09 '21 at 17:44
  • I should add, this is running on a React framework. – Broom Jul 09 '21 at 17:49
  • I dont see any reason why your request should be aborted , make sure any React settings is not interfering with the request being posted e.g a page refresh /reload – sakhunzai Jul 09 '21 at 18:11
  • Thank you! I do notice that when I hit the submit button the page immediately fully reloads, which I though was odd. Could that be interfering with or interrupting the API call, thus leading to the "Aborted" error message? – Broom Jul 09 '21 at 18:15
  • yes exactly , you should not use submit action , since submit action will reload the page – sakhunzai Jul 09 '21 at 18:19
  • you need sth like this https://stackoverflow.com/questions/39809943/react-preventing-form-submission#39841238 – sakhunzai Jul 09 '21 at 18:21
  • OMG that worked! You totally solved it for me. Thank you!!!! I added this to the handler and now the Post flows through to the backend (no page refresh) and updates DynamoDB: e.preventDefault(); – Broom Jul 09 '21 at 18:56

0 Answers0