Skip to content

Move RTK-Query OpenAPI Codegen into Monorepo, prepare release 1.0 #1680

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

Merged
merged 110 commits into from
Nov 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
3b8097c
first WIP
phryneas Dec 6, 2020
ae28e0f
generation of query function
phryneas Dec 7, 2020
d9d3fb1
first "pretty runable" state
phryneas Dec 10, 2020
61c1f98
create gitignore by gibo
kahirokunn Dec 11, 2020
0a37337
improve tsconfig for cli
kahirokunn Dec 11, 2020
5138f86
create cli
kahirokunn Dec 11, 2020
3625ed4
add arg suffix and response suffix option
kahirokunn Dec 11, 2020
17ce451
add options for cli
kahirokunn Dec 11, 2020
3b94f9e
cli bug fix and add base url option
kahirokunn Dec 11, 2020
8457f84
Merge pull request #1 from kahirokunn/main
phryneas Dec 11, 2020
c966908
RTK Query requires method property, so add it.
kahirokunn Dec 16, 2020
d1d40d2
Merge pull request #2 from kahirokunn/main
phryneas Dec 16, 2020
5e838a0
remove undefined comment (#5)
kahirokunn Dec 25, 2020
7302ddc
update generated code
phryneas Dec 25, 2020
14cd150
prepare first release
phryneas Dec 26, 2020
33cddf9
fixup
phryneas Dec 26, 2020
b76accc
0.1.0-alpha.1
phryneas Dec 26, 2020
21c7965
0.1.0
phryneas Dec 26, 2020
e5554fb
Add basic README (#6)
msutkowski Dec 26, 2020
5e31f84
Update code-generation link in README (#7)
msutkowski Dec 26, 2020
9782a07
CLI enhancements: Add --hooks, --file options (#9)
msutkowski Jan 5, 2021
2fbf33d
Support custom base query from CLI and add tests (#13)
msutkowski Jan 12, 2021
08c45cd
DX: Add interface/type name to thrown Error #12
kahirokunn Jan 12, 2021
60def83
Strip file extensions on customBaseQueryNode (#19)
msutkowski Jan 13, 2021
f100a03
start extracting ts-generation code (#11)
phryneas Jan 13, 2021
1d286bf
build head to github package registry (#20)
phryneas Jan 13, 2021
5dbd17a
Create test.yml (#21)
phryneas Jan 13, 2021
efeadda
Prepare 0.2.0 release
msutkowski Jan 13, 2021
4f9cb4b
v0.2.0
msutkowski Jan 13, 2021
5dd7726
Add tests for yaml handling
msutkowski Jan 14, 2021
64c6789
Update snapshots
msutkowski Jan 14, 2021
2f13bb2
Merge pull request #23 from rtk-incubator/tests/add-yaml-tests
msutkowski Jan 14, 2021
0a6b39a
Merge pull request #25 from rtk-incubator/next
msutkowski Jan 14, 2021
092ccc0
v0.2.1-0
msutkowski Jan 14, 2021
451b743
Include lib in package output
msutkowski Jan 14, 2021
26541be
v0.2.2-0
msutkowski Jan 14, 2021
55a065b
v0.2.2
msutkowski Jan 14, 2021
cc0e957
Move prettier to deps
msutkowski Jan 14, 2021
0ebabbb
v0.2.3
msutkowski Jan 14, 2021
0976308
Support module aliases with --baseQuery (#29)
kahirokunn Jan 16, 2021
2083937
Merge branch 'main' into next
msutkowski Jan 16, 2021
3a531fe
Prepare 0.2.4
msutkowski Jan 16, 2021
c5343ce
v0.2.4
msutkowski Jan 16, 2021
001db47
bump oazapfts
phryneas Jan 21, 2021
609b066
Fix #32 by appending ./ to relative imports from the same folder (#38)
buzzie-bee Apr 5, 2021
9b32e67
Modify --baseQuery and --baseUrl behaviors (#41)
tevariou Apr 11, 2021
d539de6
Add test for warning behavior when combining baseUrl and baseQuery op…
msutkowski Apr 11, 2021
edbd524
Update README (#44)
msutkowski Apr 13, 2021
f3256a7
Prepare 0.2.1
msutkowski Apr 13, 2021
1edadd8
v0.2.5
msutkowski Apr 13, 2021
c52d6f7
Add support for RTKQ 0.3 with the /react entrypoint
msutkowski Apr 13, 2021
3e23e4f
Merge pull request #45 from rtk-incubator/rtkq-0.3-support
msutkowski Apr 19, 2021
3fc7339
Merge pull request #46 from rtk-incubator/next
msutkowski Apr 19, 2021
0f608b2
v0.3.0
msutkowski Apr 19, 2021
f0a962f
fix: rename entityTypes to tagTypes (#47)
tevariou Apr 23, 2021
982c2fa
Merge pull request #48 from rtk-incubator/next
msutkowski Apr 23, 2021
830302f
v0.3.1
msutkowski Apr 23, 2021
7dad2b5
fix(imports): move from rtk-query to redux-toolkit (#49)
tevariou Apr 27, 2021
454d4cf
Merge pull request #50 from rtk-incubator/next
msutkowski Apr 27, 2021
2d66073
v0.3.2
msutkowski Apr 27, 2021
320a060
update back to official oazapfts version 3.3.1
phryneas May 7, 2021
4fe6368
add import path argument
StefanBRas May 13, 2021
2f100f8
add documentation
StefanBRas May 18, 2021
0e86990
refactor package imports
StefanBRas May 18, 2021
2cfcb82
type base import
StefanBRas May 18, 2021
f935565
remove it.only from test
StefanBRas May 18, 2021
992ad9b
fix: special chars in params name handling (#52)
tevariou Jul 13, 2021
bce1f66
bump oazapfts to 3.4.0
phryneas Jul 13, 2021
985d8df
re-add openapi-types
phryneas Jul 13, 2021
7c075c4
valid property access in `generatePathExpression`
phryneas Jul 13, 2021
b8ff772
correct sanitization of querystring parameters, test (#65)
pollen8 Jul 13, 2021
cc7d49d
v0.4.0
msutkowski Jul 14, 2021
37db9e7
Update README links for rtk docs
msutkowski Jul 14, 2021
451a2c2
Generates void type when Request QueryArgs are empty.
jordancarney1 Sep 15, 2021
d206121
Merge pull request #72 from jordancarney1/generate-void-type
msutkowski Sep 15, 2021
e186638
Merge pull request #55 from StefanBRas/adjustable-createApi-import-path
msutkowski Sep 16, 2021
b018371
Merge branch 'next'
msutkowski Sep 16, 2021
d4cc311
Update snapshots
msutkowski Sep 16, 2021
fc75ed6
v0.5.0
msutkowski Sep 16, 2021
15bcb8f
rewrite to work with config file instead of command line args
phryneas Oct 29, 2021
70f7769
first working test
phryneas Oct 30, 2021
8f7ed9b
fixes & tests
phryneas Nov 1, 2021
fffec14
README
phryneas Nov 1, 2021
3278020
add serializer
phryneas Nov 1, 2021
624dc5a
use local schema in tests for faster tests
phryneas Nov 1, 2021
d0baac0
remove unused code
phryneas Nov 2, 2021
93034bc
prepare moving into RTK
phryneas Nov 2, 2021
357501f
Merge remote-tracking branch 'codegen/move-into-rtk'
phryneas Nov 2, 2021
5b71275
move in graphql-codegen-openapi
phryneas Nov 2, 2021
a62118f
integrate into setup
phryneas Nov 2, 2021
7302098
lint, increase test timeout
phryneas Nov 2, 2021
2a44464
adjust CI
phryneas Nov 2, 2021
fc12ce8
only run relevant tests
phryneas Nov 2, 2021
f1cc76b
Merge remote-tracking branch 'origin/master' into rtk-query-codegen-o…
phryneas Nov 4, 2021
da867d1
fix test feedback from @grumpyTofu
phryneas Nov 4, 2021
75fa769
more tests
phryneas Nov 4, 2021
75fe971
run codegen tests in band
phryneas Nov 4, 2021
50fa150
bug fixes PR 1680 (#1693)
grumpyTofu Nov 5, 2021
9b2e112
fix test paths
phryneas Nov 5, 2021
3f652f7
fix: use correct working directory for CLI test (#1703)
Lokaltog Nov 7, 2021
4b8f6b4
fix CI config
phryneas Nov 7, 2021
f0e0127
match endpoints with a function, exclude endpoints (#1713)
lindapaiste Nov 10, 2021
3c49e15
fix for TS 4.5
phryneas Nov 21, 2021
aca2705
fix problem with ts config files in some environments
phryneas Nov 21, 2021
9e79156
Fix yaml parsing from URL test
msutkowski Nov 21, 2021
6a90c3f
docs
phryneas Nov 21, 2021
9b4dd19
Merge remote-tracking branch 'origin/master' into rtk-query-codegen-o…
phryneas Nov 21, 2021
ab3e14a
Codegen: don't drop the method on a query unless it is 'GET' (#1738)
lindapaiste Nov 23, 2021
2e3d7ab
force `oazapfts` to use the same TS version as the codegen (#1771)
phryneas Nov 26, 2021
7c6fb02
fix docs
phryneas Nov 27, 2021
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
6 changes: 4 additions & 2 deletions .codesandbox/ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"packages": [
"packages/toolkit",
"packages/rtk-query-graphql-request-base-query",
"packages/action-listener-middleware"
"packages/action-listener-middleware",
"packages/rtk-query-codegen-openapi"
],
"publishDirectory": {
"@reduxjs/toolkit": "packages/toolkit",
"@rtk-query/graphql-request-base-query": "packages/rtk-query-graphql-request-base-query",
"@rtk-incubator/action-listener-middleware": "packages/action-listener-middleware"
"@rtk-incubator/action-listener-middleware": "packages/action-listener-middleware",
"@rtk-query/codegen-openapi": "packages/rtk-query-codegen-openapi"
}
}
45 changes: 45 additions & 0 deletions .github/workflows/test-codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: RTK-Query OpenAPI Codegen Tests
defaults:
run:
working-directory: ./packages/rtk-query-codegen-openapi

on: [push, pull_request]

jobs:
changes:
name: Check for changes
runs-on: ubuntu-latest
outputs:
codegen: ${{ steps.filter.outputs.codegen }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
codegen:
- 'packages/rtk-query-codegen-openapi/**'

build:
needs: changes
if: ${{ needs.changes.outputs.codegen == 'true' }}

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: 'yarn'

- run: yarn install
- run: yarn test
17 changes: 17 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ defaults:
working-directory: ./packages/toolkit

jobs:
changes:
name: Check for changes
runs-on: ubuntu-latest
outputs:
toolkit: ${{ steps.filter.outputs.toolkit }}
steps:
- uses: actions/checkout@v2
- uses: dorny/paths-filter@v2
id: filter
with:
filters: |
toolkit:
- 'packages/toolkit/**'

build:
needs: changes
if: ${{ needs.changes.outputs.toolkit == 'true' }}

name: Lint, Test, Build & Pack on Node ${{ matrix.node }}

runs-on: ubuntu-latest
Expand Down
171 changes: 120 additions & 51 deletions docs/rtk-query/usage/code-generation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,139 @@ RTK Query's API and architecture is oriented around declaring API endpoints up f

We have early previews of code generation capabilities available as separate tools.

## GraphQL

We provide a [Plugin for GraphQL Codegen](https://www.graphql-code-generator.com/docs/plugins/typescript-rtk-query). You can find the documentation to that on the graphql-codegen homepage.

For a full example on how to use it, you can see [this example project](https://github.com/reduxjs/redux-toolkit/tree/master/examples/query/react/graphql-codegen).

## OpenAPI

We have a first version of a code generator from OpenAPI schemas over at [`rtk-incubator/rtk-query-codegen`](https://github.com/rtk-incubator/rtk-query-codegen).
We provide a package for RTK Query code generation from OpenAPI schemas. It is published as `@rtk-query/codegen-openapi` and you can find the source code at [`packages/rtk-query-codegen-openapi`](https://github.com/reduxjs/redux-toolkit/tree/master/packages/rtk-query-codegen-openapi).

### Usage

Create an empty api using `createApi` like

```ts no-transpile
// Or from '@reduxjs/toolkit/query' if not using the auto-generated hooks
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'

// initialize an empty api service that we'll inject endpoints into later as needed
export const emptySplitApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: '/' }),
endpoints: () => ({}),
})
```

You can create an api by running:
Generate a config file (json, js or ts) with contents like

```ts no-transpile
import { ConfigFile } from '@rtk-query/codegen-openapi'

const config: ConfigFile = {
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
apiFile: './src/store/emptyApi.ts',
apiImport: 'emptyApi',
outputFile: './src/store/petApi.ts',
exportName: 'petApi',
hooks: true,
}

export default config
```

and then call the code generator:

```bash
curl -o petstore.json https://petstore3.swagger.io/api/v3/openapi.json
npx @rtk-incubator/rtk-query-codegen-openapi --hooks petstore.json > petstore-api.generated.ts
npx @rtk-query/codegen-openapi openapi-config.ts
```

We recommend placing these generated types in one file that you do not modify (so you can constantly re-generate it when your API definition changes) and creating a second file to enhance it with additional info:
### Programmatic usage

```ts title="petstore-api.ts"
// file: petstore-api.generated.ts noEmit
export * from 'petstore-api.generated'
```ts no-transpile
import { generateEndpoints } from '@rtk-query/codegen-openapi'

// file: petstoreApi.ts
import { api as generatedApi } from './petstore-api.generated'
const api = await generateEndpoints({
apiFile: './fixtures/emptyApi.ts',
schemaFile: resolve(__dirname, 'fixtures/petstore.json'),
filterEndpoints: ['getPetById', 'addPet'],
hooks: true,
})
```

export const api = generatedApi.enhanceEndpoints({
addTagTypes: ['Pet'],
endpoints: {
// basic notation: just specify properties to be overridden
getPetById: {
providesTags: (result, error, arg) => [{ type: 'Pet', id: arg.petId }],
},
findPetsByStatus: {
providesTags: (result) =>
// is result available?
result
? // successful query
[
{ type: 'Pet', id: 'LIST' },
...result.map((pet) => ({ type: 'Pet' as const, id: pet.id })),
]
: // an error occurred, but we still want to refetch this query when `{ type: 'Pet', id: 'LIST' }` is invalidated
[{ type: 'Pet', id: 'LIST' }],
### Config file options

#### Simple usage

```ts no-transpile
interface SimpleUsage {
apiFile: string
schemaFile: string
apiImport?: string
exportName?: string
argSuffix?: string
responseSuffix?: string
hooks?: boolean
outputFile: string
filterEndpoints?:
| string
| RegExp
| EndpointMatcherFunction
| Array<string | RegExp | EndpointMatcherFunction>
endpointOverrides?: EndpointOverrides[]
}

export type EndpointMatcherFunction = (
operationName: string,
operationDefinition: OperationDefinition
) => boolean
```

#### Filtering endpoints

If you only want to include a few endpoints, you can use the `filterEndpoints` config option to filter your endpoints.

```ts no-transpile
const filteredConfig: ConfigFile = {
// ...
// should only have endpoints loginUser, placeOrder, getOrderById, deleteOrder
filterEndpoints: ['loginUser', /Order/],
}
```

#### Endpoint overrides

If an endpoint is generated as a mutation instead of a query or the other way round, you can override that:

```ts no-transpile
const withOverride: ConfigFile = {
// ...
endpointOverrides: [
{
pattern: 'loginUser',
type: 'mutation',
},
// alternate notation: callback that gets passed in `endpoint` - you can freely modify the object here
addPet: (endpoint) => {
endpoint.invalidatesTags = (result) =>
result ? [{ type: 'Pet', id: result.id }] : []
],
}
```

#### Multiple output files

```ts no-transpile
const config: ConfigFile = {
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
apiFile: './src/store/emptyApi.ts',
outputFiles: {
'./src/store/user.js': {
filterEndpoints: [/user/i],
},
updatePet: {
invalidatesTags: (result, error, arg) => [
{ type: 'Pet', id: arg.pet.id },
],
'./src/store/order.js': {
filterEndpoints: [/order/i],
},
deletePet: {
invalidatesTags: (result, error, arg) => [{ type: 'Pet', id: arg.petId }],
'./src/store/pet.js': {
filterEndpoints: [/pet/i],
},
},
})

export const {
useGetPetByIdQuery,
useFindPetsByStatusQuery,
useAddPetMutation,
useUpdatePetMutation,
useDeletePetMutation,
} = api
}
```

## GraphQL

There is a _very_ early WIP PR that [implements code generation based on a GraphQL spec](https://github.com/phryneas/graphql-code-generator/pull/1), and an open issue on the GraphQL Generator repo asking [if an RTK Query generator would be potentially useful](https://github.com/dotansimha/graphql-code-generator/issues/6085).
3 changes: 1 addition & 2 deletions packages/action-listener-middleware/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"allowSyntheticDefaultImports": true,
"emitDeclarationOnly": true,
"baseUrl": ".",
"paths": {
}
"paths": {}
}
}
11 changes: 3 additions & 8 deletions packages/action-listener-middleware/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src"
],
"exclude": [
"src/**/*.test.ts*",
"src/**/tests/*"
]
}
"include": ["src"],
"exclude": ["src/**/*.test.ts*", "src/**/tests/*"]
}
Loading