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

@codecraftkit/gql

v0.3.1

Published

A handler for resolvers and schemas for graphql-express

Downloads

15

Readme

cc-graphql

A package to handle schemas and resolvers for epress-graphql

Installation

npm i --save cc-graphql

How to use

const { ApolloServer } = require('@apollo/server')
const graphQLHandler = require('@codecraftkit/gql')
// by default you can store all your resolvers and schemas in folders with same name
// if not, you can specify each folder
const gqlHandler = graphQLHandler({
  __dirname
})
const schema = gqlHandler.getSchemas()

const server = new ApolloServer({
  schema,
  csrfPrevention: true,
  cache: 'bounded',
  introspection: true
})

Methods

Methods | Params | Return :-----------------------| :--------------- | :------------------- getSchemas | | makeExecutableSchema getResolvers | | resolver object

Schemas and resolvers

Resolver Guide

Convections
  • Queries and Mutators name must be defined bu its model related and action verb. Example:
    • Query for USer model and action of a "list" of users: userList ({_model_}{_action_})
    • Arguments always are root and args. Use _ for root argument if it will not usable and deconstructive for args. Example:
    (_, { newItem }) => {} // be aware _ would be root
    • root always gonna contain request object in it. And in request object gonna be available session object. Example
    const userList = async ({ request }, { options }) => {
        const { session } = request;
        console.log(session.userId, session.name);
    };
File Structure

They are two ways to work a file. Choice gonna depends on the logic complexity. For extensive resolvers would be option one over two

Option one

Individual define for each query and mutator resolver callback in a const

const items = [1, 2, 3, 4];
const modelList = (root, args) => {
  return items;
};

const modelSave = (_, { newItem }) => {
  if(typeof newItem !== 'number'){
    // validation message
  }
  items.push(newItem);
  return newItem;
};

module.exports = {
  Queries: {
    modelList,
  },
  Mutations: {
    modelSave
  },
};
Option two

Exporting directly all queries and mutators

module.exports = {
  Queries: {
    modelList(){
      return items;
    },
  },
  Mutations: {
    modelSave(){
      if(typeof newItem !== 'number'){
        //validation message
      }
      items.push(newItem);
      return newItem;
    }
  },
};

Schema Guide

Convections
  • Schema name must be defined by Model related. Example UserSchema (_Model_Schema)
  • Must be declared in a const and it must be and array with js String.
const ModelSchema = [
  `
    type Query {
      queryHere
    }
  `
];
module.exports = ModelSchema;
  • You can define whatever you want: Query, Type, Input, Mutators, Directive, and others.
  • If you want you can separate types, inputs, directives and fragments into external files:
    • _inputs.js
    • _types.js
    • _directives.js
    • _fragments.js

Scalars

By default this lib use graphql-scalars and use the next Scalar:

  • DateTime
  • EmailAddress
  • NegativeFloat
  • NegativeInt
  • NonNegativeFloat
  • NonNegativeInt
  • NonPositiveFloat
  • NonPositiveInt
  • PhoneNumber
  • PositiveFloat
  • PositiveInt
  • PostalCode
  • URL
  • BigInt
  • Long
  • HexColorCode
  • JSON
  • JSONObject

Other libs