# How can we optimize DB query based on only argument passed in the Apollo GraphQL Query

**URL:** <https://community.apollographql.com/t/how-can-we-optimize-db-query-based-on-only-argument-passed-in-the-apollo-graphql-query/5611>\
**Category:** Schema Design\
**Tags:** server, federation, schema-design\
**Created:** [January 31, 2023, 6:24am UTC](https://community.apollographql.com/t/how-can-we-optimize-db-query-based-on-only-argument-passed-in-the-apollo-graphql-query/5611 "2023-01-31T06:24:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay\_Bhatt](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/jay_bhatt/32/3798_2.png) [@Jay\_Bhatt](https://community.apollographql.com/u/Jay_Bhatt)\
**Post date:** [January 31, 2023, 6:24am UTC](https://community.apollographql.com/t/how-can-we-optimize-db-query-based-on-only-argument-passed-in-the-apollo-graphql-query/5611/1 "2023-01-31T06:24:39Z")

</div>

On the server side, for every query if we are using findMany then by default it is fetching all the parameters/columns from the table, can I modify it somehow to pass only the sent parameters in the query and optimize it?

**Model:**

```auto
model Category {
  id Int @id @default(autoincrement())
  name String @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  iconURL String?
}

```

**Query:**

```auto
query getCategory {
  getCategory {
    id
    name
  }
}

```

**Server Side:**

```auto
prisma:query SELECT "public"."Category"."id", "public"."Category"."name", "public"."Category"."createdAt", "public"."Category"."updatedAt", "public"."Category"."iconURL" FROM "public"."Category" WHERE 1=1 ORDER BY "public"."Category"."name" ASC OFFSET $1

```

---

<div class="post-metadata">

**Author:** ![watson](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/watson/32/2053_2.png) [@watson](https://community.apollographql.com/u/watson)\
**Post date:** [January 31, 2023, 11:13pm UTC](https://community.apollographql.com/t/how-can-we-optimize-db-query-based-on-only-argument-passed-in-the-apollo-graphql-query/5611/2 "2023-01-31T23:13:44Z")

</div>

Hey @Jay_Bhatt, it looks like you’re using Prisma and if you are defining your schema based on Prisma, they might be better suited to help answer your question.

If you are defining the schema in another way and having Prisma be a backing datasource, it would look like you are only creating a resolver for `Query.getCategory` which is returning that prisma query. You would need to define further resolvers and possibly restructure your schema to make that work.

If you had a schema like this:

```auto
type Query {
  getCategory: [Category]
}
type Category {
  id: Int
  name: String
  ...
}

```

You could define your resolvers like this:

```auto
const resolvers = {
  Query: {
    getCategory(parent,args,context) {
      return context.prisma.query('query SELECT "public"."Category"."id" FROM "public"."Category" WHERE 1=1 ORDER BY "public"."Category"."name" ASC OFFSET $1'
    }
  },
  Category: {
    name({id}, args, context) {
      return context.prisma.query(`query SELECT "public"."Category"."id", "public"."Category"."name", "public"."Category"."createdAt", "public"."Category"."updatedAt", "public"."Category"."iconURL" FROM "public"."Category" WHERE "public"."Category"."id"=${id} ORDER BY "public"."Category"."name" ASC OFFSET $1`);
    },
    ...
  }
}

```

The resolvers are pseudocode, meaning they definitely won’t work. I’m just giving an example of creating a resolver for the type’s fields. Do you want to actually have multiple prisma queries like that? Probably not but you probably want to rearrange things around to get the optimization you like. Teams commonly do this by using `@deprecated` on the old field where the new field implements the design optimizations. You probably always want to sprinkle in [dataloader](https://github.com/graphql/dataloader) for this stuff.

If you want to talk about it more, join us in our new [Discord server](https://discord.gg/graphos) and feel free to ping me (@watson).
