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

@aerogear/graphql-query-mapper

v0.4.2

Published

Query specific fields in resolvers

Downloads

11

Readme

GraphQL Query Mapper

GraphQL Query Mapper transform resolvers info argument into structure that can be used to filter data based on fields queried by client.

Example use case

When building public GraphQL API external developers can often missuse the queries. For example getProfile query that does expensive fetch from different servers can be used only to fetch username for home page. To prevent from overfetching we can extract information about required fields from the info object and avoid expensive queries.

Query Mapping

Query mapping will prevent from server side database overfetching data by providing list of the fields that were requested in the client side query. Developers can use them to perform targeted queries against their database and rest endpoints.

Library exposes following methods:

getQueryObject: provides list of fields that user queried with additional helpers for database access

Example:

import { getQueryObject} from '@aerogear/graphql-query-mapper'

const resolvers = {
  Query: {
    models (_, params, context, info) {
      const queryData = getQueryObject(info)
      console.log(`${queryData.getRootFields()}`)
    }
  }
}

API

getQueryObject returns following type


    /**
     * Query fields specified in client side query
     */
    fields: string[];

    /**
     * All relations that are part of the query
     */
    relations: {
        [relationName: string]: any;
    };

    /**
     * Check if object has relations
     */
    hasRelations(): boolean;

    /**
     * Checks if object has specified relation
     */
    hasRelation(name: string): boolean;

    /**
     * Returns root fields in format acceptable for most of the sql queries
     * @param separator - separates variables (default ,)
     */
    getRootFields(separator?: string): string;

    /**
     * Returns relation fields in format acceptable for most of the sql queries.
     * Method works with PostgresDB, MySQL and any other database that supports
     * this syntax.
     *
     * @param mapper - argument that maps composite field to single one.
     * By default `as` for PostgreSQL. Use `on`for mysql.
     * @param separator - separates variables (default ,)
     */
    getRelationFields(relation: string, mapper?: string, separator?: string): any;

    /**
     * Expands single key structure returned from database to graph that can
     * be returned by resolver. Method picks all fields that starts with relation name.
     * For example 'relation__field' and puts them into nested relation structure.
     */
    expandToGraph(data: any): any;

Limitations

Derived fields will still require additional checks in the resolver. For example fullname that consist of the firstName+secondName from database:

if(fields.fullname){
    fields.push("firstName")
    fields.push("secondName")
}

Performance consideration

When using query mapper we can opt out from default GraphQL query execution logic and only use only top level (root) query resolvers. Root resolvers can fetch all data required from relationships and deliver it much faster than in classical execution plan that needs to traverse thru entire graph.

Additionally developers can use graphql compiler to provide V8 optimializations for Node.js queries. See https://github.com/zalando-incubator/graphql-jit for more information.

Applying this patterns will help to archieve up to ~15 times better performance comparimng to using graphql reference implementation. This aproach will not require Facebook Data Loader cache layer sice all queries and data will be controlled from the root.

Roadmap

  • [x] Parsing info object
  • [x] Relational data query filtering
  • [ ] NoSQL data query filtering

Contributing

  • Star repository
  • Check CONTRIBUTING.md

License

Apache-2.0

Notes

Project maintained by AeroGear GraphQL Team: https://github.com/aerogear/graphql-home