Skip to content

Commit

Permalink
Add a commondatabuilder tool (#501)
Browse files Browse the repository at this point in the history
* Add a commondatabuilder tool.

* Add a filterOut option to choice configs.

* Add commondatabuilder.js smoke test.

* ok apollo codegen reverted its regression or something

* pwaaiejgpojeg

* Don't use enums, they are super confusing

* Um, upgrade apollo i guess?

* Run querybuilder.js --force.

* Tell users to pass --force to querybuilder on test failure.

* Convert issue choices to use typescript files.

* Migrate lease choices to typescript.

* Move functions to commondatabuilder.

* Rename enumName to typeName.

* Appease codeclimate.

* Add docs.

* Add test to ensure common data is synced.

* Add more docs.
  • Loading branch information
toolness authored Mar 5, 2019
1 parent 963ea54 commit 0bf802f
Show file tree
Hide file tree
Showing 32 changed files with 1,060 additions and 339 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ jobs:
command: |
node --version
# This is intended as a smoke test.
# These are intended as smoke tests.
node querybuilder.js --version
node commondatabuilder.js --version
npm run build
npm test -- --runInBand
Expand Down
2 changes: 2 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ exclude_patterns:
- "frontend/lib/queries/*"
# These are auto-generated by Django, ignore them.
- "**/migrations/*"
# These are auto-generated by commondatabuilder, ignore them.
- "common-data/*-choices.ts"

checks:
similar-code:
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,28 @@ For the Node front-end:
other value for development.
* See [frontend/webpack/webpack-defined-globals.d.ts](frontend/webpack/webpack-defined-globals.d.ts) for more values.

## Common data

Some data that is shared between the front-end and back-end is
in the [`common-data/`](common-data/) directory. The
back-end generally reads this data in JSON format, while the
front-end reads a TypeScript file that is generated from
the JSON.

A utility called `commondatabuilder` is used to generate the
TypeScript file. To execute it, run:

```
node commondatabuilder.js
```

You will need to run this whenever you make any changes to
the underlying JSON files.

If you need to add a new common data file, see
[`common-data/config.ts`](common-data/config.ts), which
defines how the conversion from JSON to TypeScript occurs.

## GraphQL

The communication between server and client occurs via [GraphQL][]
Expand Down
32 changes: 32 additions & 0 deletions common-data/borough-choices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file was auto-generated by commondatabuilder.
// Please don't edit it.

export type BoroughChoice = "BROOKLYN"|"QUEENS"|"BRONX"|"MANHATTAN"|"STATEN_ISLAND";

export const BoroughChoices: BoroughChoice[] = [
"BROOKLYN",
"QUEENS",
"BRONX",
"MANHATTAN",
"STATEN_ISLAND"
];

const BoroughChoiceSet: Set<String> = new Set(BoroughChoices);

export function isBoroughChoice(choice: string): choice is BoroughChoice {
return BoroughChoiceSet.has(choice);
}

export type BoroughChoiceLabels = {
[k in BoroughChoice]: string;
};

export function getBoroughChoiceLabels(): BoroughChoiceLabels {
return {
BROOKLYN: "Brooklyn",
QUEENS: "Queens",
BRONX: "Bronx",
MANHATTAN: "Manhattan",
STATEN_ISLAND: "Staten Island",
};
}
32 changes: 32 additions & 0 deletions common-data/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DjangoChoicesTypescriptConfig } from '../frontend/commondatabuilder/commondatabuilder';

const config: DjangoChoicesTypescriptConfig = {
rootDir: __dirname,
files: [
{
jsonFilename: 'issue-choices.json',
typeName: 'IssueChoice',
exportLabels: true,
filterOut: /^LANDLORD__/
},
{
jsonFilename: 'issue-area-choices.json',
typeName: 'IssueAreaChoice',
exportLabels: true,
filterOut: ['LANDLORD']
},
{
jsonFilename: 'borough-choices.json',
typeName: 'BoroughChoice',
exportLabels: true,
},
{
jsonFilename: 'lease-choices.json',
typeName: 'LeaseChoice',
exportLabels: true,
filterOut: ['NOT_SURE']
}
]
};

export default config;
34 changes: 34 additions & 0 deletions common-data/issue-area-choices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This file was auto-generated by commondatabuilder.
// Please don't edit it.

export type IssueAreaChoice = "HOME"|"BEDROOMS"|"KITCHEN"|"LIVING_ROOM"|"BATHROOMS"|"PUBLIC_AREAS";

export const IssueAreaChoices: IssueAreaChoice[] = [
"HOME",
"BEDROOMS",
"KITCHEN",
"LIVING_ROOM",
"BATHROOMS",
"PUBLIC_AREAS"
];

const IssueAreaChoiceSet: Set<String> = new Set(IssueAreaChoices);

export function isIssueAreaChoice(choice: string): choice is IssueAreaChoice {
return IssueAreaChoiceSet.has(choice);
}

export type IssueAreaChoiceLabels = {
[k in IssueAreaChoice]: string;
};

export function getIssueAreaChoiceLabels(): IssueAreaChoiceLabels {
return {
HOME: "Entire home and hallways",
BEDROOMS: "Bedrooms",
KITCHEN: "Kitchen",
LIVING_ROOM: "Living room",
BATHROOMS: "Bathrooms",
PUBLIC_AREAS: "Public areas",
};
}
Loading

0 comments on commit 0bf802f

Please sign in to comment.