1.1.8 • Published 4 years ago

graphql-map-selections v1.1.8

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

graphql-map-selections

A simple package to map selections from graphql resolver info object.

To install:

> yarn add graphql-map-selections

or

> npm install graphql-map-selections

Basic Usage:

import { mapSelections } from "graphql-map-selections";

const resolverFn = (source, args, context, info) => {
    const select = mapSelections(info)
    ...
}

Example:

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,
    }
}

Usage with Prisma

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})
            ...
        }
    }
}
1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.1

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago