Skip to content

Commit 524daec

Browse files
committed
[Breaking] Drop Flow type support in favor of TypeScript
1 parent 66eb26a commit 524daec

37 files changed

+450
-13392
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"@babel/preset-env",
55
{
66
"targets": {
7-
"node": "10"
7+
"node": "22"
88
}
99
}
1010
],

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ npm-debug.log*
66
venv
77
yarn-debug.log*
88
yarn-error.log*
9+
.yarnrc

API_DOCS.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ To use this feature, there are several restrictions. (Please open an issue if yo
132132
```
133133
134134
2. In the response, `properties` are spread at the same level as the `responseKey`. (Check out `getFilmsV2` in [swapi example](./examples/swapi/swapi.js).)
135-
3. All `properties` must be optional in the response object. The flow types currently don't handle the nullability of these properties correctly, so to enforce this, we recommend a build step to ensure that the underlying types are always set as maybe types.
135+
3. All `properties` must be optional in the response object. The TypeScript types currently don't handle the nullability of these properties correctly, so to enforce this, we recommend a build step to ensure that the underlying types are always set as maybe types.
136136
4. The resource must have a one-to-one correspondence between the input "properties" and the output "properties".
137137
- e.g. if we request property "name", the response must have "name" in it, and no extra data associated with it.
138138

@@ -158,7 +158,7 @@ resources:
158158
responseKey: ?string (non-optional when propertyBatchKey is used)
159159
160160
typings:
161-
language: flow
161+
language: typescript
162162
embedResourcesType:
163163
imports: string
164164
ResourcesType: string
@@ -199,7 +199,7 @@ Use this to generate type definitions for the generated DataLoaders. At this tim
199199

200200
| Key | Value Description |
201201
| ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
202-
| `language` | Must be 'flow' until we support other options. |
202+
| `language` | Must be 'typescript' until we support other options. |
203203
| `embedResourcesType.imports` | Lets you inject an arbitrary import statement into the generated file, to help you write the type statement below. |
204204
| `embedResourcesType.ResourcesType` | Inject code to describe the shape of the resources object you're going to pass into `getLoaders`. Should start with `type ResourcesType = ...` |
205205
| |

Makefile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ venv: Makefile requirements-dev.txt
88
virtualenv venv --python=$(PYTHON3)
99
venv/bin/pip install -r requirements-dev.txt
1010

11-
node_modules: package.json yarn.lock
11+
node_modules: package.json
1212
yarn
1313

1414
build: node_modules
1515
yarn build
1616
# Generate the .d.ts files
1717
node_modules/.bin/tsc --project tsconfig.json --checkJs false --emitDeclarationOnly || true
18-
# TODO: Loop through everything in the lib folder to create the flow types
19-
yarn flowgen --add-flow-header lib/runtimeHelpers.d.ts --output-file lib/runtimeHelpers.js.flow
2018

2119
.PHONY: test
2220
test: build venv node_modules

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 🤖 dataloader-codegen
22

3+
## Now with TypeScript!
4+
35
[![npm](https://img.shields.io/npm/v/dataloader-codegen.svg)](https://yarn.pm/dataloader-codegen)
46
[![Build Status](https://api.travis-ci.com/Yelp/dataloader-codegen.svg?branch=master)](https://travis-ci.com/github/Yelp/dataloader-codegen)
57

@@ -14,7 +16,7 @@ Read more about the motivation behind this library in our recent blog post: http
1416
- 🚚 Supports Batched + Non Batched Resources
1517
- ✨ Predictable DataLoader Interfaces
1618
- 🐛 Error Handling
17-
- 🔒 Type Safety (Flow)
19+
- 🔒 Type Safety (TypeScript)
1820
- 🔧 Resource Middleware
1921

2022
## Install

__tests__/genType.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getResourceTypeReference, getNewKeyTypeFromBatchKeySetType, getLoaderTypeKey } from '../src/genType';
2+
3+
it('getResourceTypeReference converts a resource path to a valid reference', () => {
4+
expect(getResourceTypeReference(null, ['foo', 'bar', 'baz'])).toBe("ResourcesType['foo']['bar']['baz']");
5+
});
6+
7+
it('getNewKeyTypeFromBatchKeySetType returns a newKey type with a valid value', () => {
8+
expect(getNewKeyTypeFromBatchKeySetType('bKey', "ResourcesType['foo']['bar']['baz']")).toBe(
9+
`Parameters<ResourcesType['foo']['bar']['baz']['bKey']['has']>[0]`,
10+
);
11+
});
12+
13+
it('getLoaderTypeKey forces a nullable batchKey to be strictly non-nullable', () => {
14+
expect(
15+
getLoaderTypeKey(
16+
{
17+
isBatchResource: true,
18+
newKey: 'test_id',
19+
batchKey: 'test_ids',
20+
},
21+
['a', 'b'],
22+
),
23+
).toBe(
24+
`Omit<Parameters<ResourcesType['a']['b']>[0], 'test_ids'> & { test_id: NonNullable<Parameters<ResourcesType['a']['b']>[0]['test_ids']>[0] }`,
25+
);
26+
});

__tests__/genTypeFlow.test.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

__tests__/implementation.test.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ import { getConfig } from '../src/config';
99

1010
const BABEL_CONFIG = {
1111
presets: [
12-
'@babel/preset-flow',
12+
'@babel/preset-typescript',
1313
[
1414
'@babel/preset-env',
1515
{
1616
modules: 'commonjs',
1717
},
1818
],
1919
],
20+
// Babel needs this dummy file name to determine it's a TypeScript file
21+
filename: 'file.ts',
2022
};
2123

2224
const RUNTIME_HELPERS = path.resolve(__dirname, '..', 'src', 'runtimeHelpers.ts');
@@ -45,7 +47,7 @@ expect.extend({
4547
async function createDataLoaders(config, cb) {
4648
await tmp.withFile(
4749
async ({ path }) => {
48-
const loadersCode = await babel.transformAsync(codegen(config, RUNTIME_HELPERS), BABEL_CONFIG);
50+
const loadersCode = await babel.transformAsync(await codegen(config, RUNTIME_HELPERS), BABEL_CONFIG);
4951
fs.writeFileSync(path, 'const regeneratorRuntime = require("regenerator-runtime");');
5052
fs.appendFileSync(path, loadersCode.code);
5153
// Import the generated code into memory :scream:
@@ -1608,3 +1610,49 @@ test('batch endpoint with propertyBatchKey with reorderResultsByKey handles resp
16081610
]);
16091611
});
16101612
});
1613+
1614+
test('embeds resource types', async () => {
1615+
// For the sake of coverage we pass in these test comments which will get interpolated in the generated code
1616+
// but otherwise we don't need to test them.
1617+
const config = {
1618+
resources: {
1619+
foo: {
1620+
isBatchResource: false,
1621+
docsLink: 'example.com/docs/foo',
1622+
},
1623+
},
1624+
typings: {
1625+
embedResourcesType: {
1626+
imports: '// test foo',
1627+
ResourcesType: '// test bar',
1628+
},
1629+
},
1630+
};
1631+
1632+
const resources = {
1633+
foo: jest
1634+
.fn()
1635+
.mockReturnValueOnce(
1636+
Promise.resolve({
1637+
message: 'knock knock',
1638+
message_suffix: '!',
1639+
}),
1640+
)
1641+
.mockReturnValueOnce(
1642+
Promise.resolve({
1643+
message: "who's there",
1644+
message_suffix: '?',
1645+
}),
1646+
),
1647+
};
1648+
1649+
await createDataLoaders(config, async (getLoaders) => {
1650+
const loaders = getLoaders(resources);
1651+
1652+
const results = await loaders.foo.loadMany([{ bar_id: 1 }, { bar_id: 2 }]);
1653+
expect(results).toEqual([
1654+
{ message: 'knock knock', message_suffix: '!' },
1655+
{ message: "who's there", message_suffix: '?' },
1656+
]);
1657+
});
1658+
});

examples/swapi/.babelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"@babel/preset-env",
55
{
66
"targets": {
7-
"node": "8"
7+
"node": "22"
88
}
99
}
1010
],
11-
"@babel/preset-flow"
11+
"@babel/preset-typescript"
1212
]
1313
}

examples/swapi/.flowconfig

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/swapi/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
build
2+
.yarnrc

examples/swapi/Makefile

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
node_modules: package.json
22
yarn
33

4-
.PHONY: swapi-loaders.js
5-
swapi-loaders.js:
6-
node ../../lib/index.js --config swapi.dataloader-config.yaml --output swapi-loaders.js
7-
8-
flow-typed: node_modules
9-
yarn flow-typed install
4+
.PHONY: swapi-loaders.ts
5+
swapi-loaders.ts:
6+
node ../../lib/index.js --config swapi.dataloader-config.yaml --output swapi-loaders.ts
107

118
.PHONY: build
129
build: node_modules
13-
yarn babel *.js -d build
10+
yarn babel *.ts -d build -x .ts

examples/swapi/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Build the example:
1919
$ cd examples/swapi
2020
$ yarn
2121
$ yarn link dataloader-codegen
22-
$ make swapi-loaders.js
22+
$ make swapi-loaders.ts
2323
$ make build
2424
# Prints out a GraphQL query result:
2525
$ node build/swapi-server.js

examples/swapi/flow-typed/npm/@babel/node_vx.x.x.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

examples/swapi/flow-typed/npm/@babel/preset-flow_vx.x.x.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

examples/swapi/flow-typed/npm/flow-bin_v0.x.x.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)