diff --git a/src/packages/graphql-interface/data-object.js b/src/packages/graphql-interface/data-object.js deleted file mode 100644 index 101faa5..0000000 --- a/src/packages/graphql-interface/data-object.js +++ /dev/null @@ -1,126 +0,0 @@ -"use strict"; - -const Promise = require("bluebird"); -const objectFromEntries = require("object.fromentries"); -const util = require("util"); - -function resolveFromDataSource(dataContext, dataSource, id) { - if (dataContext[dataSource] != null) { - return dataContext[dataSource].load(id); - } else { - throw new Error(`Specified data source '${dataSource}' does not exist`); - } -} - -function withProperty(dataSource, id, property) { - return withData(dataSource, id, (value) => { - return value[property]; - }); -} - -function withData(dataSource, id, callback) { - return function (args, context) { - let { data } = context; - - return Promise.try(() => { - return resolveFromDataSource(data, dataSource, id); - }).then((value) => { - if (value != null) { - // FIXME: Inject 'properties' - return callback(value, args, context); - } else { - // QUESTION: Why do we disallow this again? - throw new Error(`Got a null-ish value from data source '${dataSource}' for ID '${util.inspect(id)}'`); - } - }); - }; -} - -function withDynamicHandler(handler, object) { - return function (args, context) { - let { data } = context; - - function resolveProperty(property, fromObject = object) { - if (typeof fromObject[property] !== "function") { - throw new Error(`FIXME: Properties can apparently be non-functions`); - } - - return fromObject[property](args, context); - } - - let extendedContext = { - ... context, - resolveProperty: resolveProperty, - resolveProperties: function (properties, fromObject) { - return Promise.map(properties, (property) => { - return Promise.try(() => { - return resolveProperty(property, fromObject); - }).then((value) => { - return [ property, value ]; - }); - }).then((entries) => { - return objectFromEntries(entries); - }); - }, - resolvePropertyPath: function (propertyPath, fromObject) { - let initialObject = fromObject ?? object; - - return Promise.reduce(propertyPath, (last, property) => { - if (last != null) { - return resolveProperty(property, last); - } - }, initialObject); - }, - resolveDataSource: function (dataSource, id) { - return resolveFromDataSource(data, dataSource, id); - } - }; - - return handler(args, extendedContext); - }; -} - -let ID = Symbol("ID"); -let LocalProperties = Symbol("LocalProperties"); -let Dynamic = Symbol("Dynamic"); - -module.exports = { - ID: ID, - Dynamic: Dynamic, - LocalProperties: LocalProperties, - createDataObject: function createDataObject(mappings) { - let object = {}; - - if (mappings[LocalProperties] != null) { - Object.assign(object, mappings[LocalProperties]); - } - - if (mappings[Dynamic] != null) { - for (let [property, handler] of Object.entries(mappings[Dynamic])) { - object[property] = withDynamicHandler(handler, object); - } - } - - for (let [dataSource, items] of Object.entries(mappings)) { - if (items[ID] != null) { - let id = items[ID]; - - for (let [property, source] of Object.entries(items)) { - if (object[property] == null) { - if (typeof source === "string") { - object[property] = withProperty(dataSource, id, source); - } else if (typeof source === "function") { - object[property] = withData(dataSource, id, source); - } /* FIXME: else */ - } else { - throw new Error(`Handler already defined for property '${property}' - maybe you specified it twice for different data sources?`); - } - } - } else { - throw new Error(`No object ID was provided for the '${dataSource}' data source`); - } - } - - return object; - } -}; diff --git a/src/packages/graphql-interface/index.js b/src/packages/graphql-interface/index.js deleted file mode 100644 index 3cab603..0000000 --- a/src/packages/graphql-interface/index.js +++ /dev/null @@ -1,17 +0,0 @@ -"use strict"; - -const graphql = require("graphql"); - -module.exports = function createGraphQLInterface(schema, options, root) { - return function makeQuery(query, args) { - return graphql.graphql({ - schema: schema, - source: query, - rootValue: root, - contextValue: { - data: (options.loaderFactory != null) ? options.loaderFactory() : {} - }, - variableValues: args - }); - }; -}; diff --git a/src/packages/graphql-interface/tag.js b/src/packages/graphql-interface/tag.js deleted file mode 100644 index 626a38c..0000000 --- a/src/packages/graphql-interface/tag.js +++ /dev/null @@ -1,5 +0,0 @@ -"use strict"; - -module.exports = function gql(strings) { - return strings.join(""); -}; \ No newline at end of file diff --git a/src/packages/graphql-interface/type-loader.js b/src/packages/graphql-interface/type-loader.js deleted file mode 100644 index 06d96c9..0000000 --- a/src/packages/graphql-interface/type-loader.js +++ /dev/null @@ -1,11 +0,0 @@ -"use strict"; - -module.exports = function loadTypes(types) { - let loadedTypes = {}; - - for (let [name, module_] of Object.entries(types)) { - loadedTypes[name] = module_(loadedTypes); - } - - return loadedTypes; -}; \ No newline at end of file