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

@encoura/apollo-rest-utils

v1.6.1

Published

A helper package that makes connecting apollo to rest endpoints as painless as possible

Downloads

9

Readme

Apollo Rest utils

Encoura's solution to integrating rest APIs with Apollo and Apollo Client.

These utils build on top of the great work of apollo-link-rest.

This library provides helper functions and generators for making integration with REST apis and Apollo in a TypeScript application.

Features

  • A command line utiltity that takes a swagger file/url, and automatically generates input and output types, and endpoint definitions that can be used to make integration with apollo-link-rest much easier.
  • Wrapper functions for common GraphQL operations that allow you to pass in pure GraphQL, and enables the input variables and the result to be strongly typed based on the swagger definition.
  • Automatically checks your GraphQL at runtime and will throw exceptions if your GraphQL fields do not match the endpoint definition.
  • Custom apollo links to cover REST API edge cases, such as using the headersLink to retrieve data from REST response headers.

Usage

From the command line you can generate definitions for endpoints:

npx apollo-rest-utils <path_to_swagger_file_or_url> <output_directory_where_you_want_the_files> [optional_endpoint_id_to_use] 

Then you can use those definitions to make GraphQL calls within an Apollo context:

import { wrapRestQuery } from '@encoura/apollo-rest-utils';

import ROUTES from 'path/to/directory_specified_in_cli_call/__generatedRestEndpoints';

const userSearch = (searchPattern: string) => {
  const wrappedRestQuery = wrapRestQuery<'users'>();

  const { data, loading } = wrappedRestQuery(
    gql`
      query RenderUserSearchQuery($search: String!) {
        user(search: $search) {
          id
          name
          city
          state
        }
      }
    `,
    {
      endpoint: ROUTES.GET.USER_SEARCH,
      skip: !searchPattern,
      variables: {
        search: searchPattern,
      },
    },
  );

  return data?.users ?? [];
}

Endpoint IDs

To facilitate using this with multiple endpoints, you must specify an endpoint id per endpoint. See https://www.apollographql.com/docs/react/api/link/apollo-link-rest/#multiple-endpoints

Custom Links

HeadersLink

This link allows you to access the REST API's response headers within the data object that comes back from Apollo Client.

NOTE: Since GraphQL only accepts field names written in camelCase, the headers should be requested in camel case format. I.e. if your REST API returns a total-count header, you'll want to ask for it using totalCount.

HeadersLink Setup

import { HeadersLink } from '@encoura/apollo-rest-utils';
import { RestLink } from 'apollo-link-rest';

const headersLink = new HeadersLink();
const restLink = new RestLink({ ... });

new ApolloClient({
  ...
  cache: new InMemoryCache({ ... }),
  link: ApolloLink.from([headersLink, restLink]),
  ...
});

HeadersLink Usage

const { data } = wrapRestQuery<'something'>()(
  gql`
    query SomethingQuery($id: String!) {
      something(id: $id) {
        _id
        fieldA
        fieldB
        fieldC
      }
      headers {
        nextOffset
        totalCount
        totalPages
      }
    }
  `,
  {
    endpoint: REST.GET.SOMETHING,
    variables: {
      id,
    },
  },
);

console.log(data?.something); // response data
console.log(data?.headers); // response headers

Releasing

Checking in the dist folder is not necessary as it will be built upon npm install by the downstream project.

After making any changes and merging them to main, please do the following:

  • Create a new branch from main and run npm run update:version
  • Verify the CHANGELOG.md generated changes
  • Commit, push, and merge to main.
  • Create a new release using the tag generated in the previous steps
  • Use the Auto-generate release notes button to generate the release notes, and add any context you may deem necessary.