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

apollo-type-patcher

v1.0.3

Published

type patcher for apollo-link-rest

Downloads

535

Readme

apollo-type-patcher

Utility to generate Type Patcher functions for apollo-link-rest.

Build Status

Jump to: GoalInstallation | Usage | How it works | Features | Contribute

Goal

The apollo-link-rest library enables a smooth, frontend-first transition into GraphQL, by allowing you to leverage this query language in a project powered by a REST API (read some of the reasons here). However, since you don't have a schema for your type definitions (because you don't have a GraphQL server), you have to generate the type patcher functions yourself. This is a very verbose, time consuming and error-prone process, specially if you're trying to consume endpoints with a lot of nested fields whose types you want to normalize into the cache.

The goal of this apollo-type-patcher library is to generate the type patcher functions easily and in a safe maintanable way, with an object contaiting your type definition mappings.

Installation

To add this library to your project's dependencies simply run:

yarn add apollo-type-patcher

Usage

Try it out in the browser(soon!)

  1. Create your type definitions object. This object is in the form of
typeDefinitions = {
    TYPE: {
        FIELD: TYPE,
        ...
    },
    ...
}

for example:

// typeDefinitions.js
export const typeDefinitions = {
    Student: {
        classes: Class,
        extra_activities: Activities
    },
    Class: {
        chair_professor: Professor
    }
}
  1. When setting up your Apollo link rest configuration, add the type patcher as follows:
import typePatcher from "apollo-type-patcher";
import { RestLink } from "apollo-link-rest";
// your type definitions
import { typeDefinitions } from "./typeDefinitions";

const restLink = new RestLink({
  typePatcher: typePatcher(typeDefinitions),
  ...
});

How it works

The type patcher adds the typename property to nested types, since the root type is injected directly by Apollo:

// for the Query:
const GET_STUDENT = gql`
  query getStudent($id: ID!) {
    student(id: $id) @rest(type: "Student", path: "student/{args.id}") {
      id
      degree {
        id
      }
    }
  }
`;

// and for the type defintions:
typeDefinitions = {
    Student: {
        degree: Degree,
    },
}

// the output of the Apollo request (pre type patching) is:
student = {
    id: 60,
    degree: {
        id: 5,
    },
    __typename: "Student" // <- Added by Apollo directly
}

// the final of the Query (pos type patching) is:
student = {
    id: 60,
    degree: {
        id: 5,
        __typename: "Degree" // <- Added by the typePatcher
    },
    __typename: "Student"
}

Features

  • add types in nested objects
// typeDef
Student: {
    class: "Class"
}
// out
student: {
    class: {
        __typename: "Class"
    }
}
  • add types in nested arrays
// typeDef
Student: {
    topics: "Topic"
}
// out
student: {
    topics: [{
        __typename: "Topic"
    }, ...]
}
  • add types in deeply nested properties
// typeDef
Student: {
    "needs.medical_needs.insurance": "Insurance"
}
// out
student: {
    needs: {
        medical_needs: {
            insurance: {
                __typename: "Insurance"
            }
        }
    }
}

Contribute

After cloning the repo locally, you can run:

yarn // install dependencies
yarn build // build the bundle w/webpack
yarn test // run unit tests