Skip to content
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

[E4E-87]: Finally getting these input types to work + tests #15

Merged
merged 13 commits into from
Jan 24, 2022
59 changes: 36 additions & 23 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,58 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [1.3.2](https://github.com/nrccua/apollo-rest-utils/compare/1.3.1...1.3.2) (2022-01-07)
### [1.4.2](https://github.com/nrccua/apollo-rest-utils/compare/1.4.1...1.4.2) (2022-01-19)


### Changes

* [E4E-87]: Remove console.logs ([24f8f5b](https://github.com/nrccua/apollo-rest-utils/commit/24f8f5b018f0306feaa50bb387868199265accdf))
* [E4E-87]: 1.4.2 ([6d80ab3](https://github.com/nrccua/apollo-rest-utils/commit/6d80ab37bd2eb1cccf52beb165c63217346dfc8b))
* [E4E-87]: Strip optionals when possible ([f872125](https://github.com/nrccua/apollo-rest-utils/commit/f87212573b5d3d1624a6ca2effc1e01406ed7122))
* [E4E-87]: 1.4.1 ([7b0f336](https://github.com/nrccua/apollo-rest-utils/commit/7b0f336f75f5b9752ad5381de541180ca821004a))

### [1.4.1](https://github.com/nrccua/apollo-rest-utils/compare/1.4.0...1.4.1) (2022-01-07)


### Changes

* [E4E-87]: Add Input to body when combined with path/query params ([a57478f](https://github.com/nrccua/apollo-rest-utils/commit/a57478fcb59eba26f18599df20909c6faab768ef))
* [E4E-87]: 1.4.0 (for real this time) ([66aa604](https://github.com/nrccua/apollo-rest-utils/commit/66aa604f9e4230e59da19781ae4f74f870917679))

## [1.4.0](https://github.com/nrccua/apollo-rest-utils/compare/1.3.3...1.4.0) (2022-01-07)


### Changes

* [E4E-87]: Allow empty result selections for mutations ([874370d](https://github.com/nrccua/apollo-rest-utils/commit/874370db6e74bc67e651ad1284bf876aedd26bd6))
* [E4E-87]: 1.4.0 ([ba62579](https://github.com/nrccua/apollo-rest-utils/commit/ba62579a5b10ff5989dc0e755b0d27649716ac80))
* [E4E-87]: Allow for empty selectionSet on mutations, add some debug logging ([c4d128a](https://github.com/nrccua/apollo-rest-utils/commit/c4d128a2ba6573b7d07866bf65a2ca56557f22e9))
* [E4E-87]: Update libraries; do some lint fixes ([5431c56](https://github.com/nrccua/apollo-rest-utils/commit/5431c56acee25b948edaccf85630d9bfdb78bfec))
* [E4E-87]: 1.3.3 ([b5d8961](https://github.com/nrccua/apollo-rest-utils/commit/b5d896118af5f7767c1a17b27b1adfe288dc1002))

### [1.3.3](https://github.com/nrccua/apollo-rest-utils/compare/1.3.2...1.3.3) (2022-01-07)

### Changes

* [E4E-87]: Finally getting these input types to work + tests ([e52136b](https://github.com/nrccua/apollo-rest-utils/commit/e52136b34233105cab091a889389c6d5d0e9de20))
* [E4E-87]: 1.3.2 ([b2d38d5](https://github.com/nrccua/apollo-rest-utils/commit/b2d38d5655eca628775cf63b7da2e20c2b2b1670))

### [1.3.2](https://github.com/nrccua/apollo-rest-utils/compare/1.3.1...1.3.2) (2022-01-07)

### Changes

* [E4E-87]: Allow some params to be optional, duh ([c507d2b](https://github.com/nrccua/apollo-rest-utils/commit/c507d2b2c81eda25cf4b7c75b5a77e0eba20309c))
* [E4E-87]: 1.3.1 ([494dae2](https://github.com/nrccua/apollo-rest-utils/commit/494dae20089d6bd9dff3d8942026dc1abfc059f0))

### [1.3.1](https://github.com/nrccua/apollo-rest-utils/compare/1.3.0...1.3.1) (2022-01-07)


### Changes

* [E4E-87]: Allow an empty input body to be passed to be passed in variables ([4b67e40](https://github.com/nrccua/apollo-rest-utils/commit/4b67e4033b6773a682b4363224076e9dcdf0f828))
* [E4E-87]: 1.3.0 ([cbda2b6](https://github.com/nrccua/apollo-rest-utils/commit/cbda2b6cef2483571b99b99943777aafb21c2449))

## [1.3.0](https://github.com/nrccua/apollo-rest-utils/compare/1.2.2...1.3.0) (2022-01-06)


### Changes

* [E4E-87]: Update to fix missing path params when a body is specified ([655fd4c](https://github.com/nrccua/apollo-rest-utils/commit/655fd4c3ae23df53d4e0bc1dfd3a4d9c7cf6d3fd))
Expand All @@ -30,7 +63,6 @@ All notable changes to this project will be documented in this file. See [standa

### [1.2.2](https://github.com/nrccua/apollo-rest-utils/compare/1.2.1...1.2.2) (2021-12-09)


### Changes

* [E4E-0]: Only support 14 + 16 ([71f44cc](https://github.com/nrccua/apollo-rest-utils/commit/71f44cc8622bf69e8caa8951e30d316edf4bcdd4))
Expand All @@ -40,7 +72,6 @@ All notable changes to this project will be documented in this file. See [standa

### [1.2.1](https://github.com/nrccua/apollo-rest-utils/compare/1.2.0...1.2.1) (2021-12-08)


### Changes

* [E4E-58]: Add helper rest type extractors ([fc0fbad](https://github.com/nrccua/apollo-rest-utils/commit/fc0fbad7435a5217ade6857bd6be006bfd8e000a))
Expand All @@ -49,7 +80,6 @@ All notable changes to this project will be documented in this file. See [standa

## [1.2.0](https://github.com/nrccua/apollo-rest-utils/compare/1.1.1...1.2.0) (2021-12-06)


### Changes

* [E4E-0]: Flesh out unit tests ([5668f6b](https://github.com/nrccua/apollo-rest-utils/commit/5668f6b24a25e394865225ac3f8e0dd013a3007f))
Expand All @@ -60,7 +90,6 @@ All notable changes to this project will be documented in this file. See [standa

### [1.1.1](https://github.com/nrccua/apollo-rest-utils/compare/1.1.0...1.1.1) (2021-12-03)


### Changes

* [E4E-0]: Account for Swagger 2 differences in file generation ([5027a5e](https://github.com/nrccua/apollo-rest-utils/commit/5027a5e59a2e9a1be84a3dbde1f7009a727717f4))
Expand All @@ -76,79 +105,69 @@ All notable changes to this project will be documented in this file. See [standa

## [1.1.0](https://github.com/nrccua/apollo-rest-utils/compare/1.0.14...1.1.0) (2021-11-29)


### Changes

* [E4E-30]: Add ability to specify an endpoint id ([dd01aea](https://github.com/nrccua/apollo-rest-utils/commit/dd01aea0daad0fbf64e7ee358c4a34d571456c82))
* [E4E-30]: 1.0.14 ([191a478](https://github.com/nrccua/apollo-rest-utils/commit/191a47871b111b63aa9444538e5d45f3fcd9ab99))

### [1.0.14](https://github.com/nrccua/apollo-rest-utils/compare/1.0.13...1.0.14) (2021-11-24)


### Changes

* [E4E-30]: Fix Response name generation + better tests ([e66d290](https://github.com/nrccua/apollo-rest-utils/commit/e66d290c350bcf22abfd650e79f55ef371447dc9))
* [E4E-30]: 1.0.13 ([55ba637](https://github.com/nrccua/apollo-rest-utils/commit/55ba6373e2e3706af197fadf2ac6fe4fa8a266e3))

### [1.0.13](https://github.com/nrccua/apollo-rest-utils/compare/1.0.12...1.0.13) (2021-11-22)


### Changes

* [E4E-30]: Typo inception ([4fef9ae](https://github.com/nrccua/apollo-rest-utils/commit/4fef9ae4d34446801259ad4ea4992fbed1c6dcc2))
* [E4E-30]: 1.0.12 ([17195db](https://github.com/nrccua/apollo-rest-utils/commit/17195db1caaea1f94c3586a644975fa2581e54bc))

### [1.0.12](https://github.com/nrccua/apollo-rest-utils/compare/1.0.11...1.0.12) (2021-11-22)


### Changes

* [E4E-30]: Fix typo ([72a57dc](https://github.com/nrccua/apollo-rest-utils/commit/72a57dc81a3426811cfd680fd1398e934505ceb1))
* [E4E-30]: 1.0.11 ([d6c1039](https://github.com/nrccua/apollo-rest-utils/commit/d6c1039ca24d04227e48edee6849b2d6eb97e9d5))

### [1.0.11](https://github.com/nrccua/apollo-rest-utils/compare/1.0.10...1.0.11) (2021-11-21)


### Changes

* [E4E-30]:ApolloClient<object> ([ae6db2a](https://github.com/nrccua/apollo-rest-utils/commit/ae6db2a5c13366b7df670b501b1d4f14460f8308))
* [E4E-30]: 1.0.10 ([6bce871](https://github.com/nrccua/apollo-rest-utils/commit/6bce871f8c23f80affc1493a5f75463995e09203))

### [1.0.10](https://github.com/nrccua/apollo-rest-utils/compare/1.0.9...1.0.10) (2021-11-21)


### Changes

* [E4E-30]: Fix a few more unknown => any ([be51ef0](https://github.com/nrccua/apollo-rest-utils/commit/be51ef09609061982e2c0b4c36cadf49e5e5f079))
* [E4E-30]: 1.0.9 ([f7beab7](https://github.com/nrccua/apollo-rest-utils/commit/f7beab79299838ef1d14d1cee5266e71702fa618))

### [1.0.9](https://github.com/nrccua/apollo-rest-utils/compare/1.0.8...1.0.9) (2021-11-21)


### Changes

* [E4E-30]: Allow ApolloClient<any> for dowstream flexibility ([a8e0b0f](https://github.com/nrccua/apollo-rest-utils/commit/a8e0b0f8feba57b171fa6aaf40fc07e257cc0430))
* [E4E-30]: 1.0.8 ([c2be2d5](https://github.com/nrccua/apollo-rest-utils/commit/c2be2d5722c6564e590449a63f5d20f71b319189))

### [1.0.8](https://github.com/nrccua/apollo-rest-utils/compare/1.0.7...1.0.8) (2021-11-21)


### Changes

* [E4E-30]: Remove invalid stuff from pr-testing ([a71f7cb](https://github.com/nrccua/apollo-rest-utils/commit/a71f7cbfff71c05a61f19ab832b7702e0db06bc9))
* [E4E-30]: 1.0.7 ([e3189a2](https://github.com/nrccua/apollo-rest-utils/commit/e3189a293b0b75c269bec35553e2f4dd8b82068b))

### [1.0.7](https://github.com/nrccua/apollo-rest-utils/compare/1.0.6...1.0.7) (2021-11-21)


### Changes

* [E4E-30]: Fix build script to generate restSchema files ([41dd938](https://github.com/nrccua/apollo-rest-utils/commit/41dd938260be9966c048305aa9f19a8b063be45b))
* [E4E-30]: 1.0.6 ([c96cf93](https://github.com/nrccua/apollo-rest-utils/commit/c96cf93c4b46f2b361a6ee9e5e1b55ee1e66796b))

### [1.0.6](https://github.com/nrccua/apollo-rest-utils/compare/1.0.5...1.0.6) (2021-11-21)


### Changes

* [E4E-30]: Add Github actions for PRs ([7a84926](https://github.com/nrccua/apollo-rest-utils/commit/7a84926378790a268e8aec377a32676a69f1deac))
Expand All @@ -157,47 +176,41 @@ All notable changes to this project will be documented in this file. See [standa

### [1.0.5](https://github.com/nrccua/apollo-rest-utils/compare/1.0.4...1.0.5) (2021-11-21)


### Changes

* [E4E-30] Specify node as the shell for the executable and chmod ([7775766](https://github.com/nrccua/apollo-rest-utils/commit/7775766850dd01ac6fbf23503b7908cc80e7b741))
* [E4E-30]: 1.0.4 ([5526410](https://github.com/nrccua/apollo-rest-utils/commit/5526410bb34eb59b5ad66bef34f2508382072fd8))

### [1.0.4](https://github.com/nrccua/apollo-rest-utils/compare/1.0.3...1.0.4) (2021-11-21)


### Changes

* [E4E-30]: Guess you don't specify node... ([cba67e1](https://github.com/nrccua/apollo-rest-utils/commit/cba67e171074e51050261a275189a807741b5af0))
* [E4E-30]: 1.0.3 ([6cb92fb](https://github.com/nrccua/apollo-rest-utils/commit/6cb92fb3c5a5c216f8bf00631604ee031abfa98d))

### [1.0.3](https://github.com/nrccua/apollo-rest-utils/compare/1.0.2...1.0.3) (2021-11-21)


### Changes

* [E4E-30]: Fix issue with no bin being generated on downstream ([391e872](https://github.com/nrccua/apollo-rest-utils/commit/391e87273a95f2303ada672578a53b24e769b18b))
* [E4E-30]: 1.0.2 ([ea5ddff](https://github.com/nrccua/apollo-rest-utils/commit/ea5ddffab510578181cb9f66759dcf3751859be3))

### [1.0.2](https://github.com/nrccua/apollo-rest-utils/compare/1.0.1...1.0.2) (2021-11-21)


### Changes

* [E4E-30]: FIx the lack of a root index.js on build ([c464ba6](https://github.com/nrccua/apollo-rest-utils/commit/c464ba68f0d03e5afea5ce0f069413e848e4c21f))
* [E4E-30]: Update changelog + version = 1.0.1 ([6e745be](https://github.com/nrccua/apollo-rest-utils/commit/6e745be765fcfebb400501dcfd567fa061064f49))

### [1.0.1](https://github.com/nrccua/apollo-rest-utils/compare/1.0.0...1.0.1) (2021-11-21)


### Changes

* [E4E-30]: Port of existing code from private repo ([7691b31](https://github.com/nrccua/apollo-rest-utils/commit/7691b3185ce2f06a6d7f6981a318f346b6c84fa3))
* [E4E-30]: Add standard-version initial CHANGELOG ([5c3b975](https://github.com/nrccua/apollo-rest-utils/commit/5c3b9755144880be47f7bd6b68879e438470fc0e))

## 1.0.0 (2021-11-20)


### Changes

* [E4E-30]: Initial commit; base repo (no functionality) ([b437b91](https://github.com/nrccua/apollo-rest-utils/commit/b437b9140ff1b1a229f5fdb78d8ac144b803217e))
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ with REST apis and Apollo in a TypeScript application.

From the command line you can generate definitions for endpoints:

<!-- markdownlint-capture -->
<!-- markdownlint-disable MD013 -->
```bash
npx apollo-rest-utils <path_to_swagger_file_or_url> <output_directory_where_you_want_the_files> [optional_endpoint_id_to_use]
npx apollo-rest-utils <path_to_swagger_file_or_url> <output_directory_where_you_want_the_files> [optional_endpoint_id_to_use]
```
<!-- markdownlint-restore -->

Then you can use those definitions to make GraphQL calls within an Apollo context:

Expand Down
39 changes: 27 additions & 12 deletions lib/generateRoutes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,7 @@ export async function generateTypes(apiPath: string, filePath: string): Promise<
}

export function generateTypescript(api: OpenAPI.Document, typeImportLocation: string, endpointId?: string): string {
let generatedTSEndpoints =
'/**\n* This file was auto-generated by the generateRoutes endpoint generator.\n* Do not make direct changes to the file.\n' +
`* To update this file run \`npx apollo-rest-utils <swagger_definition> <directory_of_this_file>\`\n*/\n\n`;
generatedTSEndpoints += `import { paths } from '${typeImportLocation}'\n\nimport { IRestEndpoint} from 'apollo-rest-utils';\n\n\n`;
let generatedTSEndpoints = '';
// Create an object representing routes by method, e.g., { 'get': {}, ... }
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
const routes = Object.entries(OpenAPIV3.HttpMethods).reduce(
Expand Down Expand Up @@ -162,15 +159,11 @@ export function generateTypescript(api: OpenAPI.Document, typeImportLocation: st
// eslint-disable-next-line no-nested-ternary
const requestBody = contentKeys.includes('application/json')
? // OpenAPI 3 way
`paths['${endpointPath}']['${method}']['requestBody']['content']['application/json']${
queryPathParamsObject ? ` & ${queryPathParamsObject}` : ''
}`
`paths['${endpointPath}']['${method}']['requestBody']['content']['application/json']`
: // Swagger 2 way
bodyParam
? `paths['${endpointPath}']['${method}']['parameters']['body']['body']${
queryPathParamsObject ? ` & ${queryPathParamsObject}` : ''
}`
: queryPathParamsObject;
? `paths['${endpointPath}']['${method}']['parameters']['body']['body']`
: undefined;
const headers =
endpointObject.parameters
?.filter(p => 'in' in p && p.in === 'header')
Expand All @@ -190,7 +183,17 @@ export function generateTypescript(api: OpenAPI.Document, typeImportLocation: st
: ''
}${
responseSchema ? `responseSchema: ${JSON.stringify(responseSchema)},\n` : ''
}} as IRestEndpoint<${responseBody}${requestBody ? `,${requestBody}` : ''}>,`;
}} as IRestEndpoint<${responseBody}${
// eslint-disable-next-line no-nested-ternary
requestBody && queryPathParamsObject
? `,Input<${requestBody}> & ${queryPathParamsObject}`
: // eslint-disable-next-line no-nested-ternary
requestBody
? `,${requestBody}`
: queryPathParamsObject
? `,${queryPathParamsObject}`
: ''
}>,`;
});
});
generatedTSEndpoints += 'const ROUTES = {\n';
Expand All @@ -207,6 +210,18 @@ export function generateTypescript(api: OpenAPI.Document, typeImportLocation: st
});
generatedTSEndpoints += '};\n\nexport default ROUTES;';

const warningHeader =
'/**\n* This file was auto-generated by the generateRoutes endpoint generator.\n* Do not make direct changes to the file.\n' +
`* To update this file run \`npx apollo-rest-utils <swagger_definition> <directory_of_this_file>\`\n*/\n\n`;
// eslint-disable-next-line lodash/prefer-includes
if (generatedTSEndpoints.indexOf('Input<') > -1) {
// eslint-disable-next-line max-len
generatedTSEndpoints = `${warningHeader}import { paths } from '${typeImportLocation}'\n\nimport { IRestEndpoint, Input } from 'apollo-rest-utils';\n\n\n${generatedTSEndpoints}`;
} else {
// eslint-disable-next-line max-len
generatedTSEndpoints = `${warningHeader}import { paths } from '${typeImportLocation}'\n\nimport { IRestEndpoint } from 'apollo-rest-utils';\n\n\n${generatedTSEndpoints}`;
}

return generatedTSEndpoints;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/types/restSchema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type RestRequest<T> = T extends IRestEndpoint<unknown, infer U> ? U : nev
// 1. All fields to put into an input object
// or
// 2. Allow an empty input object to overcome a bug in apollo-link-rest where PATCH/POST/PUT barfs with no body
export type Input<T> = (T & { input?: Record<string, never> }) | { input: T };
export type Input<T> = T | (T & { input?: Record<string, never> }) | { input: T };

export interface IEndpointOptions<T, U> {
endpoint: IRestEndpoint<T, U>;
Expand Down
Loading