Greg Belyea
Greg Belyea

Reputation: 878

apollo graphql schema react-admin beginner

Brand new to these technologies and am pretty pumped by what i see so far, i am having a rough time finding an example of something i am stuck on. The docs for react-admin suggest my schema for say Posts follows the allPosts and _allPostsMeta.. The allPosts part works no problem but naturally react-admin chokes to death on the _allPostsMeta because i am not sure how that fits into things.. Examples mostly just show what your schema should look like but don't go any further than that.. Is there a demo server app setup like this anywhere that one could look at? I have my resolvers setup using join monster presently and that all makes decent sense to me, so i guess my question is, could anyone tell me, or point me to what my resolver should look like to satisfy that _allPostsMeta query that returns ListMetadata.. i looked into a few things like resolver chaining thinking maybe i am supposed to just return the length of the Post[] from allPosts but maybe that is overcomplicating things.. Any help or explanations would be greatly appreciated...

Thanks

Upvotes: 3

Views: 939

Answers (2)

Zovitch
Zovitch

Reputation: 73

this is what I used with a Mongoose Model in the backend

async _allPostsMeta() {
  try {
    const countAll = await Post.countDocuments();
    return {
      count: countAll,
    };
  } catch (error) {
    throw new Error(err);
  }
},

So the full resolvers looks like this

Query: {
async Post(_, { id }) {
  return await Post.findById(id);
},

async allPosts() {
  try {
    const posts = await Post.find();
    return posts;
  } catch (err) {
    throw new Error(err);
  }
},

//query for _allPostMeta
async _allPostsMeta() {
  try {
    const countAll = await Post.countDocuments();
    return {
      count: countAll,
    };
  } catch (error) {
    throw new Error(err);
  }
},

},

Upvotes: 0

Chris Pook
Chris Pook

Reputation: 51

appreciate this is an old question and you've probably moved on but thought I'd put a response here as I hit a similar situation myself whilst trying to implement a React-Admin front-end with an Apollo-Server-Express GraphQL back-end.

Based on the React Admin example of ra-data-graphql-simple (https://github.com/marmelab/react-admin/tree/master/packages/ra-data-graphql-simple)

My resolver ended up looking as follows:

Query: {
    _allPostsMeta: (parent, args, { db }, info) => db.post.findAndCountAll()
}

Note findAndCountAll() not findAll() as I've seen in other examples. This was enough to get React Admin pagination working for me so far. The lack of documentation / examples for all of this does make me somewhat dubious that this software is not the way to go or has been surpassed by something else.

Upvotes: 0

Related Questions