Skip to content

Commit

Permalink
js fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
austinhallock committed Jun 11, 2020
1 parent f064808 commit 74f0cee
Show file tree
Hide file tree
Showing 23 changed files with 870 additions and 917 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12.16.1-buster
FROM node:14.4.0

RUN apt-get update && apt-get install -y gdal-bin git python python-pip graphicsmagick imagemagick libcairo2-dev libjpeg-dev libpango1.0-dev libgif-dev librsvg2-dev build-essential

Expand Down
13 changes: 3 additions & 10 deletions Gulpfile.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
import gulp from 'gulp'
import { spawn } from 'child_process'

Expand All @@ -11,21 +9,16 @@ const paths = {
]
}

gulp.task('default', 'dev')

gulp.task('dev', 'watch:dev')

gulp.task('watch:dev', gulp.series('dev:server', () => gulp.watch(paths.js, ['dev:server']))
)

let devServer = null
gulp.task('dev:server', function () {
process.on('exit', () => devServer?.kill())
devServer && devServer.kill()
devServer = spawn('js', [paths.serverBin], { stdio: 'inherit' })
devServer = spawn('node', [paths.serverBin], { stdio: 'inherit' })
return devServer.on('close', function (code) {
if (code === 8) {
return gulp.log('Error detected, waiting for changes')
}
})
})

gulp.task('dev', gulp.parallel('dev:server', () => gulp.watch(paths.js, gulp.series('dev:server'))))
21 changes: 11 additions & 10 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import log from 'loga'
import cluster from 'cluster'
import os from 'os'
import _ from 'lodash'

import { setup, childSetup, server } from '../index.js'
import { setup, childSetup, serverPromise } from '../index.js'
import config from '../config.js'

if (config.ENV === config.ENVS.PROD) {
Expand All @@ -17,22 +16,24 @@ if (config.ENV === config.ENVS.PROD) {
})

return cluster.on('exit', function (worker) {
log(`Worker ${worker.id} died, respawning`)
console.log(`Worker ${worker.id} died, respawning`)
return cluster.fork()
})
}).catch(log.error)
}).catch(console.log)
} else {
childSetup().then(() =>
childSetup().then(async () => {
const server = await serverPromise
server.listen(config.PORT, () =>
log.info('Worker %d, listening on %d', cluster.worker.id, config.PORT)
console.log('Worker %d, listening on %d', cluster.worker.id, config.PORT)
)
)
})
}
} else {
console.log('Setting up')
setup().then(() =>
setup().then(async () => {
const server = await serverPromise
server.listen(config.PORT, () =>
log.info('Server listening on port %d', config.PORT)
console.log('Server listening on port %d', config.PORT)
)
).catch(log.error)
}).catch(console.log)
}
9 changes: 0 additions & 9 deletions bulk-decaffeinate.config.js

This file was deleted.

2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
import _ from 'lodash'
import assertNoneMissing from 'assert-none-missing'
import { assertNoneMissing } from 'backend-shared'

const {
env
Expand Down
7 changes: 5 additions & 2 deletions graphql/directives.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Format } from 'backend-shared'
import { defaultFieldResolver } from 'graphql'
import { SchemaDirectiveVisitor } from 'graphql-tools'
import GraphQL from 'graphql'
import GraphQLTools from 'graphql-tools'

const { defaultFieldResolver } = GraphQL
const { SchemaDirectiveVisitor } = GraphQLTools

export const nameCase = class NameCase extends SchemaDirectiveVisitor {
visitFieldDefinition (field) {
Expand Down
24 changes: 1 addition & 23 deletions graphql/irs_person/model.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,7 @@
/* eslint-disable
constructor-super,
no-constant-condition,
no-eval,
no-this-before-super,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
import _ from 'lodash'
import { Base, cknex, elasticsearch } from 'backend-shared'
import config from '../../config'
import { Base, cknex } from 'backend-shared'

class IrsPersonModel extends Base {
constructor (...args) {
{
// Hack: trick Babel/TypeScript into allowing this before super.
if (false) { super() }
const thisFn = (() => { return this }).toString()
const thisName = thisFn.match(/return (?:_assertThisInitialized\()*(\w+)\)*;/)[1]
eval(`${thisName} = this;`)
}
this.getAllByEin = this.getAllByEin.bind(this)
super(...args)
}

getScyllaTables () {
return [
{
Expand Down
5 changes: 2 additions & 3 deletions graphql/irs_person/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// TODO: This file was created by bulk-decaffeinate.
// Sanity-check the conversion and remove this comment.
import { GraphqlFormatter } from 'backend-shared'
import IrsPerson from './model'

import IrsPerson from './model.js'

export const Query = {
irsPersons (rootValue, { ein, query, limit }) {
Expand Down
114 changes: 59 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
import fs from 'fs'
import _ from 'lodash'
import cors from 'cors'
import express from 'express'
import Promise from 'bluebird'
import bodyParser from 'body-parser'
import http from 'http'
import { ApolloServer } from 'apollo-server-express'
import { buildFederatedSchema } from '@apollo/federation'
import { SchemaDirectiveVisitor } from 'graphql-tools'
import ApolloServerExpress from 'apollo-server-express'
import ApolloFederation from '@apollo/federation'
import GraphQLTools from 'graphql-tools'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import { Schema, elasticsearch } from 'backend-shared'
import helperConfig from 'backend-shared/lib/config.js'

import { setup, childSetup } from './services/setup.js'
import { setNtee } from './services/irs_990_importer/set_ntee.js'
import { loadAllForYear } from './services/irs_990_importer/load_all_for_year.js'
import {
processUnprocessedOrgs, processEin, fixBadFundImports, processUnprocessedFunds
} from './services/irs_990_importer.js'
} from './services/irs_990_importer/index.js'
import { parseGrantMakingWebsites } from './services/irs_990_importer/parse_websites.js'
import IrsOrg990 from './graphql/irs_org_990/model.js'
import directives from './graphql/directives.js.js'
import * as directives from './graphql/directives.js'
import config from './config.js'

// FIXME: change to a setup fn that everything else waits on
helperConfig.set(_.pick(config, config.SHARED_WITH_PHIL_HELPERS))
const { ApolloServer } = ApolloServerExpress
const { buildFederatedSchema } = ApolloFederation
const { SchemaDirectiveVisitor } = GraphQLTools
const __dirname = dirname(fileURLToPath(import.meta.url))

let resolvers, schemaDirectives
let typeDefs = fs.readFileSync('./graphql/type.graphql', 'utf8')

let schema = Schema.getSchema({ directives, typeDefs, dirName: __dirname })
const schemaPromise = Schema.getSchema({ directives, typeDefs, dirName: __dirname })

Promise.config({ warnings: false })

Expand All @@ -50,7 +52,7 @@ app.get('/tableCount', function (req, res) {
if (validTables.indexOf(req.query.tableName) === -1) {
res.send({ error: 'invalid table name' })
}
return elasticsearch.count({
return elasticsearch.client.count({
index: req.query.tableName
})
.then(c => res.send(JSON.stringify(c)))
Expand Down Expand Up @@ -89,14 +91,14 @@ app.get('/setES', async function (req, res) {
// refreshInterval = null

return res.send(await Promise.map(validTables, async function (tableName) {
const settings = await elasticsearch.indices.getSettings({
const settings = await elasticsearch.client.indices.getSettings({
index: tableName
})
const previous = settings[tableName].settings.index
const diff =
{ number_of_replicas: replicas }
// refresh_interval: refreshInterval
await elasticsearch.indices.putSettings({
await elasticsearch.client.indices.putSettings({
index: tableName,
body: diff
})
Expand All @@ -115,7 +117,7 @@ app.get('/setMaxWindow', async function (req, res) {
res.send({ error: 'must be number between 10,000 and 100,000' })
}

return res.send(await elasticsearch.indices.putSettings({
return res.send(await elasticsearch.client.indices.putSettings({
index: req.query.tableName,
body: { max_result_window: maxResultWindow }
}))
Expand Down Expand Up @@ -183,50 +185,52 @@ app.get('/processUnprocessedFunds', function (req, res) {
app.get('/parseGrantMakingWebsites', function (req, res) {
parseGrantMakingWebsites()
return res.send('syncing')
});

({ typeDefs, resolvers, schemaDirectives } = schema)
schema = buildFederatedSchema({ typeDefs, resolvers })
// https://github.com/apollographql/apollo-feature-requests/issues/145
SchemaDirectiveVisitor.visitSchemaDirectives(schema, schemaDirectives)

const defaultQuery = `
query($query: ESQuery!) {
irsOrgs(query: $query) {
nodes {
name
employeeCount
volunteerCount
})

const serverPromise = schemaPromise.then((schema) => {
({ typeDefs, resolvers, schemaDirectives } = schema)
schema = buildFederatedSchema({ typeDefs, resolvers })
// https://github.com/apollographql/apollo-feature-requests/issues/145
SchemaDirectiveVisitor.visitSchemaDirectives(schema, schemaDirectives)

const defaultQuery = `
query($query: ESQuery!) {
irsOrgs(query: $query) {
nodes {
name
employeeCount
volunteerCount
}
}
}
}
`

const defaultQueryVariables = `
{
"query": {"range": {"volunteerCount": {"gte": 10000}}}
}
`

const graphqlServer = new ApolloServer({
schema,
introspection: true,
playground: {
// settings:
tabs: [
{
endpoint: config.ENV === config.ENVS.DEV
? `http://localhost:${config.PORT}/graphql`
: 'https://api.techby.org/990/v1/graphql',
query: defaultQuery,
variables: defaultQueryVariables
}
]
`

const defaultQueryVariables = `
{
"query": {"range": {"volunteerCount": {"gte": 10000}}}
}
`

const graphqlServer = new ApolloServer({
schema,
introspection: true,
playground: {
// settings:
tabs: [
{
endpoint: config.ENV === config.ENVS.DEV
? `http://localhost:${config.PORT}/graphql`
: 'https://api.techby.org/990/v1/graphql',
query: defaultQuery,
variables: defaultQueryVariables
}
]
}

})
graphqlServer.applyMiddleware({ app, path: '/graphql' })
})
graphqlServer.applyMiddleware({ app, path: '/graphql' })

const server = http.createServer(app)
return http.createServer(app)
})

export { server, setup, childSetup }
export { serverPromise, setup, childSetup }
19 changes: 8 additions & 11 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"description": "",
"main": "index.js",
"type": "module",
"directories": {
"test": "test"
},
Expand All @@ -28,7 +29,7 @@
"dependencies": {
"@apollo/federation": "^0.16.0",
"apollo-server-express": "^2.14.2",
"assert-none-missing": "^0.1.2",
"assert-none-missing": "github:claydotio/assert-none-missing#es",
"backend-shared": "file:backend-shared",
"bluebird": "^3.5.3",
"body-parser": "^1.14.1",
Expand Down
2 changes: 1 addition & 1 deletion services/cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PREFIXES = {
export default {
EIN_FROM_NAME: 'ein:name',
ENTITY_ID: 'entity:id2',
ENTITY_SLUG: 'entity:slug1'
Expand Down
Loading

0 comments on commit 74f0cee

Please sign in to comment.