From 457806cad45e8a5952cabc833189f655716af835 Mon Sep 17 00:00:00 2001 From: Dylan Simowitz Date: Mon, 12 Nov 2018 13:38:52 -0800 Subject: [PATCH] Add create method --- lib/index.js | 102 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/lib/index.js b/lib/index.js index 5592e20..bcadc07 100755 --- a/lib/index.js +++ b/lib/index.js @@ -9,7 +9,7 @@ const runTime = require('./run-time'); // Create the service. class Service { - constructor (options = {}) { + constructor(options = {}) { this._options = Object.assign({}, options, runTime); // Define options & convenience methods. this.sqlDb = undefined; @@ -17,14 +17,15 @@ class Service { this._extraAuthProps = Array.isArray(props) ? props : [props]; } - setup (app) { + setup(app) { this._app = app; // Compile the GraphQL definition. this._schema = makeExecutableSchema({ // Add custom GraphQL scalar types typeDefs: runTime.schemaTypes + this._options.schemas, - resolvers: Object.assign({}, + resolvers: Object.assign( + {}, runTime.resolverTypes, this._options.resolvers(this._app, this._options) ) @@ -43,19 +44,15 @@ class Service { } } - find (params) { - if (!params.query || !params.query.query || typeof params.query.query !== 'string') { - throw new BadRequest('A GraphQL query requires a "params.query.query" property. (@feathers-plus/graphql)'); - } - + runQuery(query, params) { const content = { app: this._app, batchLoaders: {}, // where batchloaders are stored for this request cache: {}, // where tables used with a .filter() are cached provider: params.provider, user: params.user, - authenticated: params.authenticated, - } + authenticated: params.authenticated + }; const props = this._options.extraAuthProps || []; @@ -75,34 +72,69 @@ class Service { operationName: allows the caller to specify which operation in requestString will be run, in cases where requestString contains multiple top-level operations. */ - return graphql(this._schema, params.query.query, {}, content) - // GraphQL throws on initialization errors. It swallows others errors. - .catch(err => { - throw new BadRequest(err.message, { stack: err.stack }); - }) - .then(response => { - if (!response.errors) { - return !content.pagination ? response.data : - Object.assign({}, content.pagination, { data: response.data }); - } - - // Reformat GraphQL error for Feathers. - const errors = response.errors.map(({ locations = [], message = '', path }) => ({ - message, - line: locations.length ? locations[0].line : undefined, - column: locations.length ? locations[0].column : undefined, - path: (path || []).reduce((path, elem, i) => `${path}${i ? ':' : ''}${elem}`, '') - })); - - throw new BadRequest('GraphQL processing error. (@feathers-plus/graphql)', { - errors, - data: response.data // Return what GraphQL data is available. Likely contains 'null's. - }); - }); + return ( + graphql(this._schema, query, {}, content) + // GraphQL throws on initialization errors. It swallows others errors. + .catch(err => { + throw new BadRequest(err.message, { stack: err.stack }); + }) + .then(response => { + if (!response.errors) { + return !content.pagination + ? response.data + : Object.assign({}, content.pagination, { data: response.data }); + } + + // Reformat GraphQL error for Feathers. + const errors = response.errors.map( + ({ locations = [], message = '', path }) => ({ + message, + line: locations.length ? locations[0].line : undefined, + column: locations.length ? locations[0].column : undefined, + path: (path || []).reduce( + (path, elem, i) => `${path}${i ? ':' : ''}${elem}`, + '' + ) + }) + ); + + throw new BadRequest( + 'GraphQL processing error. (@feathers-plus/graphql)', + { + errors, + data: response.data // Return what GraphQL data is available. Likely contains 'null's. + } + ); + }) + ); + } + + create(data, params) { + if (!data.query || typeof data.query !== 'string') { + throw new BadRequest( + 'A GraphQL query requires a "data.query" property. (@feathers-plus/graphql)' + ); + } + + return this.runQuery(data.query, params); + } + + find(params) { + if ( + !params.query || + !params.query.query || + typeof params.query.query !== 'string' + ) { + throw new BadRequest( + 'A GraphQL query requires a "params.query.query" property. (@feathers-plus/graphql)' + ); + } + + return this.runQuery(params.query.query, params); } } -function moduleExports (options) { +function moduleExports(options) { return new Service(options); }