himerus
himerus

Reputation: 51

Prisma orderBy in a resolver/query outside of Playground

Okay, so I've been working with Prisma for a couple weeks now, and have made some great progress and I love the setup/ease of use of getting a lot of advanced features implemented.

I'm at the point I'm trying to implement sorting on table fields.

I have the options to (among other fields) sort by term_ASC and term_DESC.

Assume the following query definition: MyConnection(filter: String, order: MyOrderByInput, limit: Int, offset: Int): MyConnection!

If I run the following code in the GraphQL Playground, it works fine:

query myPaginatedResults {
    myConnection(filter: "lorem", limit: 25, offset: 0, order: term_ASC) {
        aggregate {
            count
        }
        edges {
            node {
                id
                term
            }
        }
    }
}

Main point/question... The usage of term_ASC/DESC works in playground, but how do I pass that in my JS?

If I wrap it in quotes "term_ASC", errors ensue, and if it's not wrapped, then it (resolver/api) throw errors about an obviously undefined variable.

const myConnection = (parent, args, context, info) => {
  const where = args.filter
  ? {
    OR: [
      { term_contains: args.filter },
      { type_contains: args.filter },
      { id_in: args.filter },
    ],
  } : {}

  const order = args.order;
  // const order = `title_ASC`;
  const skip = args.offset ? args.offset : 0;
  const limit = args.limit ? args.limit : 50;

  // console.log(context.db.query);
  const results = context.db.query.myDbConnection({
    where,
    orderBy: order,
    first: limit,
    skip: skip,
  }, info);

  return results;
}

So the portion where I'm trying to test how to pass this orderBy variable in through a variable (hard coded, commented out in this sample) or before passing into the resolver via my args.order, I can't figure out how that should be passed.

Upvotes: 3

Views: 2321

Answers (1)

himerus
himerus

Reputation: 51

Okay, so per usual, once my question was written down/voiced, I came up with the answer.

The problem was not that I wasn't able to wrap the enum values from MyOrderByInput in quotes in the resolver or assign to a variable pre Query, the problem was that I was trying to test with title_ASC/title_DESC which didn't exist on my Type.

-1 for me.

Lesson here: Ensure you are using an ordering method that actually DOES exist on your Prisma schema.

Upvotes: 2

Related Questions