-
Notifications
You must be signed in to change notification settings - Fork 13
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
dcf16ce
e0336ea
714a1dc
5248343
def2cd2
737e308
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| 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'; | ||
| 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()), | ||
| }; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,13 @@ import path from 'path'; | |
| import yaml from 'js-yaml'; | ||
| import Ajv from 'ajv'; | ||
|
|
||
| export enum LanguageOptions { | ||
| FLOW = 'flow', | ||
| TYPESCRIPT = 'typescript', | ||
| } | ||
|
||
| export interface GlobalConfig { | ||
| typings: { | ||
| language: 'flow'; | ||
| language: LanguageOptions; | ||
| embedResourcesType: { | ||
| imports: string; | ||
| ResourcesType: string; | ||
|
|
||
| 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('.')}]`; | ||
|
|
@@ -136,6 +136,7 @@ export function getLoaderType(resourceConfig: ResourceConfig, resourcePath: Read | |
| } | ||
|
|
||
| export function getLoadersTypeMap( | ||
| language: LanguageOptions = LanguageOptions.FLOW, | ||
|
||
| config: object, | ||
| paths: ReadonlyArray<ReadonlyArray<string>>, | ||
| current: ReadonlyArray<string>, | ||
|
|
@@ -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], | ||
|
|
||

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
urlimport but tbh this is probably a mistake, this is easy using stdlib (available as of node v10)try the following in a node repl:
or if you don't want to rely on a magic .toString()