Socket
Socket
Sign inDemoInstall

@coderich/graphql-shape

Package Overview
Dependencies
7
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @coderich/graphql-shape

[![Build Status](https://github.com/CoderichLLC/nodejs-graphql-shape/actions/workflows/publish.yml/badge.svg)](https://github.com/CoderichLLC/nodejs-graphql-shape/actions/workflows/publish.yml)


Version published
Maintainers
1
Install size
468 kB
Created

Readme

Source

GraphQLShape

Build Status

Shape the response of your GraphQL queries, declaratively!

This project explores the concept of Query & Transformation Collocation in GraphQL.

It includes JSONPath+ to help select and transform data to the exact shape required!


Usage

  1. Annotate a query with transformation rules
  2. Parse the query AST/String pre-request
  3. Transform the result post-response
const { parse } = require('@coderich/graphql-shape');

const { query, transform } = parse(annotatedQuery, [options]);
const data = await graphqlClient.request(query, args); // Your own client
const shaped = transform(data);

Annotations (directives)

Annotations can be defined on any field that requires transformation. By default, the directive name is shape and may be configured via options.name when calling parse()

annotationdescription.parse()
@shapeTransform an existing field in the GraphQL SchemaThe annotation is removed from the field
@_shapeDefine/Transform a non-existing field in the GraphQL SchemaThe field is removed from the query

Transformations (annotation arguments)

Transformations are performed via annotation arguments where each key:value pair maps to a transformation name:args function call:

  • Transformations are evaluated depth-first (inside-out, bottom-up) and from left-to-right
  • Each transformation assigns it's return value to the annotated field (mutating it)
  • Each transformation receives the current field value as it's first argument
Example
query {
  books @shape(self: "edges[*].node") {
    edges {
      node {
        isbn
        title
        author @shape(self: "name") {
          name
        }
        details @shape(pick: ["summary", "rating"], hoist: false) # Schemaless JSON
      }
    }
  }
}
{
  "books": [
    {
      "isbn": "0-061-96436-0",
      "title": "Moby Dick",
      "author": "Herman Melville",
      "summary": "A legendary tale...",
      "rating": "4.90"
    },
    "...",
  ]
}

API

Each transformation falls into 1 of the following lookup tables (referenced in order of preference):

Lib
nameargdescription
self{string|array} JSONPathSelect from the current field
parent{string|array} JSONPathSelect from the field's parent
root{string|array} JSONPathSelect from the root object
map{Object|AoO} TransformIterate field value(s) and apply transformation(s) to each
assign{*} ValueAssign a value to the field
rename{string} KeyRename the field key
hoist{boolean} KeepHoist all field attributes to the parent and optionally delete field
Core

Javascript core object references. Cannot be re-defined.

nameargtypedescriptionexample
*MethodStringInvoke a core object methodDate.now(value, ...args)
*nullnullInvoke a core object (no method)Boolean(value, ...args)
*"new"StringInstantiate a core objectnew Array(value, ...args)

Where * is one of [Object, Array, Number, String, Boolean, Symbol, Date, RegExp, Set, Map, WeakMap, WeakSet, Buffer, Math, JSON, Intl]

User

Useful set of transformations. Can be re-defined.

nameargtypedescription
pushAnyString, ArrayArray.concat alias
popnullnullArray.pop; return array
shiftnullnullArray.shift; return array
unshiftAnyString, ArrayArray.unshift; return array
inValuesArrayReturn boolean if value in values
ninValuesArrayReturn boolean if value not in values
eqValuesArrayHoist all field attributes to the parent and optionally delete field
neKeep?BooleanHoist all field attributes to the parent and optionally delete field
gtKeep?BooleanHoist all field attributes to the parent and optionally delete field
gteKeep?BooleanHoist all field attributes to the parent and optionally delete field
ltKeep?BooleanHoist all field attributes to the parent and optionally delete field
lteKeep?BooleanHoist all field attributes to the parent and optionally delete field
notKeep?BooleanHoist all field attributes to the parent and optionally delete field
orKeep?BooleanHoist all field attributes to the parent and optionally delete field
andKeep?BooleanHoist all field attributes to the parent and optionally delete field
addKeep?BooleanHoist all field attributes to the parent and optionally delete field
subKeep?BooleanHoist all field attributes to the parent and optionally delete field
divKeep?BooleanHoist all field attributes to the parent and optionally delete field
mulKeep?BooleanHoist all field attributes to the parent and optionally delete field
modKeep?BooleanHoist all field attributes to the parent and optionally delete field
getKeep?BooleanHoist all field attributes to the parent and optionally delete field
setKeep?BooleanHoist all field attributes to the parent and optionally delete field
nvlKeep?BooleanHoist all field attributes to the parent and optionally delete field
uvlKeep?BooleanHoist all field attributes to the parent and optionally delete field
defaultKeep?BooleanHoist all field attributes to the parent and optionally delete field
pickKeep?BooleanHoist all field attributes to the parent and optionally delete field
pairsKeep?BooleanHoist all field attributes to the parent and optionally delete field
flattenKeep?BooleanHoist all field attributes to the parent and optionally delete field
unflattenKeep?BooleanHoist all field attributes to the parent and optionally delete field
Value

Lastly, invoke value.key(...args) if function; otherwise return value.

Extension

You may define (or redefine) a user transformation via:

GraphQLShape.define(name, function); // or
GraphQLShape.define(Map); // { name: function, name: function, ... }

Function signature: (value, ...args) => newValue

FAQs

Last updated on 04 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc