From 8808b8e88bff57665ff6bf9a066def410ada1817 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 23 Jan 2018 19:11:09 -0800 Subject: graphql endpoint --- app.js | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'app.js') diff --git a/app.js b/app.js index 68bfcffb..940a4600 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,40 @@ import passport from './passport' import cache from './cache' import dotenv from 'dotenv' import ba from 'bitcoinaverage' +import fs from 'fs' +import grpc from 'grpc' +import { graphqlExpress, graphiqlExpress } from 'apollo-server-express' +const { makeExecutableSchema } = require('graphql-tools'); + +// Some fake data +const books = [ + { + title: "Harry Potter and the Sorcerer's stone", + author: 'J.K. Rowling', + }, + { + title: 'Jurassic Park', + author: 'Michael Crichton', + }, +]; + +// The GraphQL schema in string form +const typeDefs = ` + type Query { books: [Book] } + type Book { title: String, author: String } +`; + +// The resolvers +const resolvers = { + Query: { books: () => books }, +}; + +// Put together a schema +const schema = makeExecutableSchema({ + typeDefs, + resolvers, +}); + dotenv.config() require('dotenv').config() @@ -21,7 +55,11 @@ const l = console.log const users = require('./routes/users') const transactions = require('./routes/transactions') const restClient = ba.restfulClient(process.env.BITCOINAVERAGE_PUBLIC, process.env.BITCOINAVERAGE_SECRET) - const lnrpc = await require('lnrpc')({ server: 'localhost:10009' }) + const lnrpc = await require('lnrpc')({ server: 'localhost:10001' }) + const adminMacaroon = fs.readFileSync('/home/adam/.lnd/data/core_test/admin.macaroon'); + const meta = new grpc.Metadata(); + meta.add('macaroon', adminMacaroon.toString('hex')); + app.get('/users.json', transactions.index) app.post('/login', users.login) @@ -32,10 +70,9 @@ const l = console.log app.get('/:user/transactions', transactions.index) app.post('/transactions/:txid', transactions.update) app['delete']('/:user/transactions/:txid', transactions['delete']) - app.get('/:user.json', users.json) app.get('/secret', passport.authenticate('jwt', { session: false }), users.secret) app.get('/balance', async (req, res) => { - res.json(await lnrpc.walletBalance({witness_only: true})) + res.json(await lnrpc.walletBalance({witness_only: true}, meta)) }) let fetchRates @@ -50,6 +87,13 @@ const l = console.log res.json(app.get('rates')) }) + app.use('/graphql', bodyParser.json(), graphqlExpress({ + schema, + context: {}, + })) + + app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' })) + app.use(function (err, req, res, next) { res.status(500) res.send('An error occurred') -- cgit v1.2.3