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

moonbuggy

v1.0.0

Published

An AOP framework for building modular Apollo-Server GraphQL schema

Downloads

3

Readme

An AOP framework for building modular Apollo-Server GraphQL schema

See src/example for full example

import { Request } from 'hapi';
import { 
  module, 
  imports,
  name,
  rule, 
  exportOnly, 
  resolver, 
  schema 
} from './index';

const isAuth = (request: Request): boolean => Boolean(request.auth.credentials);

// define our module (name is class name by default, use `name` to override)
// optionally import resolvers from `Setting` module (to be used within our schema)
@module(
  name('Player'), 
  imports('Setting', ['getSettings']),
) 
class User {
  // define resolver (name is method name by default, use `name` to override)
  //
  // optionally mark resolver as `exportOnly` 
  // this resolver will not be bundled up in this module but can be imported by others
  @resolver(name('user'), exportOnly()) 
  // optionally define pre-conditions (can have multiple)
  @rule(isAuth, new Error('User is unauthenticated'))
  public user(root: any, args: any, context: Request) {
    return {
      id: '1',
      name: 'daniel',
      address: {
        line1: 'line1',
        postcode: 'ne23 ftg',
      },
    };
  }

  @resolver(name('friends'))
  public friendsResolver(root: any, args: any, context: Request) {
    return [{
      name: 'graeme',
    }];
  }

  @schema() // mark partial/full schema
  public friends() {
    return `
      type Friend {
        name: String
      }
    `;
  }

  @schema()
  public address() {
    return `
      type Address {
        line1: String
        postcode: String
      }
    `;
  }

  @schema()
  public basic() {
    return `
      type User {
        id: String
        name: String
        address: Address
        friends: [Friend]
        getSettings: [Setting]
      }
    `;
  }

}

export default new User(); // return instance of module as default export

For Mutations, if we want to simulate a union type - we can take advatage of inputMappers. Here, we define an input with multiple fields - yet provide only one resolver for the type. An inputMapper decorates the resolver which states which input field we're decorating, and the rules associated with each field in the union type.

A mutation request should only provide one implemented field. The corresponding rule is evaluated ensuring the request is valid, and the field's data is passed to the single resolver.

import { 
  module, 
  imports,
  name,
  rule, 
  exportOnly, 
  resolver, 
  schema 
} from './index';

@module()
class Registration {

  @resolver(
    name('register'),
    exportOnly(),
  )
  @inputMapper('input', new Error('failed to authorize register input'),
    field('retail', (request) => !!request.auth.credentials.retail),
    field('digital', (request) => !!request.auth.credentials.digital),
  )
  public register(root: any, args: any, context: any) {
    return {
      name: 'daniel',
    };
  }

  @schema()
  public mapper() {
    return `
      type Result {
        name: String
      }

      input Retail {
        PIN: String
      }

      input Digital {
        username: String
      }

      input RegistrationUnion {
        retail: Retail
        digital: Digital
      }
    `;
  }
}

export default new Registration();

Using the framework (with hapi):

import { Server } from 'hapi';
import { graphqlHapi, graphiqlHapi } from 'graphql-server-hapi';
import { makeExecutableSchema } from 'graphql-tools';
import { getBundle, BundleOptions, Bundle } from '../index';

const options: BundleOptions = {
  moduleRootDir: `${__dirname}/modules`,
  moduleFilename: 'index',
};

const { typeDefs, resolvers }: Bundle = getBundle(options);

const schema = {
  typeDefs,
  resolvers,
} as any;

const executableSchema = makeExecutableSchema(schema);

const server = new Server();

server.connection({
  host: 'localhost',
  port: 8080,
});

server.register([{
  register: graphqlHapi,
  options: {
    path: '/graphql',
    graphqlOptions: (request) => ({
      pretty: true,
      schema: executableSchema,
      context: request,
    }),
  },
}, {
  register: graphiqlHapi,
  options: {
    path: '/graphiql',
    graphiqlOptions: {
      endpointURL: '/graphql',
    },
  },
}]);

server.start((err) => {
  if (err) {
    throw err;
  }
  // tslint:disable-next-line no-console
  console.log(`Server running at: ${server.info.uri}`);
});