npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

graphql-transform-scalars

v2.1.1

Published

Graphql response transformer for custom scalars

Downloads

16,523

Readme

graphql-transform-scalars

Transform the response of your graphql request with mapper functions for your custom scalar types.

License npm version

Installation

Install with yarn or npm:

yarn add graphql-transform-scalars

or

npm install graphql-transform-scalars

Usage with graphql-request

With graphql-codegen

import { GraphQLClient } from 'graphql-request';
import fs from 'fs';
import { CalendarDate } from 'calendar-date';
import { getSdkWrapper, TransformCustomScalars } from 'graphql-transform-scalars';
import { getSdk } from './generated/graphql';

// Custom Scalar definition
const customScalarDefinitions = [
    {
        name: 'CalendarDate',
        parseValue: (val: unknown) => new CalendarDate(val as string),
    },
    {
        name: 'DateTime',
        parseValue: (val: unknown) => new Date(val as string),
    },
];

// The base schema is needed to get the Information about the graphql types returned from your request.
const schema = fs.readFileSync('path/to/your/schema.graphql', 'utf8');
// The operations are needed to match field aliases used in operations to the types in the schema.
const operations = fs.readFileSync('path/to/your/operations.graphql', 'utf8');
const transformScalars = new TransformCustomScalars({
    transformDefinitions: customScalarDefinitions,
    schema,
    operations
});
const sdk = getSdk(new GraphQLClient('url'), getSdkWrapper(this.transformScalars));

You can directly pass your defined GraphQLScalarTypes to the TransformCustomScalars constructor. graphql-scalars has a lot of already pre-defined definitions you can use.

import { GraphQLError, GraphQLScalarType, Kind } from 'graphql';
import { CalendarDate } from 'calendar-date';

export const GraphQLCalendarDate: GraphQLScalarType = new GraphQLScalarType({
    name: 'CalendarDate',

    description:
    'A field representing a date without time information according to ISO 8601. E.g. "2020-01-01".',
    
    serialize(value: unknown) {
        if (value instanceof CalendarDate) {
            return value.toString();
        }
        if (typeof value === 'string') {
            try {
                const calendarDate = new CalendarDate(value);
                return calendarDate.toString();
            } catch (e) {
                throw new TypeError(
                    `Value of type string does not represent a valid calendar date: ${value}`,
                );
            }
        }
        throw new TypeError(`Value is not an instance of CalendarDate and not a string: ${value}`);
    },
    
    parseValue(value: unknown) {
        if (value instanceof CalendarDate) {
           return value;
        }
        if (typeof value === 'string') {
            try {
                return new CalendarDate(value);
            } catch (e) {
                throw new TypeError(
                    `Value of type string does not represent a valid calendar date: ${value}`,
                );
            }
        }
        throw new TypeError(`Value is not an instance of CalendarDate and not a string: ${value}`);
    },
    
    parseLiteral(ast) {
        if (ast.kind !== Kind.STRING) {
            throw new GraphQLError(`Can only validate strings as CalendarDates but got a: ${ast.kind}`);
        }
        try {
            return new CalendarDate(ast.value);
        } catch (e) {
            throw new TypeError(
                `Value of type string does not represent a valid calendar date: ${ast.kind}`,
            );
        }
    },
});