sequelize-graphql-schema
A helper library that lets you focus on business logic by automatically generates GraphQLSchema
and manages graphQL from Sequelize model.
Installation
npm install sequelize-graphql-schema
Prerequisites
This package assumes you have graphql
and sequelize
already installed (both packages are declared as peerDependencies
).
Options
option | type | example | description |
---|---|---|---|
authorizer | Function | Your custom authorization mechanism goes here, all queries and mutations will be called after this. This must return a promise. | |
exclude | Array | ['MODEL_NAME', 'MODEL_NAME'] |
Pass in model names to exclude from graphql schema. |
includeArguments | Object | { 'customArgument', 'int' } |
These arguments will be included in all queries and mutations. |
remote | Object | See Remote Options | Import queries from external graphql schema. |
Model Options
option | type | example | description |
---|---|---|---|
attributes | Object | {exclude: ['ATTRIBUTE_NAME'], include: { customAttributeName: 'int' }} |
Model attributes in exclude will be excluded from graphql types. Non-Model custom attributes will be added in graphql type from include. |
bulk | Array | ['create', 'destroy'] |
Create mutations for bulk create or destroy operations. |
alias | Object | { fetch: 'myQuery', create: 'myCreateMutation', destroy: 'myDeleteMutation, update: 'myUpdateMutation' } |
Rename default queries and mutations with alias. |
excludeMutations | Array | [ 'create', 'update', 'destroy' ] |
|
excludeQueries | Array | [ 'fetch' ] |
|
types | Object | {myType: { id: '[int]' }, myTypeInput: { id: 'int' }} |
Create custom types. Add Input postfix to convert to input type. |
mutations | Object | {myMutation: { input: 'myTypeInput', output: '[myType]', resolver: customResolver}} |
Custom mutations to be created. input or output can refer to a custom input type or default graphql types. |
queries | Object | {myQuery: { output: '[myType]', resolver: customResolver }} |
|
before | Object | {create: (source, args, context, info) => { return Promise.resolve(); }} |
To run before default query or mutation executes. Available options are create , fetch , destroy and update . Functions must return a promise. |
overwrite | Object | same as before | This will overwrite default query or mutation. |
extend | Object | same as before with data coming from default passed to this function: create: (data, source, args, context, info) |
To extend default functionality. |
import | Array | see remote options | Associations with remote schema. |
Productgraphql = attributes: exclude: 'description' include: modelPortfolioId: 'int' obj: 'myObj' ;
Remote Options
option | type | example | description |
---|---|---|---|
import | Object | {'RemoteData': {endpoint: 'http://garphql-endpoint.com',queries: { 'myQuery': { as: 'RemoteQuery' } },headers: ['authorization']} |
Remote graphql data to import given queries, alias of query and headers to pass when calling it. |
headers | Array | [ 'authorization' ] |
Common headers passed to all endpoints. |
Model Import Options
option | type | example | description |
---|---|---|---|
from | String | Remote schema name from Import. | |
as | String | alias for remote schema. | |
with | String | foreign key from model. | |
to | String | target key from remote model. |
Usage
const generateModelTypes generateSchema = options;const models = const schema = // Generates the schema// ORconst types = const schema = // Generates the schema by reusing the types
Example with Express
const GraphQLSchema = ;const express = ;const graphqlHTTP = ; var options = exclude: "Users" const generateSchema = options; const models = ; const app = ; app app
Credits
This library is inspired and was initiated from https://github.com/rpellerin/graphql-sequelize-schema-generator.