UNPKG

@teamstarter/graphql-sequelize-generator

Version:

A set of tools to easily generate a Graphql API from sequelize models.

52 lines (48 loc) 1.46 kB
import { GraphQLList, GraphQLType } from 'graphql' import { defaultArgs, defaultListArgs } from 'graphql-sequelize' import injectAssociations from '../associations/inject' import createListResolver from './createListResolver' /** * Returns a root `GraphQLObjectType` used as query for `GraphQLSchema`. * * It creates an object whose properties are `GraphQLObjectType` created * from Sequelize models. * @param {*} models The sequelize models used to create the root `GraphQLSchema` */ export default function generateListResolver( modelType: any, allSchemaDeclarations: any, outputTypes: any, models: any, globalPreCallback: any ): { type: GraphQLList<GraphQLType> args: any resolve: any } { const schemaDeclaration = allSchemaDeclarations[modelType.name] if (!schemaDeclaration.model) { throw new Error( `You provided an empty/undefined model for the endpoint ${modelType}. Please provide a Sequelize model.` ) } return { type: new GraphQLList( injectAssociations( modelType, allSchemaDeclarations, outputTypes, models, globalPreCallback ) ), args: { ...defaultArgs(schemaDeclaration.model), ...defaultListArgs(), ...(schemaDeclaration.list && schemaDeclaration.list.extraArg ? schemaDeclaration.list.extraArg : {}), }, resolve: createListResolver(schemaDeclaration, models, globalPreCallback), } }