Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,294 changes: 1,749 additions & 1,545 deletions examples/swapi/swapi-loaders.js

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions examples/swapi/swapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Typescript version clientlib for a subset of data in https://swapi.dev/
*/

import fetch from 'node-fetch';
import * as url from 'url';
Copy link
Collaborator

@magicmark magicmark Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the status quo uses the url import but tbh this is probably a mistake, this is easy using stdlib (available as of node v10)

try the following in a node repl:

new URL('/people/123', 'https://swapi.dev/api/');

or if you don't want to rely on a magic .toString()

(new URL('/people/123', 'https://swapi.dev/api/')).href

const SWAPI_URL = 'https://swapi.dev/api/';

export interface SWAPI_Planet {
readonly name: string;
readonly rotation_period: string;
readonly orbital_period: string;
readonly diameter: string;
readonly climate: string;
readonly gravity: string;
readonly terrain: string;
readonly surface_water: string;
readonly population: string;
readonly residents: readonly string[];
readonly films: readonly string[];
readonly created: string;
readonly edited: string;
readonly url: string;
}

export interface SWAPI_Person {
readonly name: string,
readonly height: string,
readonly mass: string,
readonly hair_color: string,
readonly skin_color: string,
readonly eye_color: string,
readonly birth_year: string,
readonly gender: string,
readonly homeworld: string,
readonly films: readonly string[],
readonly species: readonly string[],
readonly vehicles: readonly string[],
readonly starships: readonly string[],
readonly created: string,
readonly edited: string,
readonly url: string,
}

export interface SWAPI_Film {
readonly title: string;
readonly episode_id: number;
readonly opening_crawl: string;
readonly director: string;
readonly producer: string;
readonly release_date: string;
readonly species: readonly string[];
readonly starships: readonly string[];
readonly vehicles: readonly string[];
readonly characters: readonly string[];
readonly planets: readonly string[];
readonly url: string;
readonly created: string;
readonly edited: string;
}

export interface SWAPI_Film_V2 {
readonly properties: ReadonlyArray<{
id: number;
title?: string;
episode_id?: number;
director?: string;
producer?: string;
}>;
}

export interface SWAPI_Vehicle {
readonly name: string;
readonly key: string;
}

interface SWAPI_Root {
readonly people: string;
readonly planets: string;
readonly films: string;
readonly species: string;
readonly vehicles: string;
readonly starships: string;
}

export interface SWAPIClientlibTypes {
getPlanets: (params: { readonly planet_ids: readonly number[] }) => Promise<readonly SWAPI_Planet[]>;
getPeople: (params: { readonly people_ids: readonly number[] }) => Promise<readonly SWAPI_Person[]>;
getVehicles: (params: { readonly vehicle_ids: readonly number[] }) => Promise<readonly SWAPI_Vehicle[]>;
getFilms: (params: { film_ids: Set<number> }) => Promise<readonly SWAPI_Film[]>;
getFilmsV2: (params: {
readonly film_ids: readonly number[];
readonly properties: readonly string[];
}) => Promise<SWAPI_Film_V2>;
getRoot: (params: {}) => Promise<SWAPI_Root>;
}

export default function(): SWAPIClientlibTypes {
return {
getPlanets: ({ planet_ids: planetIds }) =>
Promise.all(
planetIds.map((id) => fetch(url.resolve(SWAPI_URL, `planets/${id}`)).then((res) => res.json())),
),
getPeople: ({ people_ids: peopleIds }) =>
Promise.all(
peopleIds.map((id) => fetch(url.resolve(SWAPI_URL, `people/${id}`)).then((res) => res.json())),
),
getVehicles: ({ vehicle_ids: vehicleIds }) =>
Promise.all(
vehicleIds.map((id) => fetch(url.resolve(SWAPI_URL, `vehicles/${id}`)).then((res) => res.json())),
),
getFilms: ({ film_ids: filmIds }) =>
Promise.all(
[...filmIds].map((id) => fetch(url.resolve(SWAPI_URL, `films/${id}`)).then((res) => res.json())),
),
getFilmsV2: ({ film_ids: filmIds, properties }) => {
return Promise.resolve({
properties: [
{
id: 4,
director: 'George Lucas',
producer: 'Rick McCallum',
episode_id: 1,
title: 'The Phantom Menace',
},
],
});
},
getRoot: ({}) => fetch(SWAPI_URL).then((res) => res.json()),
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/js-yaml": "^3.12.1",
"@types/lodash": "^4.14.144",
"@types/node": "^12.7.12",
"@types/node-fetch": "^2.6.9",
"@types/object-hash": "^1.3.0",
"@types/prettier": "^1.18.3",
"@types/yargs": "^13.0.3",
Expand Down
11 changes: 6 additions & 5 deletions src/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import _ from 'lodash';
import prettier from 'prettier';
import { GlobalConfig, getResourcePaths } from './config';
import { GlobalConfig, getResourcePaths, LanguageOptions } from './config';
import { getLoaderType, getLoadersTypeMap, getResourceTypings } from './genTypeFlow';
import getLoaderImplementation from './implementation';

function getLoaders(config: object, paths: Array<Array<string>>, current: Array<string>) {
function getLoaders(language: LanguageOptions, config: object, paths: Array<Array<string>>, current: Array<string>) {
if (_.isEqual(paths, [[]])) {
return getLoaderImplementation(_.get(config, current.join('.')), current);
return getLoaderImplementation(language, _.get(config, current.join('.')), current);
}

const nextValues = _.uniq(paths.map((p) => p[0]));

const objectProperties: Array<string> = nextValues.map(
(nextVal) =>
`${nextVal}: ${getLoaders(
language,
config,
paths.filter((p) => p[0] === nextVal).map((p) => p.slice(1)),
[...current, nextVal],
Expand Down Expand Up @@ -110,10 +111,10 @@ export default function codegen(
* ===============================
*/

export type LoadersType = ${getLoadersTypeMap(config.resources, getResourcePaths(config.resources), [])};
export type LoadersType = ${getLoadersTypeMap(config.typings.language, config.resources, getResourcePaths(config.resources), [])};

export default function getLoaders(resources: ResourcesType, options?: DataLoaderCodegenOptions): LoadersType {
return ${getLoaders(config.resources, getResourcePaths(config.resources), [])};
return ${getLoaders(config.typings.language, config.resources, getResourcePaths(config.resources), [])};
}
`;

Expand Down
6 changes: 5 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import path from 'path';
import yaml from 'js-yaml';
import Ajv from 'ajv';

export enum LanguageOptions {
FLOW = 'flow',
TYPESCRIPT = 'typescript',
}
Copy link
Collaborator

@magicmark magicmark Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: enum is fine i guess, but I think a union would be more idiomatic

Screenshot 2024-02-15 at 8 57 36 AM
Suggested change
export enum LanguageOptions {
FLOW = 'flow',
TYPESCRIPT = 'typescript',
}
export type LanguageOption = 'flow' | 'typescript';

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(also note the LanguageOptions -> LanguageOption (singular), since there's only one one option being decided here (the target language)

export interface GlobalConfig {
typings: {
language: 'flow';
language: LanguageOptions;
embedResourcesType: {
imports: string;
ResourcesType: string;
Expand Down
4 changes: 3 additions & 1 deletion src/genTypeFlow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import assert from './assert';
import { GlobalConfig, ResourceConfig } from './config';
import { GlobalConfig, ResourceConfig, LanguageOptions } from './config';

function errorPrefix(resourcePath: ReadonlyArray<string>): string {
return `[dataloader-codegen :: ${resourcePath.join('.')}]`;
Expand Down Expand Up @@ -136,6 +136,7 @@ export function getLoaderType(resourceConfig: ResourceConfig, resourcePath: Read
}

export function getLoadersTypeMap(
language: LanguageOptions = LanguageOptions.FLOW,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, this file is called genTypeFlow.ts implying this is specific for generating types for flow. in my mind, for doing typescript type generation, we'd just copy and paste this whole file and start again making it only typescript focused (rather than having each individual function do a little if statement to check are we generating for flow or typescript)

with that in mind, the choice of 'flow' here would be implicit and not needed as an argument

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we use separate genTypeFlow and genTypeTypesciprt, then we could move to codegen function check if the language option is flow or typescript. but in implementations, we'll probably still can't avoid these little if statements.. any suggestions?

export default function codegen(
    /**
     * The user specified config object, defining the shape and behaviour of
     * the resources. May be arbitrarily nested, hence the 'any' type.
     * (Read from dataloader-config.yaml)
     */
    config: GlobalConfig,
   ...
) {
...
if config.language == typescript {
 output = getTypescriptLoaders()
} 
else {
 output = getFlowLoaders()
}
}

config: object,
paths: ReadonlyArray<ReadonlyArray<string>>,
current: ReadonlyArray<string>,
Expand All @@ -149,6 +150,7 @@ export function getLoadersTypeMap(
const objectProperties: ReadonlyArray<string> = nextValues.map(
(nextVal) =>
`${nextVal}: ${getLoadersTypeMap(
language,
config,
paths.filter((p) => p[0] === nextVal).map((p) => p.slice(1)),
[...current, nextVal],
Expand Down
16 changes: 8 additions & 8 deletions src/implementation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ResourceConfig, BatchResourceConfig, NonBatchResourceConfig } from './config';
import { ResourceConfig, BatchResourceConfig, NonBatchResourceConfig, LanguageOptions } from './config';
import assert from './assert';
import { getLoaderTypeKey, getLoaderTypeVal } from './genTypeFlow';
import { errorPrefix } from './runtimeHelpers';
Expand Down Expand Up @@ -312,7 +312,7 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
`;
}

function getBatchLoader(resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
function getBatchLoader(language: LanguageOptions, resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
assert(
resourceConfig.isBatchResource === true,
`${errorPrefix(resourcePath)} Expected getBatchLoader to be called with a batch resource config`,
Expand Down Expand Up @@ -421,7 +421,7 @@ function getBatchLoader(resourceConfig: BatchResourceConfig, resourcePath: Reado
)`;
}

function getPropertyBatchLoader(resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
function getPropertyBatchLoader(language: LanguageOptions, resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
assert(
resourceConfig.isBatchResource === true,
`${errorPrefix(resourcePath)} Expected getBatchLoader to be called with a batch resource config`,
Expand Down Expand Up @@ -519,7 +519,7 @@ function getPropertyBatchLoader(resourceConfig: BatchResourceConfig, resourcePat
)`;
}

function getNonBatchLoader(resourceConfig: NonBatchResourceConfig, resourcePath: ReadonlyArray<string>) {
function getNonBatchLoader(language: LanguageOptions, resourceConfig: NonBatchResourceConfig, resourcePath: ReadonlyArray<string>) {
assert(
resourceConfig.isBatchResource === false,
`${errorPrefix(resourcePath)} Expected getNonBatchLoader to be called with a non-batch endpoint config`,
Expand Down Expand Up @@ -555,14 +555,14 @@ function getNonBatchLoader(resourceConfig: NonBatchResourceConfig, resourcePath:
})`;
}

export default function getLoaderImplementation(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>) {
export default function getLoaderImplementation(language: LanguageOptions = LanguageOptions.FLOW, resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>) {
if (resourceConfig.isBatchResource) {
if (typeof resourceConfig.propertyBatchKey === 'string') {
return getPropertyBatchLoader(resourceConfig, resourcePath);
return getPropertyBatchLoader(language, resourceConfig, resourcePath);
} else {
return getBatchLoader(resourceConfig, resourcePath);
return getBatchLoader(language, resourceConfig, resourcePath);
}
} else {
return getNonBatchLoader(resourceConfig, resourcePath);
return getNonBatchLoader(language, resourceConfig, resourcePath);
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./lib" /* Redirect output structure to the directory. */,
"rootDir": "./src" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"rootDir": "./" /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
Expand Down
19 changes: 18 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,14 @@
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.144.tgz#12e57fc99064bce45e5ab3c8bc4783feb75eab8e"
integrity sha512-ogI4g9W5qIQQUhXAclq6zhqgqNUr7UlFaqDHbch7WLSLeeM/7d3CRaw7GLajxvyFvhJqw4Rpcz5bhoaYtIx6Tg==

"@types/node-fetch@^2.6.9":
version "2.6.9"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.9.tgz#15f529d247f1ede1824f7e7acdaa192d5f28071e"
integrity sha512-bQVlnMLFJ2d35DkPNjEPmd9ueO/rh5EiaZt2bhqiSarPjZIuIV6bPQVqcrEyvNo+AfTrRGVazle1tl597w3gfA==
dependencies:
"@types/node" "*"
form-data "^4.0.0"

"@types/node@*", "@types/node@^12.7.12":
version "12.7.12"
resolved "https://registry.yarnpkg.com/@types/node/-/node-12.7.12.tgz#7c6c571cc2f3f3ac4a59a5f2bd48f5bdbc8653cc"
Expand Down Expand Up @@ -1688,7 +1696,7 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

combined-stream@^1.0.6, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -2201,6 +2209,15 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down