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

simple-graphql-assembler

v0.1.0

Published

Simple backend graphql shcema assembler for apollo projects

Downloads

14

Readme

Build Status codecov

simple-graphql-assembler

The main goal of this package is to create type definitions and resolvers based on the selected folder .gql and .resolver.js files. The main funtion will create the input tpyeDefs and resolvers for the AppolloServer by searching for .gql and .resolver.js files.

The .gql file formats are not validated, just simply reduced to a one string variable.

// hero.gql
type Hero {
  id: ID!
  name: String!
  actions: [Action]
}

extend type Query {
  heroes: [Hero]
  hero(id: ID!): Hero
}

It is required in the resolver modules to export a plain object with a __name property, because the package cannot figure out the key of the end resolver object.

// hero.resolver.js
const heroDb = require('../heroDb');

module.exports = {
  __name: 'Hero',

  actions(h) {
    return h.actions;
  },

  Query: {
    heroes() {
      return [...heroDb];
    },
    hero(_, { id }) {
      return heroDb.find(h => h.id === id);
    },
  },
};

The __name field will be removed from the final resolvers tree.

The gql file and resolver concatenations are separeted. The package will not search for specific resolver and type definition files. The patterns are based on the .gql extension and the .resolver.js postfix.

In the example i put the Query under the Hero resolver but it is not necessary, because the package will search for the Mutation or the Query named property in the resolver and moves them to the final tree in a separeted reduced property.

installation

npm

npm install simple-graphql-assembler

yarn

yarn add simple-graphql-assembler

usage

const { ApolloServer, gql } = require('apollo-server');
const assemble = require('simple-graphql-assembler');

const { typeDefs, resolvers, errors } = assemble(__dirname);
/*
The errors contains the validation errors.
Two specific rules:
   - Resolvers has to be a plain object
   - Resolvers must contain a __name property (String)
*/

// validation errors
if (errors) {
  console.log(errors);
  process.exit(1);
}

console.log(resolvers);
/*
{ Mutation: { addAction: [Function: addAction] },
  Action: { hero: [Function: hero] },
  Query: { heroes: [Function: heroes], hero: [Function: hero] },
  Hero: { actions: [Function: actions] } }
*/

const server = new ApolloServer({
  typeDefs: gql(typeDefs),
  resolvers,
});

server.listen().then(({ url }) => {
  console.log(`Example server listen at ${url}`);
});

cli usage

I added a tool for cli usage, the tool is called make-typedefs This tool will be traverse the current working directory for .gql files and with a very simple technique (copy) it will create one single file as a merged schema.gql.

Usage: make-typedefs [options]

Options:
  --version     Show version number                                    [boolean]
  --root, -r    Root folder of your gql definitions and resolvers [default: "."]
  --output, -o  Output file for type definitions       [default: "./schema.gql"]
  -h, --help    Show help                                              [boolean]

Examples:
  make-typedefs -r . -o ./schema.gql  Create the gql type definition file for
                                      your definitions from the current
                                      directory