0.0.7 • Published 5 years ago
cc-graphql v0.0.7
cc-graphql
A package to handle schemas and resolvers for epress-graphql
Installation
npm i --save cc-graphqlHow 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
rootandargs. Use_for root argument if it will not usable and deconstructive forargs. Example:(_, { newItem }) => {} // be aware _ would be root rootalways gonna containrequestobject in it. And inrequestobject gonna be availablesessionobject. Exampleconst userList = async ({ request }, { options }) => { const { session } = request; console.log(session.userId, session.name); };
- Query for USer model and action of a "list" of users:
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