graphql-map-selections
Version:
A package to map selections from graphql resolver info object
101 lines (81 loc) • 1.68 kB
Markdown
A simple package to map selections from graphql resolver info object.
```
> yarn add graphql-map-selections
```
or
```
> npm install graphql-map-selections
```
```
import { mapSelections } from "graphql-map-selections";
const resolverFn = (source, args, context, info) => {
const select = mapSelections(info)
...
}
```
Graphql query to fetch profile:
```
query MyProfile {
myProfile {
id
firstName
lastName
email
phoneNumber
address {
id
city
country
street
postalCode
}
}
}
```
Query resolver function:
```
import { mapSelections } from "graphql-map-selections";
const resolvers = {
Query: {
myProfile: (source, args, context, info) => {
const select = mapSelections(info)
...
}
}
}
```
In above resolver function, the mapSelectons(info) will return:
```
{
id: true,
firstName: true,
lastName: true,
email: true,
phoneNumber: true,
address {
id: true,
city: true,
country: true,
street: true,
postalCode: true,
}
}
```
```
import { mapSelections, toPrismaSelect } from "graphql-map-selections";
const resolvers = {
Query: {
myProfile: async (source, args, context, info) => {
const select = mapSelections(info)
const prismaSelect = toPrismaSelect(select)
await prismaClient.profile.findUnique({where: {id: args.id}, ...select})
...
}
}
}
```