11

I am doing my queries exactly as in the documentation (https://aws-amplify.github.io/docs/js/api#amplify-graphql-client) but I keep receiving the following error:

TypeError: Must provide Source. Received: undefined

The query is as follows:

export const listUsers = `query ListUsers(
  $filter: ModelUserFilterInput
  $limit: Int
  $nextToken: String
) {
  listUsers(filter: $filter, limit: $limit, nextToken: $nextToken) {
    items {
      id
      firstName
      prefix
      lastName
      phone
      cigarettesDay
      enableNotifications
      age
      coach {
        id
        name
      }
      stopDate {
        nextToken
      }
      notifications {
        nextToken
      }
    }
    nextToken
  }
}
`;

Using API.graphql I call the query as follows:

  async componentDidMount() {
    try {
      const result = await API.graphql(graphqlOperation(listUsers));
      console.log(result);
    } catch (err) {
      console.log(err);
    }
  }

According to this issue https://github.com/graphql/graphql-js/issues/1038 I need to add a source, but I cannot find any example or documentation on how to do that. Any help is much appreciated!

Edit

It worked when I did it as follows:

 async componentDidMount() {
   try {
     const result = await API.graphql(graphqlOperation(queries.listUsers));
     console.log(result);
   } catch (err) {
     console.log(err);
   }
 }

And imported it as import * as queries from '../../../graphql/queries';

Why?

Solution

It was because I imported the query as

import listUsers from '../../../graphql/queries';

Instead of

import { listUsers } from '../../../graphql/queries';
Femke
  • 165
  • 3
  • 10
  • 2
    Indeed, you need to add a data source and a resolver to your API definition, otherwise AppSync will have no idea where to fetch the data from. https://docs.aws.amazon.com/appsync/latest/devguide/attaching-a-data-source.html I would suggest to test the API is working correctly in the AppSync console before to start coding against it. – Sébastien Stormacq Jan 31 '19 at 10:22
  • 2
    @SébastienStormacq thank you for your quick reply. I am new to aws amplify so still learning! I have used amplify add api and then amplify push to push my schema into the cloud. It then auto created the queries, mutations etc for me. It also created a resolver (ListUserResolver) in cloudformation. I am not sure where to add the data source.. sorry if this might sound a bit stupid. – Femke Jan 31 '19 at 11:38
  • Did you try the Query in the AppSync console ? Does it work there ? – Sébastien Stormacq Jan 31 '19 at 12:19
  • Yes, I tried running it in the console and it returned the list of users. – Femke Jan 31 '19 at 13:33
  • 1
    I have found a way it worked (I have edited the question), but I don't understand why this made it work. – Femke Jan 31 '19 at 13:40
  • the "import * as queries" tells to import the object in the namespace "queries" so it is normal to use that name to prefix imported construct names. – Sébastien Stormacq Jan 31 '19 at 13:41
  • My question was why it worked when I needed to import all the queries instead of just one, but I realised I forgot the curly brackets when importing the query. Thank you for your help!! – Femke Jan 31 '19 at 14:00
  • I had the same problem, although I imported as {listUsers}. I got it sorted by using the `await API.graphql(graphqlOperation(listUsers));`. It gave the same error when using `await API.graphql({query: listUsers})`. Not sure why this is happening. – Indika K Mar 22 '21 at 13:29
  • Please write your answer in the answer box, and not in the question. – Luke Mar 07 '23 at 11:11

0 Answers0