Query all users from meteor


#1

Hi All, I tries to query all the users from meteor using apollo.

const GET_USERS = gql`
  query {
    users {
      _id,
      username,
      emails,
    }
  }
`;

const UserListWithData = graphql(GET_USERS, {
  props: ({data: {users, loading}}) => {
    console.log(users);
    return {
      loading,
      users,
    }
  }
})(UserList);

I can query users through the graphiql tool, but users is undefined when I console log users at the client side. Why is that? Do I need to do anything with Meteor account, such as publishing all users first? but I thought apollo does not depend on the Meteor pub-sub.

Thanks!


#2

How are you console.log-ing it?


#3

I console log in the props function. Please see my edited code. Thank you very much for your prompt response.


#4

the resolver

Query: {
    async users(root, args, context) {
      return await Meteor.users.find().fetch();
    },
}


#5

It should log twice - once as undefined with loading = true, and once with the data. Is the request coming back from the server? You can look in your Chrome Network tool to inspect the requests sent to the GraphQL server.


#6

Thank you @sashko, I found the bug. It needs to specify the subfield of the email field to get the query response back.


#7

How did you fix the issue? Can you elaborate for others’ benefit?