简体   繁体   中英

Using the Amplify Graphql Client in Javascript to Query Data

On the API(GraphQL) - Getting Started documentation here , it says to query your data using the following:

import { API } from 'aws-amplify';
import * as queries from './graphql/queries';

// Simple query
const allTodos = await API.graphql({ query: queries.listTodos });
console.log(allTodos); // result: { "data": { "listTodos": { "items": [/* ..... */] } } }

However, when I try to apply their code to my javascript code it says that it does not recognize the word await . Online it says I can only use the await keyword inside of a async function. When I take the await keyword out, the promise from the query function does not get settled so it returns the promise first before the data.

I tried setting up an async function before, and posted a stackoverflow post about it. The solution got a little messy, and did not quite work for me. So, I am wondering what is the best way to go about querying data using Graphql? And how do I implement that?

await can be used only with in an async context, so ideally what you would have to do is the following

const allTodos = async () => {
   const todos = await API.graphql({ query: queries.listTodos }); 
   return todos
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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