cc-graphql

0.0.7 • Public • Published

cc-graphql

A package to handle schemas and resolvers for epress-graphql

Installation

npm i --save cc-graphql

How to use

const express = require('express');
const graphqlHTTP = require('express-graphql');
const graphQLHandler = require('cc-graphql');
const app = express();
// by default you can store all your resolvers and schemas in folders with same name
// if not, you can specify each folder
const gql = graphQLHandler({
  resolversPath: 'resolvers',
  schemasPath: 'schemas'
});
const schema = gql.getSchemas();

app.use('/gql', graphqlHTTP({
    schema,
    graphiql: 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

Package Sidebar

Install

npm i cc-graphql

Weekly Downloads

0

Version

0.0.7

License

MIT

Unpacked Size

8.2 kB

Total Files

3

Last publish

Collaborators

  • development.fino