0.1.71 • Published 4 years ago

sequelize-graphql-schema v0.1.71

Weekly downloads
88
License
MIT
Repository
github
Last release
4 years ago

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

optiontypeexampledescription
authorizerFunctionYour custom authorization mechanism goes here, all queries and mutations will be called after this. This must return a promise.
excludeArray['MODEL_NAME', 'MODEL_NAME']Pass in model names to exclude from graphql schema.
includeArgumentsObject{ 'customArgument', 'int' }These arguments will be included in all queries and mutations.
remoteObjectSee Remote OptionsImport queries from external graphql schema.

Model Options

optiontypeexampledescription
attributesObject{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.
bulkArray['create', 'destroy']Create mutations for bulk create or destroy operations.
aliasObject{ count: 'myCountQuery', fetch: 'myQuery', create: 'myCreateMutation', destroy: 'myDeleteMutation, update: 'myUpdateMutation' }Rename default queries and mutations with alias.
excludeMutationsArray[ 'create', 'update', 'destroy' ]Exclude default mutations.
excludeQueriesArray[ 'count', 'fetch' ]Exclude default queries.
excludeSubscriptionsArray[ 'default', 'create', 'update', 'destroy' ]Exclude default subscriptions.
typesObject{myType: { id: '[int]' }, myTypeInput: { id: 'int' }}Create custom types. Add Input postfix to convert to input type.
mutationsObject{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.
queriesObject{myQuery: { output: '[myType]', resolver: customResolver }}Custom queries to be created. input or output can refer to a custom input type or default graphql types.
subscriptionsObject{mySubs: { output: '[myType]', resolver: customResolver, subscriber: customSubscriber }}Custom subscriptions to be created. input or output can refer to a custom input type or default graphql type
subsFilterObject{create: (payload, variables, context, info) => { return Promise.resolve(); }}To run before subscription executes. Available options are default, create, fetch, destroy and update. Functions must return a promise or boolean.
beforeObject{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.
overwriteObjectsame as beforeThis will overwrite default query or mutation.
extendObjectsame as before with data coming from default passed to this function: create: (data, source, args, context, info)To extend default functionality.
importArraysee remote optionsAssociations with remote schema.
Product.graphql = {
    attributes: {
        exclude: ['description'],
        include: { modelPortfolioId: 'int', obj: 'myObj' },
    }
};

Remote Options

optiontypeexampledescription
importObject{'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.
headersArray[ 'authorization' ]Common headers passed to all endpoints.

Model Import Options

optiontypeexampledescription
fromStringRemote schema name from Import.
asStringalias for remote schema.
withStringforeign key from model.
toStringtarget key from remote model.

Usage

const {generateModelTypes, generateSchema} = require('sequelize-graphql-schema')(options);
const models = require('./models')
const schema = generateSchema(models) // Generates the schema
// OR
const types = generateModelTypes(models)
const schema = generateSchema(models, types) // Generates the schema by reusing the types

Example with Express

const { GraphQLSchema } = require('graphql');
const express = require('express');
const graphqlHTTP = require('express-graphql');

var options = {
  exclude: ["Users"]
}

const {generateSchema} = require('sequelize-graphql-schema')(options);

const models = require('./models');

const app = express();

app.use(
  '/graphql',
  graphqlHTTP({
    schema: new GraphQLSchema(generateSchema(models)),
    graphiql: true
  })
)

app.listen(8080, function() {
  console.log('RUNNING ON 8080. Graphiql http://localhost:8080/graphql')
})

Credits

This library is inspired and was initiated from https://github.com/rpellerin/graphql-sequelize-schema-generator.

1.0.0-beta.18

4 years ago

1.0.0-beta.17

4 years ago

1.0.0-beta.16

4 years ago

1.0.0-beta.15

4 years ago

1.0.0-beta.14

4 years ago

1.0.0-beta.12

4 years ago

1.0.0-beta.13

4 years ago

1.0.0-beta.11

4 years ago

1.0.0-beta.10

4 years ago

1.0.0-beta.9

4 years ago

1.0.0-beta.8

4 years ago

1.0.0-beta.7

4 years ago

1.0.0-beta.6

4 years ago

1.0.0-beta.5

4 years ago

1.0.0-beta.4

4 years ago

1.0.0-beta.3

4 years ago

1.0.0-beta.2

4 years ago

1.0.0-beta.1

4 years ago

0.1.71

4 years ago

0.1.70

5 years ago

0.1.69

5 years ago

0.1.68

5 years ago

1.0.0-beta.0

5 years ago

0.1.67

5 years ago

0.1.66

5 years ago

0.1.65

5 years ago

0.1.64

5 years ago

0.1.63

5 years ago

0.1.62

5 years ago

0.1.61

5 years ago

0.1.60

5 years ago

0.1.59

5 years ago

0.1.58

5 years ago

0.1.57

5 years ago

0.1.56

5 years ago

0.1.55

5 years ago

0.1.54

5 years ago

0.1.53

5 years ago

0.1.52

5 years ago

0.1.51

5 years ago

0.1.50

5 years ago

0.1.49

5 years ago

0.1.48

5 years ago

0.1.47

5 years ago

0.1.44

5 years ago

0.1.43

5 years ago

0.1.42

5 years ago

0.1.41

5 years ago

0.1.40

5 years ago

0.1.39

5 years ago

0.1.38

5 years ago

0.1.37

5 years ago

0.1.36

5 years ago

0.1.35

6 years ago

0.1.34

6 years ago

0.1.33

6 years ago

0.1.32

6 years ago

0.1.31

6 years ago

0.1.30

6 years ago

0.1.29

6 years ago

0.1.28

6 years ago

0.1.27

6 years ago

0.1.26

6 years ago

0.1.25

6 years ago

0.1.24

6 years ago

0.1.23

6 years ago

0.1.22

6 years ago

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago