Skip to content

Tsclientlib and language option #349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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.

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

import fetch from 'node-fetch';
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(new URL(`planets/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getPeople: ({ people_ids: peopleIds }) =>
Promise.all(
peopleIds.map((id) => fetch(new URL(`people/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getVehicles: ({ vehicle_ids: vehicleIds }) =>
Promise.all(
vehicleIds.map((id) => fetch(new URL(`vehicles/${id}`, SWAPI_URL)).then((res) => res.json())),
),
getFilms: ({ film_ids: filmIds }) =>
Promise.all(
[...filmIds].map((id) => fetch(new URL(`films/${id}`, SWAPI_URL)).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
3 changes: 2 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import path from 'path';
import yaml from 'js-yaml';
import Ajv from 'ajv';

export type LanguageOption = 'flow' | 'typescript';
export interface GlobalConfig {
typings: {
language: 'flow';
language: LanguageOption;
embedResourcesType: {
imports: string;
ResourcesType: string;
Expand Down
6 changes: 3 additions & 3 deletions src/codegen.ts → src/genTypeFlow/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _ from 'lodash';
import prettier from 'prettier';
import { GlobalConfig, getResourcePaths } from './config';
import { getLoaderType, getLoadersTypeMap, getResourceTypings } from './genTypeFlow';
import getLoaderImplementation from './implementation';
import { GlobalConfig, getResourcePaths } from '../config';
import { getLoaderType, getLoadersTypeMap, getResourceTypings } from './genType';
import getLoaderImplementation from '../genTypeFlow/implementation';

function getLoaders(config: object, paths: Array<Array<string>>, current: Array<string>) {
if (_.isEqual(paths, [[]])) {
Expand Down
4 changes: 2 additions & 2 deletions src/genTypeFlow.ts → src/genTypeFlow/genType.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 assert from '../assert';
import { GlobalConfig, ResourceConfig, LanguageOption } from '../config';

function errorPrefix(resourcePath: ReadonlyArray<string>): string {
return `[dataloader-codegen :: ${resourcePath.join('.')}]`;
Expand Down
Loading