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

v1.3.0

Published

Transform fields into relay connections using a @connection directive

Downloads

21

Readme

GraphQL pagination transform

Transforms GraphQL schema fields into a relay connections using a @connection directive.


Features

  • Create the needed Connection and Edge object types.
  • Reassign the type of marked fields to the Connection type.
  • Remove any @connection directives.
  • Generate the PageInfo object type if it hasn't been defined.
  • Support non-nullable types
  • Throw errors if the generated Connection and Edge types have a name conflict with types already defined in your SDL.
  • Leave everything else in your SDL untouched.
  • Apply cacheControl directives to Connection and Edge types if exist.
  • Works with Apollo Federation schemas

Usage examples

  1. Install library
npm install --save graphql-pagination-transform
  1. Add @connection directive to the fields in your schema that needs to be transformed into relay connections.

  2. Import connecton directive from this lib

import { connectionDirective } from 'graphql-pagination-transform'

const { connectionDirectiveTypeDefs } = connectionDirective()
  1. Merge it with your schema type definitions
import gql from 'graphql'
import typeDefs from './typeDefs' // Path to your schema type definitions

const schema = gql([typeDefs, connectionDirectiveTypeDefs])

export default schema

or using mergeTypeDefs from graphql-tools in case your type definitions are DocumentNode's

import { mergeTypeDefs } from '@graphql-tools/merge'
import typeDefs from './typeDefs' // Path to your schema type definitions

const schema = mergeTypeDefs([assetTypeDefs, scalarTypes, directiveTypeDefs])

export default schema
  1. Transform type definitions. This will remove all @connection directives and create connection types with edges, nodes and pageInfo
import transform from 'graphql-directive-connection'
impot schema from './schema'

const result = transform({ typeDefs, cacheControl: { enable: true, apollo: true } })

Transformation result is a string representation of your type definitions. You will probaly want to convert it to GraphQLSchema type later. This could be archived using graphql-tools makeExecutableSchema or Apollo buildSubgraphSchema (for Apollo Federation).

cacheControl directives

This plugin will apply cacheControl directive on Connection type, edge and pageInfo fields by default.

Remember to add cacheControl directive to your schema in case you are not explicitly disabling it in a transform function.

enum CacheControlScope {
  PUBLIC
  PRIVATE
}

directive @cacheControl(
  maxAge: Int
  scope: CacheControlScope
  inheritMaxAge: Boolean
) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION

In order to completely ignore cache arguments and disable cacheControl directive pass cacheControl: false or cacheControl: { enable: false } argument to transform (default plugin export) function. The package will then use the largest maxAge across the connection fields with custom types and apply it to non-scalar fields and types (e.g. edges, node and pageInfo).

GraphQL Apollo v3 and later supports inheritMaxAge argument which forces a particular field to inherit the maxAge of its parent field. You can enable this feature by passing cacheControl: { enable: true, apollo: true } to a transform function.

Keep in mind that due to the modified cacheControl heuristics in Apollo v3+ this could technically make any queries with Connection types uncacheable (see https://www.apollographql.com/docs/apollo-server/performance/caching/#why-are-these-the-maxage-defaults). Enabling defaultMaxAge across your GraphQL implementation might partially solve the problem, but only for Apollo v2 and lower versions. Thus, it is recommended to leave cacheControl directives enabled.

Authors