Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving the feed query #56

Open
Drago96 opened this issue Feb 24, 2019 · 0 comments
Open

Improving the feed query #56

Drago96 opened this issue Feb 24, 2019 · 0 comments

Comments

@Drago96
Copy link

Drago96 commented Feb 24, 2019

First of all, thanks for the great course. As someone with zero experience in GraphQL, the course helped me get a deeper understanding of how things work.

I feel like the feed query, defined here https://github.com/howtographql/graphql-js/blob/master/src/resolvers/Query.js#L1 , can be improved upon. Currently, the server makes a database request for both links and count, regardless of whether the client only asked for one or the other.

Modifying the code a bit, by rewriting the separate database calls into functions, seems to have fixed the issue.

const feed = (root, args, context, info) => {
  const createWhereClause = () =>
    args.filter
      ? {
          OR: [
            { description_contains: args.filter },
            { url_contains: args.filter }
          ]
        }
      : {};

  const links = () =>
    context.prisma.links({
      where: createWhereClause(),
      skip: args.skip,
      first: args.first,
      orderBy: args.orderBy
    });

  const count = () =>
    context.prisma
      .linksConnection({
        where: createWhereClause()
      })
      .aggregate()
      .count();

  return {
    links,
    count
  };
};

module.exports = {
  feed
};

I am not sure if this is the best approach to the problem, however this has reduced the redundant database calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant