Skip to content

Commit

Permalink
Added graphi and graphql to project (part1).
Browse files Browse the repository at this point in the history
    * Implement example from graphi project.
      project:  https://github.com/geek/graphi
    * Graphi options passed through confidence object
    * test coverage
    * sources: https://github.com/geek/graphi and http://graphql.org
  • Loading branch information
zoe-1 committed Jan 19, 2018
1 parent c147d0d commit 1970999
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ internals.config = {
}
}]
}
},
graphi: {
$filter: 'env',
production: {
graphiqlPath: false,
authStrategy: 'default'
},
test: {
graphiqlPath: false,
authStrategy: 'default'
},
$default: {
authStrategy: false, // @todo beef up graphi's validations.
graphiqlPath: './graphqli'
}
}
}
};
Expand Down
40 changes: 40 additions & 0 deletions lib/graphi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const Graphi = require('graphi');


const schema = `
type Person {
firstname: String!
lastname: String!
}
type Query {
person(firstname: String!): Person!
}
`;

const getPerson = function (args, request) {

return new Promise((resolve) => {

resolve({ firstname: 'billy', lastname: 'jean' });
});
};

const resolvers = {
person: getPerson
};


exports.plugin = {
name: 'graphi-registeration',
version: '1.0.0',
description: 'register graphi and build schema.',
register: async function (server, options) {

await server.register({ plugin: Graphi, options: { schema, resolvers, authStrategy: options.authStrategy, grahpiqlPath: options.grahpiqlPath } });
}
};

// open https://localhost:8000/graphiql?query=%7B%20person(firstname%3A%20%22billy%22)%20%7B%20lastname%20%7D%20%7D&variables=%7B%7D
// curl -k -X POST -H "Content-Type: application/json" -d '{"query":"{person(firstname:\"billy\"){lastname}}"}' https://127.0.0.1:8000/graphql
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const HapiAuthBearerToken = require('hapi-auth-bearer-token');
const AuthTokenStrategy = require('./authtoken');
const Cache = require('./cache');
const Good = require('good');
const Graphi = require('./graphi');
const Config = require('./config');

// route plugins
Expand All @@ -31,7 +32,8 @@ internals.init = async (environment) => {
{ plugin: Version, options: {} },
{ plugin: User, options: {} },
{ plugin: Cache, options: { expiresIn: options.plugins.authToken.expiresIn } },
{ plugin: Good, options: options.plugins.good }
{ plugin: Good, options: options.plugins.good },
{ plugin: Graphi, options: options.plugins.graphi }
];

const server = new Hapi.Server(options.server);
Expand Down
44 changes: 43 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"good": "^8.0.0-rc1",
"good-file": "^6.0.1",
"good-squeeze": "5.x.x",
"graphi": "^5.3.0",
"hapi": "17.x.x",
"hapi-auth-bearer-token": "6.x.x",
"hoek": "^5.0.2"
Expand Down
37 changes: 37 additions & 0 deletions test/graphi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const { expect } = require('code');
const Lab = require('lab');
const { suite, test } = exports.lab = Lab.script();


suite('graphi development', () => {

test('search returns last name', async () => {

const University = require('../lib');

const server = await University.init('test');

expect(server).to.be.an.object();

const authenticateRequest = { method: 'POST', url: '/authenticate', payload: { username: 'foofoo', password: '12345678' } };

const authRes = await server.inject(authenticateRequest);

expect(authRes.result.token.length).to.equal(36);

const request = { method: 'POST', url: '/graphql', headers: { authorization: 'Bearer ' + authRes.result.token }, payload: { query:'{person(firstname:\"billy\"){lastname}}' } };

const res = await server.inject(request);

expect(JSON.parse(res.payload)).to.equal({
'data':
{ 'person':
{ 'lastname': 'jean' }
}
});

await server.stop({ timeout: 4 });
});
});

0 comments on commit 1970999

Please sign in to comment.