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 🙏

© 2025 – Pkg Stats / Ryan Hefner

graphqly

v0.1.11

Published

GraphQL made easy

Downloads

57

Readme

Graphqly

Graphqly is a library to reduce the complexity of developing Graphql services.

1. Motivation

I think it's complicated to develop & maintain Graphql services (mainly their schemas). Let's look at how we currently define such schemas:

import { makeExecutableSchema, addResolveFunctionsToSchema } from "graphql-tools";

const rootSchema = [
  `
  interface List {
    offset: Int!
    limit: Int!
    total: Int!
  }
  type Post {
    id: Int!
    title: String!
    content: String!
  }
  type Posts implements List {
    offset: Int!
    limit: Int!
    total: Int!
    posts: [Post!]
  }
  type Query {
    # List all posts
    posts: [Post]
  }
  type Mutation {
    addPost(title: String!, content: String!): Post
  }
  type Subscription {
    # Subscription fires on every comment added
    postAdded: Post
  }
  schema {
    query: Query
    mutation: Mutation
    subscription: Subscription
  }
`
];

const rootResolvers = {
  Query: {
    posts(root, args, context) {
      // ..
    }
  },
  Mutation: {
    addPost(root, { title, content }, context) {
      // ..
    }
  }
};

const resolverMap = {
  List: {
    __resolveType(obj, context, info) {
      if (obj.products) {
        return "Products";
      }
      return null;
    }
  }
}

const schema = makeExecutableSchema({
  typeDefs: [...rootSchema],
  resolvers: _.merge(rootResolvers)
});
addResolveFunctionsToSchema(executableSchema, resolverMap);

In my opinion, there are several things to pay attention to:

  • We have to define basic structures (type, interface, input, enum) and there's no mechanism for us to reuse such structures in other defintions. By reuse, I mean we can't have something like:

    type PostEx extends Post{
          
    }

    This makes reusability hard.

  • Each operation (query, mutation, subscription) requires an associated resolver function. Separating definitions and resolvers makes Graphql flexible. But if you have large operations to serve, it's a pain in the ass to organize them properly.

Graphqly is designed to solve above problems elegantly.

2. Installation

npm install graphqly --save

# or if you're using yarn
# yarn add graphqly

3. Usage

For more information, please visit repo graphqly-demo

3.1 Definitions

import graphly from "graphqly";

const gBuilder = graphly.createBuilder();

// define types, inputs ... (in any order)
gBuilder.type("Products").implements("List").def(`
  products: [Product]!
`);

gBuilder.type("Product").def(`
  id: ID!
  name: String!
  link: String
  price: Int
`);

// we're too lazy to define a separate input, so we can `extend` other structure
gBuilder.input("ProductInput").ext("Product");

gBuilder.enum("ProductOrder").def(`
  PRICE_DESCENDING
  PRICE_ASCENDING
  NEWEST
`);

// define interface `List`
gBuilder.iface("List").def(`
  total: Int!,
  offset: Int!,
  limit: Int!,
  # number of items actually in this window
  size: Int!
`);

// subscriptions may not need to provide resolving functions
// If you want to manipulate published data, you must provide resolving functions of 
// `(payload, args, context, info) => {any}` as described by
// https://github.com/apollographql/graphql-subscriptions#payload-manipulation
gBuilder.subscription(`
  productAdded: Product
`);


gBuilder.query(`
  products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products
`)
.resolve((root, args, context) => {
  // your resolver here
  // `this` is binded to the current Query instance
});

gBuilder.mutation(`
  createProduct(product: ProductInput): Response!
`)
.resolve(function(root, args, context) {
  const { product } = args;
  // Publish events whenever a new product is added
  // `this` is binded to the current Mutation instance
  this.publish("productAdded", product);
  return createProduct({ product });
});

// and finally
const schema = gBuilder.build(); // inside, `makeExecutableSchema` is invoked

You may work with meta data of Resolvable instances (including Queries, Mutations & Subscriptions) via methods set, get:

gBuilder.query(`
  products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products
`)
.set("scope", ["products.read"]) // imagine we have scopes to use when authorizing requests
.resolve(function(root, args, context) {
  // your resolver here
  // `this` is binded to the current Query instance
  console.log(this.get("scope")); // will printed ["products.read"] out.
});

3.2 Reusability

We may reuse other definitions by grouping them into providers:

function Brand(builder){
  builder.type("Brands").implements("List").def(`
    brands: [Brand]!
    `);
  builder.type("Brand").def(`
    id: ID!
    name: String!
    link: String
    createdAt: String!
    updatedAt: String
    `);
}

function Product(builder){
  // ......
}

gBuilder.use(Brand); // it's fun, right?

// you can also use multiple providers
gBuilder.use([Brand, Product]);

3.3 Extendability

Graphqly can be extended by using hooks. For example, we can have features of logging, caching, authorizing... through hooks.

Graphqly has 2 kinds of hooks:

  • Global hooks in SchemaBuilder
  • Local hooks in each operations (queries or mutations)

There are 4 hook points, including pre.query, post.query, pre.mutation, post.mutation.

// Global hook
gBuilder.hook({
  options: {
    point: "pre.query"
  },
  // hook functions will receive above configuring options,
  handle: function(opts){
    // if you invoke done(value), resolving functions will not be called
    // the promise chain will stop immediately
    // NOTE: `this` is binded to the associated Resolvable (determine at runtime)
    //  If you use `arrow functions`, `this` is not binded consistently. So use a normal one instead.
    return function(root, args, context, done){
      console.log("Logging...");
      // we may get `scope` here to determine if users are authorized to access the API
      return [root, args, context];
    };
  }
});

// Local hook
gBuilder
  .query("products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products")
  .resolve((root, args, context) => {
    const { offset, limit, filter } = args;
    return getProducts({ offset, limit, filter });
  })
  .hook({
    options: {
      point: "pre.query"
    },
    handle: function(opts){
      return function(root, args, context){
        // `this` is binded to the query (or mutation)
        console.log("In query....", this._name);
        // Remember to return an array.
        return [root, args, context];
      }
    }
  })
  ;

3.4 Logging

createBuilder accepts configurable options. Currently, graphqly only allows users to configure logging capability.

import { createBuilder, extra } from "graphqly";
const gBuilder = createBuilder({
  loggerFactory: new extra.winston.WinstonLoggerFactory()
});

// now in resolving functions, you may use functions `error`, `info`...
gBuilder
  .query("products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products")
  .resolve(function(root, args, context){
    this.info("Accessing API `products`");
    const { offset, limit, filter } = args;
    return getProducts({ offset, limit, filter });
  });

You may provide your own logger factories. Please have a look at directory lib/extra/winston for more information.

By default, there's no logger factory configured. But if you configure it, in case of runtime errors in resolving functions, you may have related information printed out. For example

2017-07-26T13:29:49.403Z - error: [Subscription orderTimelineChanged] Filtering error. Detail: ReferenceError: _ is not defined

4. License

GNU GPL3

Please submit a pull request if you see anything that can be improved!