@raynode/graphql-connector
Advanced tools
Comparing version 0.7.3 to 0.8.0
@@ -27,4 +27,5 @@ | ||
'!**/*.d.ts', // no need to test only types and interfaces | ||
'!**/tests/*', // no need to test only types and interfaces | ||
], | ||
// setupFiles: ['./jest.setup.js'], | ||
} |
@@ -1,10 +0,12 @@ | ||
import { AnyModel } from './model'; | ||
import { GraphQLFieldConfigMap, Thunk } from 'graphql'; | ||
import { PartialGeneratorConfiguration } from './configuration'; | ||
import { AnyModel, ModelFields } from './model'; | ||
export declare type ModelList<Types, Models> = Array<AnyModel<Types, Models>>; | ||
export interface BaseSchema { | ||
queryFields: any; | ||
mutationFields?: any; | ||
subscriptionFields?: any; | ||
export interface BaseSchema<Models> { | ||
getModel: (modelName: keyof Models) => ModelFields; | ||
queryFields: Thunk<GraphQLFieldConfigMap<any, any>>; | ||
mutationFields?: Thunk<GraphQLFieldConfigMap<any, any>>; | ||
subscriptionFields?: Thunk<GraphQLFieldConfigMap<any, any>>; | ||
} | ||
export declare type BaseSchemaGenerator<Types, Models> = (models: Models) => BaseSchema; | ||
export declare type BaseSchemaGenerator<Types, Models> = (models: Models) => BaseSchema<Models>; | ||
export declare const createBaseSchemaGenerator: <Types, Models>(configuration: PartialGeneratorConfiguration<Types, Models>) => BaseSchemaGenerator<Types, Models>; |
@@ -14,9 +14,9 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var graphql_1 = require("graphql"); | ||
var args_filter_generator_1 = require("./args-filter-generator"); | ||
var configuration_1 = require("./configuration"); | ||
var field_reducers_1 = require("./field-reducers"); | ||
var filter_field_generator_1 = require("./filter-field-generator"); | ||
var generic_types_1 = require("./generic-types"); | ||
var type_converter_1 = require("./type-converter"); | ||
var graphql_1 = require("graphql"); | ||
var configuration_1 = require("./configuration"); | ||
var field_reducers_1 = require("./field-reducers"); | ||
var extendModelReducer = function (configuration, models) { return function (record, name) { | ||
@@ -118,4 +118,6 @@ var model = configuration.modelMapper(name, models[name]); | ||
return { | ||
getModel: function (name) { return getModel(name).types; }, | ||
queryFields: modelNames.reduce(field_reducers_1.queryFieldReducer(getModel, configuration), {}), | ||
mutationFields: modelNames.reduce(field_reducers_1.mutationFieldReducer(getModel, configuration), {}), | ||
subscriptionFields: modelNames.reduce(field_reducers_1.subscriptionFieldReducer(getModel, configuration), {}), | ||
}; | ||
@@ -122,0 +124,0 @@ }; }; |
@@ -6,2 +6,3 @@ import { FilterMapper, FilterParser } from './filter-field-generator'; | ||
import { TypeMapper } from './type-converter'; | ||
import { PubSub } from 'graphql-subscriptions'; | ||
export interface GeneratorConfiguration<Types, Models> extends PartialGeneratorConfiguration<Types, Models> { | ||
@@ -19,2 +20,3 @@ modelMapper: GeneratedModelMapper<Types, Models>; | ||
orderMapper?: OrderMapper<any>; | ||
pubSub?: PubSub; | ||
} | ||
@@ -30,2 +32,3 @@ export declare const applyDefaultConfiguration: <Types, Models>(configuration: PartialGeneratorConfiguration<Types, Models>) => { | ||
}> | OrderMapper<any>; | ||
pubSub: PubSub; | ||
}; |
@@ -17,3 +17,4 @@ "use strict"; | ||
var order_mapper_1 = require("./order-mapper"); | ||
exports.applyDefaultConfiguration = function (configuration) { return (__assign({ filterMapper: filter_field_generator_1.defaultFilterMapper, filterParser: filter_field_generator_1.createDefaultFilterParser(), namingStrategy: naming_strategy_1.defaultNamingStrategy, orderMapper: order_mapper_1.defaultOrderMapper }, configuration)); }; | ||
var graphql_subscriptions_1 = require("graphql-subscriptions"); | ||
exports.applyDefaultConfiguration = function (configuration) { return (__assign({ filterMapper: filter_field_generator_1.defaultFilterMapper, filterParser: filter_field_generator_1.createDefaultFilterParser(), namingStrategy: naming_strategy_1.defaultNamingStrategy, orderMapper: order_mapper_1.defaultOrderMapper, pubSub: new graphql_subscriptions_1.PubSub() }, configuration)); }; | ||
//# sourceMappingURL=configuration.js.map |
@@ -0,5 +1,9 @@ | ||
import { GraphQLFieldConfigMap, Thunk } from 'graphql'; | ||
import { GeneratorConfiguration } from './configuration'; | ||
import { ExtendedModel } from './model'; | ||
export declare type Fields = Thunk<GraphQLFieldConfigMap<any, any>>; | ||
export declare type ModelFetcher<Types, Models> = (modelName: keyof Models) => ExtendedModel<Types, Models>; | ||
export declare const queryFieldReducer: <Types, Models>(getModel: ModelFetcher<Types, Models>, configuration: GeneratorConfiguration<Types, Models>) => (queryFields: any, name: any) => any; | ||
export declare type FieldReducer<Types, Models> = (fields: Fields, name: keyof Models) => Fields; | ||
export declare const queryFieldReducer: <Types, Models>(getModel: ModelFetcher<Types, Models>, configuration: GeneratorConfiguration<Types, Models>) => FieldReducer<Types, Models>; | ||
export declare const mutationFieldReducer: <Types, Models>(getModel: ModelFetcher<Types, Models>, configuration: GeneratorConfiguration<Types, Models>) => (mutationFields: any, name: any) => any; | ||
export declare const subscriptionFieldReducer: <Types, Models>(getModel: ModelFetcher<Types, Models>, configuration: GeneratorConfiguration<Types, Models>) => (subscriptionFields: any, name: any) => any; |
@@ -51,5 +51,7 @@ "use strict"; | ||
var data = _a.data; | ||
return model.resolvers.create(_, { | ||
var node = model.resolvers.create(_, { | ||
data: filterParser('create', data), | ||
}, context, info); | ||
configuration.pubSub.publish(names.events.create, { node: node }); | ||
return node; | ||
}, | ||
@@ -65,6 +67,8 @@ type: model.types.type, | ||
var data = _a.data, where = _a.where; | ||
return model.resolvers.update(_, { | ||
var node = model.resolvers.update(_, { | ||
data: filterParser('update', data), | ||
where: filterParser('where', where), | ||
}, context, info); | ||
configuration.pubSub.publish(names.events.update, { node: node }); | ||
return node; | ||
}, | ||
@@ -80,5 +84,7 @@ type: graphql_1.GraphQLNonNull(graphql_1.GraphQLList(graphql_1.GraphQLNonNull(model.types.type))), | ||
var where = _a.where, order = _a.order; | ||
return model.resolvers.delete(_, { | ||
var nodes = model.resolvers.delete(_, { | ||
where: filterParser('where', where), | ||
}, context, info); | ||
configuration.pubSub.publish(names.events.delete, { nodes: nodes }); | ||
return nodes; | ||
}, | ||
@@ -89,2 +95,32 @@ type: graphql_1.GraphQLNonNull(graphql_1.GraphQLList(graphql_1.GraphQLNonNull(model.types.type))), | ||
}; }; | ||
exports.subscriptionFieldReducer = function (getModel, configuration) { return function (subscriptionFields, name) { | ||
var model = getModel(name); | ||
var names = configuration.namingStrategy(name); | ||
var filterParser = filter_field_generator_1.applyFilterParser(configuration.filterParser)(model); | ||
subscriptionFields[names.events.create] = { | ||
subscribe: function () { return configuration.pubSub.asyncIterator(names.events.create); }, | ||
resolve: function (_a) { | ||
var node = _a.node; | ||
return node; | ||
}, | ||
type: graphql_1.GraphQLNonNull(model.types.type), | ||
}; | ||
subscriptionFields[names.events.delete] = { | ||
subscribe: function () { return configuration.pubSub.asyncIterator(names.events.delete); }, | ||
resolve: function (_a) { | ||
var nodes = _a.nodes; | ||
return nodes; | ||
}, | ||
type: graphql_1.GraphQLNonNull(graphql_1.GraphQLList(graphql_1.GraphQLNonNull(model.types.type))), | ||
}; | ||
subscriptionFields[names.events.update] = { | ||
subscribe: function () { return configuration.pubSub.asyncIterator(names.events.update); }, | ||
resolve: function (_a) { | ||
var node = _a.node; | ||
return node; | ||
}, | ||
type: model.types.type, | ||
}; | ||
return subscriptionFields; | ||
}; }; | ||
//# sourceMappingURL=field-reducers.js.map |
export interface Names { | ||
arguments: Record<'data' | 'filter' | 'order' | 'page' | 'where', string>; | ||
events: Record<'create' | 'delete' | 'update', string>; | ||
fields: Record<'create' | 'delete' | 'findMany' | 'findOne' | 'update', string>; | ||
arguments: Record<'data' | 'filter' | 'order' | 'page' | 'where', string>; | ||
types: Record<'createType' | 'dataType' | 'filterType' | 'orderType' | 'whereType', string>; | ||
@@ -5,0 +6,0 @@ } |
@@ -5,2 +5,14 @@ "use strict"; | ||
exports.defaultNamingStrategy = function (name) { return ({ | ||
arguments: { | ||
data: 'data', | ||
order: 'order', | ||
filter: 'filter', | ||
page: 'page', | ||
where: 'where', | ||
}, | ||
events: { | ||
create: "onCreate" + inflection_1.singularize(name.toString()), | ||
delete: "onDelete" + inflection_1.pluralize(name.toString()), | ||
update: "onUpdate" + inflection_1.singularize(name.toString()), | ||
}, | ||
fields: { | ||
@@ -13,9 +25,2 @@ create: "create" + inflection_1.singularize(name.toString()), | ||
}, | ||
arguments: { | ||
data: 'data', | ||
order: 'order', | ||
filter: 'filter', | ||
page: 'page', | ||
where: 'where', | ||
}, | ||
types: { | ||
@@ -22,0 +27,0 @@ createType: "Create" + inflection_1.singularize(name.toString()) + "Data", |
@@ -7,2 +7,2 @@ import { GraphQLSchema } from 'graphql'; | ||
export declare const applyToRecordOf: <Keys, Types, Result>(record: RecordOf<Keys, Types>, method: (type: Types, key: string) => Result) => RecordOf<Keys, Result>; | ||
export declare const createSchema: ({ queryFields, mutationFields }: BaseSchema) => GraphQLSchema; | ||
export declare const createSchema: <Models>({ queryFields, mutationFields, subscriptionFields }: BaseSchema<Models>) => GraphQLSchema; |
@@ -7,14 +7,21 @@ "use strict"; | ||
exports.createSchema = function (_a) { | ||
var queryFields = _a.queryFields, mutationFields = _a.mutationFields; | ||
return new graphql_1.GraphQLSchema({ | ||
query: new graphql_1.GraphQLObjectType({ | ||
name: 'Query', | ||
fields: queryFields, | ||
}), | ||
mutation: new graphql_1.GraphQLObjectType({ | ||
var queryFields = _a.queryFields, mutationFields = _a.mutationFields, subscriptionFields = _a.subscriptionFields; | ||
var query = new graphql_1.GraphQLObjectType({ | ||
name: 'Query', | ||
fields: queryFields, | ||
}); | ||
var mutation = mutationFields | ||
? new graphql_1.GraphQLObjectType({ | ||
name: 'Mutation', | ||
fields: mutationFields, | ||
}), | ||
}); | ||
}) | ||
: null; | ||
var subscription = subscriptionFields | ||
? new graphql_1.GraphQLObjectType({ | ||
name: 'Subscription', | ||
fields: subscriptionFields, | ||
}) | ||
: null; | ||
return new graphql_1.GraphQLSchema({ query: query, mutation: mutation, subscription: subscription }); | ||
}; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@raynode/graphql-connector", | ||
"version": "0.7.3", | ||
"version": "0.8.0", | ||
"description": "", | ||
@@ -43,2 +43,3 @@ "main": "lib/index.js", | ||
"@types/lodash": "^4.14.118", | ||
"graphql-subscriptions": "^1.0.0", | ||
"inflection": "^1.12.0", | ||
@@ -62,3 +63,3 @@ "lodash": "^4.17.11" | ||
}, | ||
"gitHead": "90207850effbf1743dd90f19418ec75b887753b9" | ||
"gitHead": "67b0925c3f793d6225ba2e89c944053f71948484" | ||
} |
@@ -0,2 +1,17 @@ | ||
import { | ||
GraphQLFieldConfigMap, | ||
GraphQLID, | ||
GraphQLInputObjectType, | ||
GraphQLInt, | ||
GraphQLInterfaceType, | ||
GraphQLList, | ||
GraphQLNonNull, | ||
GraphQLObjectType, | ||
GraphQLType, | ||
Thunk, | ||
} from 'graphql' | ||
import { createArgsFields } from './args-filter-generator' | ||
import { applyDefaultConfiguration, GeneratorConfiguration, PartialGeneratorConfiguration } from './configuration' | ||
import { ModelFetcher, mutationFieldReducer, queryFieldReducer, subscriptionFieldReducer } from './field-reducers' | ||
import { | ||
@@ -11,3 +26,3 @@ applyFilterMapper, | ||
import { ListType, NodeType, PageInputType, PageType } from './generic-types' | ||
import { AnyModel, Attribute, ExtendedModel, GenericField, Model } from './model' | ||
import { AnyModel, Attribute, ExtendedModel, GenericField, Model, ModelFields } from './model' | ||
import { GeneratedModelMapper } from './model-mapper' | ||
@@ -18,26 +33,12 @@ import { defaultNamingStrategy, Names, NamingStrategy } from './naming-strategy' | ||
import { | ||
GraphQLID, | ||
GraphQLInputObjectType, | ||
GraphQLInt, | ||
GraphQLInterfaceType, | ||
GraphQLList, | ||
GraphQLNonNull, | ||
GraphQLObjectType, | ||
GraphQLType, | ||
} from 'graphql' | ||
import { applyDefaultConfiguration, GeneratorConfiguration, PartialGeneratorConfiguration } from './configuration' | ||
import { ModelFetcher, mutationFieldReducer, queryFieldReducer } from './field-reducers' | ||
export type ModelList<Types, Models> = Array<AnyModel<Types, Models>> | ||
export interface BaseSchema { | ||
queryFields: any | ||
mutationFields?: any | ||
subscriptionFields?: any | ||
export interface BaseSchema<Models> { | ||
getModel: (modelName: keyof Models) => ModelFields | ||
queryFields: Thunk<GraphQLFieldConfigMap<any, any>> | ||
mutationFields?: Thunk<GraphQLFieldConfigMap<any, any>> | ||
subscriptionFields?: Thunk<GraphQLFieldConfigMap<any, any>> | ||
} | ||
export type BaseSchemaGenerator<Types, Models> = (models: Models) => BaseSchema | ||
export type BaseSchemaGenerator<Types, Models> = (models: Models) => BaseSchema<Models> | ||
@@ -174,4 +175,6 @@ const extendModelReducer = <Types, Models>(configuration: GeneratorConfiguration<Types, Models>, models: Models) => < | ||
return { | ||
getModel: name => getModel(name).types, | ||
queryFields: modelNames.reduce(queryFieldReducer(getModel, configuration), {}), | ||
mutationFields: modelNames.reduce(mutationFieldReducer(getModel, configuration), {}), | ||
subscriptionFields: modelNames.reduce(subscriptionFieldReducer(getModel, configuration), {}), | ||
} | ||
@@ -178,0 +181,0 @@ } |
@@ -7,2 +7,4 @@ import { createDefaultFilterParser, defaultFilterMapper, FilterMapper, FilterParser } from './filter-field-generator' | ||
import { PubSub } from 'graphql-subscriptions' | ||
export interface GeneratorConfiguration<Types, Models> extends PartialGeneratorConfiguration<Types, Models> { | ||
@@ -20,2 +22,3 @@ modelMapper: GeneratedModelMapper<Types, Models> | ||
orderMapper?: OrderMapper<any> | ||
pubSub?: PubSub | ||
} | ||
@@ -30,3 +33,4 @@ | ||
orderMapper: defaultOrderMapper, | ||
pubSub: new PubSub(), | ||
...configuration, | ||
}) |
@@ -1,2 +0,2 @@ | ||
import { GraphQLList, GraphQLNonNull } from 'graphql' | ||
import { GraphQLFieldConfigMap, GraphQLList, GraphQLNonNull, Thunk } from 'graphql' | ||
import { GeneratorConfiguration } from './configuration' | ||
@@ -7,3 +7,5 @@ import { applyFilterParser } from './filter-field-generator' | ||
export type Fields = Thunk<GraphQLFieldConfigMap<any, any>> | ||
export type ModelFetcher<Types, Models> = (modelName: keyof Models) => ExtendedModel<Types, Models> | ||
export type FieldReducer<Types, Models> = (fields: Fields, name: keyof Models) => Fields | ||
@@ -13,3 +15,3 @@ export const queryFieldReducer = <Types, Models>( | ||
configuration: GeneratorConfiguration<Types, Models>, | ||
) => (queryFields, name) => { | ||
): FieldReducer<Types, Models> => (queryFields, name) => { | ||
const model = getModel(name) | ||
@@ -71,4 +73,4 @@ const names = configuration.namingStrategy(name) | ||
args: { [names.arguments.data]: { type: model.argsFields.create } }, | ||
resolve: (_, { data }, context, info) => | ||
model.resolvers.create( | ||
resolve: (_, { data }, context, info) => { | ||
const node = model.resolvers.create( | ||
_, | ||
@@ -80,3 +82,6 @@ { | ||
info, | ||
), | ||
) | ||
configuration.pubSub.publish(names.events.create, { node }) | ||
return node | ||
}, | ||
type: model.types.type, | ||
@@ -90,4 +95,4 @@ } | ||
}, | ||
resolve: (_, { data, where }, context, info) => | ||
model.resolvers.update( | ||
resolve: (_, { data, where }, context, info) => { | ||
const node = model.resolvers.update( | ||
_, | ||
@@ -100,3 +105,6 @@ { | ||
info, | ||
), | ||
) | ||
configuration.pubSub.publish(names.events.update, { node }) | ||
return node | ||
}, | ||
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(model.types.type))), | ||
@@ -110,4 +118,4 @@ } | ||
}, | ||
resolve: (_, { where, order }, context, info) => | ||
model.resolvers.delete( | ||
resolve: (_, { where, order }, context, info) => { | ||
const nodes = model.resolvers.delete( | ||
_, | ||
@@ -119,3 +127,6 @@ { | ||
info, | ||
), | ||
) | ||
configuration.pubSub.publish(names.events.delete, { nodes }) | ||
return nodes | ||
}, | ||
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(model.types.type))), | ||
@@ -126,1 +137,28 @@ } | ||
} | ||
export const subscriptionFieldReducer = <Types, Models>( | ||
getModel: ModelFetcher<Types, Models>, | ||
configuration: GeneratorConfiguration<Types, Models>, | ||
) => (subscriptionFields, name) => { | ||
const model = getModel(name) | ||
const names = configuration.namingStrategy(name) | ||
const filterParser = applyFilterParser(configuration.filterParser)(model) | ||
subscriptionFields[names.events.create] = { | ||
subscribe: () => configuration.pubSub.asyncIterator(names.events.create), | ||
resolve: ({ node }) => node, | ||
type: GraphQLNonNull(model.types.type), | ||
} | ||
subscriptionFields[names.events.delete] = { | ||
subscribe: () => configuration.pubSub.asyncIterator(names.events.delete), | ||
resolve: ({ nodes }) => nodes, | ||
type: GraphQLNonNull(GraphQLList(GraphQLNonNull(model.types.type))), | ||
} | ||
subscriptionFields[names.events.update] = { | ||
subscribe: () => configuration.pubSub.asyncIterator(names.events.update), | ||
resolve: ({ node }) => node, | ||
type: model.types.type, | ||
} | ||
return subscriptionFields | ||
} |
import { capitalize, pluralize, singularize } from 'inflection' | ||
export interface Names { | ||
arguments: Record<'data' | 'filter' | 'order' | 'page' | 'where', string> | ||
events: Record<'create' | 'delete' | 'update', string> | ||
fields: Record<'create' | 'delete' | 'findMany' | 'findOne' | 'update', string> | ||
arguments: Record<'data' | 'filter' | 'order' | 'page' | 'where', string> | ||
types: Record<'createType' | 'dataType' | 'filterType' | 'orderType' | 'whereType', string> | ||
@@ -11,2 +12,14 @@ } | ||
export const defaultNamingStrategy: NamingStrategy = name => ({ | ||
arguments: { | ||
data: 'data', | ||
order: 'order', | ||
filter: 'filter', | ||
page: 'page', | ||
where: 'where', | ||
}, | ||
events: { | ||
create: `onCreate${singularize(name.toString())}`, | ||
delete: `onDelete${pluralize(name.toString())}`, | ||
update: `onUpdate${singularize(name.toString())}`, | ||
}, | ||
fields: { | ||
@@ -19,9 +32,2 @@ create: `create${singularize(name.toString())}`, | ||
}, | ||
arguments: { | ||
data: 'data', | ||
order: 'order', | ||
filter: 'filter', | ||
page: 'page', | ||
where: 'where', | ||
}, | ||
types: { | ||
@@ -28,0 +34,0 @@ createType: `Create${singularize(name.toString())}Data`, |
@@ -12,12 +12,20 @@ import { GraphQLObjectType, GraphQLSchema, printSchema } from 'graphql' | ||
export const createSchema = ({ queryFields, mutationFields }: BaseSchema) => | ||
new GraphQLSchema({ | ||
query: new GraphQLObjectType({ | ||
name: 'Query', | ||
fields: queryFields, | ||
}), | ||
mutation: new GraphQLObjectType({ | ||
name: 'Mutation', | ||
fields: mutationFields, | ||
}), | ||
export const createSchema = <Models>({ queryFields, mutationFields, subscriptionFields }: BaseSchema<Models>) => { | ||
const query = new GraphQLObjectType({ | ||
name: 'Query', | ||
fields: queryFields, | ||
}) | ||
const mutation = mutationFields | ||
? new GraphQLObjectType({ | ||
name: 'Mutation', | ||
fields: mutationFields, | ||
}) | ||
: null | ||
const subscription = subscriptionFields | ||
? new GraphQLObjectType({ | ||
name: 'Subscription', | ||
fields: subscriptionFields, | ||
}) | ||
: null | ||
return new GraphQLSchema({ query, mutation, subscription }) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
142042
2395
6
+ Addedgraphql-subscriptions@^1.0.0
+ Addedgraphql@15.10.1(transitive)
+ Addedgraphql-subscriptions@1.2.1(transitive)
+ Addediterall@1.3.0(transitive)