Skip to content
This repository has been archived by the owner on Dec 17, 2018. It is now read-only.

Commit

Permalink
refactor(*): Remove express wrapper, change default package export to…
Browse files Browse the repository at this point in the history
… express route handler

Removed the express wrapper around the package. Replaced the main export of the package with the
configured express-graphql instance so that this package can be installed into othr connect-based
packages without enforcing the use of express, or the authentication methods previously used.

BREAKING CHANGE: Removed express boilerplate. Package now only exposes GraphQL route handler.
  • Loading branch information
Simon Wears committed Mar 26, 2017
1 parent 03c2abc commit 960aa73
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 1,220 deletions.
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "healthgraph-middleware",
"name": "healthgraphql",
"version": "0.0.0",
"description": "Healthgraph API middleware",
"main": "src/app.js",
"description": "GraphQL route handler for accessing the Healthgraph API",
"main": "src/healthgrahql.js",
"license": "MIT",
"config": {
"commitizen": {
Expand All @@ -16,25 +16,22 @@
"cz-conventional-changelog": "^2.0.0",
"eslint": "^3.17.1",
"eslint-config-degree53-core": "^3.0.11",
"eslint-config-degree53-es6": "^3.0.1",
"nodemon": "^1.11.0"
"eslint-config-degree53-es6": "^3.0.1"
},
"dependencies": {
"cookie-parser": "^1.4.3",
"dataloader": "^1.3.0",
"ejs": "^2.5.6",
"express": "^4.15.2",
"express-graphql": "^0.6.3",
"graphql": "^0.9.1",
"graphql-relay": "^0.5.1",
"node-polyglot": "^2.2.2",
"superagent": "^3.5.0",
"superagent-promise": "^1.1.0",
"winston": "^2.3.1"
"superagent-promise": "^1.1.0"
},
"engines": {
"node": ">6.10.0"
},
"scripts": {
"lint": "eslint src/",
"start": "nodemon src/app.js"
"lint": "eslint src/"
},
"eslintConfig": {
"env": {
Expand Down
35 changes: 0 additions & 35 deletions src/app.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/data-types/node-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const nodeDefinitions = require('graphql-relay').nodeDefinitions;

const { nodeInterface, nodeField } = nodeDefinitions(
(globalId, context) => {
const { type, id } = fromGlobalId(globalId);
const { id } = fromGlobalId(globalId);
return context.healthGraphLoader.load(id);
},
object => {
Expand Down
51 changes: 51 additions & 0 deletions src/healthgraphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const graphqlHTTP = require('express-graphql');
const graphql = require('graphql');
const GraphQLSchema = graphql.GraphQLSchema;
const UserType = require('data-types/user');
const createLoader = require('helpers/healthgraph-loader');
const createStrengthItem = require('mutations/strength-training/create-item');
const deleteStrengthItem = require('mutations/strength-training/delete-item');
const editStrengthItem = require('mutations/strength-training/edit-item');

const DEFAULTS = {
getAccessToken () { },
graphiql: false
};

/**
*
* @param {object} options - Configuration object
* @param {boolean} [options.graphiql=false] - Enable graphiql interface
* @param {function} options.getAccessToken - Function to get the access token to be used for the request to the Healthgraph
* @returns {*}
*/
module.exports = function (options = {}) {

const opts = Object.assign(DEFAULTS, options);

return graphqlHTTP(req => {

const access_token = opts.getAccessToken(req);
const healthGraphLoader = createLoader(access_token);

return {
context: {
access_token,
healthGraphLoader
},
graphiql: opts.graphiql,
schema: new GraphQLSchema({
query: UserType,
mutation: new graphql.GraphQLObjectType({
name: 'healthgraph',
fields: {
create_strength_activity: createStrengthItem,
delete_strength_activity: deleteStrengthItem,
edit_strength_activity: editStrengthItem
}
})
})
}
});

};
3 changes: 0 additions & 3 deletions src/helpers/healthgraph-loader.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
const request = require('superagent-promise')(require('superagent'), Promise);
const DataLoader = require('dataloader');
const log = require('./logging');
const HEALTHGRAPH = require('../constants/healthgraph');

module.exports = function createLoader (access_token) {

return new DataLoader(
urls => Promise.all(urls.map(uri => {

log.info('HEALTHGRAPH_REQUEST', {uri});

return request
.get(`${HEALTHGRAPH.BASE_URL}${uri}`)
.query({
Expand Down
30 changes: 0 additions & 30 deletions src/helpers/logging.js

This file was deleted.

44 changes: 0 additions & 44 deletions src/routes/auth.js

This file was deleted.

38 changes: 0 additions & 38 deletions src/routes/graphql.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/routes/index.js

This file was deleted.

14 changes: 0 additions & 14 deletions src/templates/access-denied.ejs

This file was deleted.

14 changes: 0 additions & 14 deletions src/templates/index.ejs

This file was deleted.

Loading

0 comments on commit 960aa73

Please sign in to comment.