Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #12 from DylanSimowitz/master
Browse files Browse the repository at this point in the history
Add create method to support Apollo
  • Loading branch information
eddyystop authored Nov 15, 2018
2 parents e43b7c6 + 457806c commit da4e227
Showing 1 changed file with 67 additions and 35 deletions.
102 changes: 67 additions & 35 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ 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;

const props = this._options.extraAuthProps || [];
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)
)
Expand All @@ -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 || [];

Expand All @@ -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);
}

Expand Down

0 comments on commit da4e227

Please sign in to comment.