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-aggregate

v0.2.0

Published

Aggregation functions for lists in graphql

Downloads

49

Readme

graphql-aggregate

CircleCI Coverage Status

Generates an aggregation schema for graphql that can be used to perform aggregation functions via a graphql query on Arrays of GraphQL types.

Usage

To get access to an aggregation schema on a graphql you require an array of objects

###Sample Code

A small sample, using express-graphql and some sample data exists here

###Example

The following GraphQLFieldConfig defines a list of answers

answers : {
  type: new GraphQLList(AnswerType)
}

It can be turned into an aggregate type using the following GraphQLFieldConfig

see GraphQL's documentation on field configuration of the GraphQLObjectType

import {AggregationType} from 'graphql-aggregate'

// Creates an AggregationType with based on the AnswerType
// The resolver must return an Array that can be resolved into AnswerTypes

aggregateAnswers: {
    type: AggregationType(AnswerType), 
    resolve: (obj) => obj.answers
}

after this is done, the schema will allow the user to aggregate tusing the fields in the answer type.

for instance if the AnswerType had the following definition.

type AnswerType {
  id: ID,
  username: String,
  usersAnswer: Int!
}

The following query would be a valid way of finding the amount of answers attributed to each user.

aggregateAnswers {
    groupedBy {
      username {
        keys
        values {
            count
          }
        }
      }
    }
  }
}

You can also further apply aggregations on the aggregation.

aggregateAnswers {
    groupedBy {
      username {
        keys
        values {
            groupedBy {
              usersAnswer {
                asMap
              }
            }
          }
        }
      }
    }
  }
}

Supplied Schema

Aggregation types will be named based on the type the were created from, for instance if our type was named Answer our aggregation type would be named AnswerAggregation.

####Fields Provided

The follow is the fields that are provided via the api, the best way of seeing exactly how it works is by using GraphiQL to investigate the fields.

The Aggregation provides fields dependent on the type that it is created with, this allows a more natural syntax then using arguments to lookup preknown values, and allows us to use graphql type checking simply on the queries.

Aggregation<T> : {
  values: [T],                        // Values in aggregation.
  count: Int,                         // Amount of values in aggregation.
  groupedBy: GroupedByAggregation<T> : { 
    fields in T...: KeyedList: {
      asMap: Scaler                   //Map of key/values.
      keys: List<String>              //List of keys in order.
      values: Aggregation<T>          //Returns the aggregation functions to be used on the values of the current aggregation
    },
  },
  filter: FilterAggregation<T> : {    //Filter aggregation methods
    int fields in T...args:(gt: int, lt: int, gte: int, equal int): Aggregation<T>
    string fields in T...args:(gt: string, lt: string, gte: string, equal string): Aggregation<T>
  },
  sum: {
    float or int fields in T...: int // sum of the all the values in the field.
  },
  average: {
    float or int fields in T...: int // average of the all the values in the field.
  },
  min: {
    float or int fields in T...: int // minimum value in the field.
  },
  max: {
    float or int fields in T...: int // maximum value in the field.
  }
}