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

v1.0.4

Published

Define your GraphQL types in separate files

Downloads

1

Readme

GraphQL Scanner

Define your GraphQL types in separate files

Build Status Coverage Status NPM Downloads Dependencies Known Vulnerabilities

Introduction

A GraphQL server generally needs two things: a typeDefs string and a resolvers object.

  • The typeDefs string contains the schema definitions,
  • resolvers object describes how to resolve certain attributes of a type.

The problem is, both the typeDefs and resolvers can get huge and/or impractical to use.

Enter GraphQL Scanner! A tiny util that allows you to define each type (and their matching resolver) in a separate file.

Tested with Apollo Server, but should also work with GraphQL.js and express-graphql

Quickstart

Install the dependency

$ npm install graphql-scanner --save

Create a types directory

Create a 'types' directory in your project, and use it to store your GraphQL types.

For example:

├── types
|   ├── Query.js
|   └── TodoList.js
|   └── User.js
├── app.js

Define your types

Each type MUST expose an object containing two attributes:

  • typeDef (String, required): the GraphQL schema definition of your type
  • resolver (Object, optional): an object to resolve certain attributes of the type you've defined

For example:

types/Query.js

module.exports = {
  typeDef: `
      type Query {
        currentUser: User
      }
    `,
  resolver: {
    currentUser(_, args, ctx) {
      const { apikey } = ctx;
      return {
        firstName: "John",
        lastName: "Do",
        apikey
      };
    }
  }
};

types/User.js

module.exports = {
  typeDef: `
      type User {
        firstName: String!
        lastName: String
        apikey: String
        todoLists: [TodoList]
      }
    `,
  resolver: {
    todoLists(user, args, ctx) {
      return Promise.resolve([
        {
          subject: 'Holidays',
        }
      ]);
    }
  }
};

types/TodoList.js

module.exports = {
  typeDef: `
      type TodoList {
        subject: String!
        tasks: [String]
      }
    `,
  resolver: {
    tasks(todoList, arg, ctx)  {
      return Promise.resolve(['Fix a cab', 'Book a flight']);
    }
  }
};

Use the GraphQL Scanner

You need to pass the path to your directory that contain your types.

For example:

app.js:

const path = require('path');
const graphqlScanner = require('graphql-scanner');

// ...

const dir = path.join(__dirname, './types');
const { typeDefs, resolvers } = graphqlScanner(dir);
const server = new GraphQLServer({ typeDefs, resolvers });

The graphqlScanner(dir) expression scans your types and returns an object containing typeDefs and resolvers.

You can then pass these typeDefs and resolvers to your favourite GraphQL server library.

How does it work?

Check the code, it's less than 20 lines!

It uses require-all to load all modules in the dir you've passed as an argument

Then, it will iterate over all loaded modules and

  • append each individual typeDef string to a typeDefs variable.
  • add each individual resolver to a resolvers object as an attribute. The key of each attribute is the file name (minus extension) of the type

Finally, it will return an object: { typeDefs, resolvers }