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-data-transform

v1.0.6

Published

Transform data objects based on GraphQL schema

Downloads

30

Readme

GraphQL data transform

Transform data objects based on GraphQL schema.

Data retrieved from GraphQL queries sometimes needs some manipulation to make it suitable in different contexts. Some of these are:

  • Serialised scalar types (such as dates)
  • Populating an object for a form
  • Transformation for GraphQL mutation variables

The graphqlDataTransform function allows using an existing GraphQL Schema to perform the transforms to and from the different contexts.

Usage

Define your app's data transformations

// dataTransform.js
import graphqlDataTransform from 'graphql-data-transform'
import schema from './schema.graphql'

const transforms = {
  // Our schema defines a DateTime scalar type. This is unserialized from query
  // responses to a Date object, and serialized for mutation inputs from a Date object
  DateTime: {
    data: value => new Date(value),
    input: value => value.toISOString()
  },
  // Numbers in our form components are using <input type="string"> and expect a
  // string type as value and produce a string in their `onChange` event.
  // This transform rule ensures integers are transformed for forms
  Int: {
    format: value => String(value),
    parse: value => parseInt(value, 10)
  },
  // The Money scalar type is displayed as a dollar value
  Money: {
    props: value => `$${(value / 100).toFixed(2)}`
  }
}

// we accept data either from GraphQL query responses (`data()`) or from "form"
// components (`parse()`)
const setMethods = ['data', 'parse']

// we send data to GraphQL mutations (`input()`), to "form" components (`format()`)
// or "view" components (`props()`)
const getMethods = ['input', 'format', 'props']

// export the schema type based transformations
export default graphqlDataTransform(schema, transforms, setMethods, getMethods)

Convert data using the transformation methods

import types from './dataTransform'

const data = {
  __typename: "Post",
  id: "Post:1",
  timestamp: "2018-12-03T20:54:58.364Z"
}
// `types.Post.data()` is a "set method". It accepts an object and transforms
// it's attributes based on the `Post` type in the GraphQL schema. It returns
// an object that contains "get methods".
// The `.props()` method returns the transformed object suitable for passing
// to components
const props = types.Post.data(data).props()
// -> props: { id: "Post:1", timestamp: <Date> }

const data = { price: 1000 }
// `types.Product.data()` transforms an object based on the `Product` type.
// The `.format()` method returns the object transformed to be suitable for
// passing to form components.
const formData = types.Product.data(data).format()
// -> formData: { price: "1000" }

const formData = { price: "999" }
// `types.ProductInput.parse()` is another "set method". It accepts an object
// that has come from a form component and prepares it for the `ProductInput`
// type. The `.input()` method is used to serialize the object's properties
// to be ready for a GraphQL mutation
const attributes = types.ProductInput.parse(formData).input()
// -> attributes: { price: 999 }

// It's possible to transform values to specialized formats. This example shows
// how a 'Money' type might be used to convert price values (returned as
// integers from the GraphQL query) to a human-friendly format to pass to
// components
const data = { price: 1000 }
const props = types.Product.data(data).props()
// -> props: { price: "$10.00" }

Possible future enhancements

  • Support aliases. The transformation functions do not have access to the GraphQL query so have no knowledge of aliases. The result is the data structure to be transformed does not match the schema. Devising a way to feed the query to the transformation functions is currently beyond the scope of this module.