Skip to content

Commit 7e80265

Browse files
DominicGBauerDominic
andauthored
feat(typescript-rtk-query): add importAlternateApiName to conifg options (#75)
* feat: add importAlternateApiName to conifg options * Create changeset.md --------- Co-authored-by: Dominic <[email protected]>
1 parent a27104d commit 7e80265

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

.changeset/changeset.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@graphql-codegen/typescript-rtk-query": minor
3+
---
4+
5+
[typescript-rtk-query] feat: add importAlternateApiName to conifg options
6+
7+
Add functionality to allow users to use the optional config variable importAlternateApiName to change the api name of the import used by baseApi. It will default to 'api'.

packages/plugins/typescript/rtk-query/src/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,32 @@ export interface RTKConfig {
2424
* ```
2525
*/
2626
importBaseApiFrom: string;
27+
/**
28+
* @name importBaseApiAlternateName
29+
* @description Change the import name of the baseApi from default 'api'
30+
* @default 'api'
31+
*
32+
33+
* @exampleMarkdown
34+
* ```ts filename="codegen.ts"
35+
* import type { CodegenConfig } from '@graphql-codegen/cli';
36+
*
37+
* const config: CodegenConfig = {
38+
* // ...
39+
* generates: {
40+
* 'path/to/file.ts': {
41+
* plugins: ['typescript', 'typescript-resolvers', 'typescript-rtk-query'],
42+
* config: {
43+
* importBaseApiFrom: 'src/app/api/baseApi',
44+
* importBaseApiAlternateName: 'alternateApiName'
45+
* },
46+
* },
47+
* },
48+
* };
49+
* export default config;
50+
* ```
51+
*/
52+
importBaseApiAlternateName?: string;
2753
/**
2854
* @name exportHooks
2955
* @description Whether to export React Hooks from the generated api. Enable only when using the `"@reduxjs/toolkit/query/react"` import of `createApi`

packages/plugins/typescript/rtk-query/src/visitor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class RTKQueryVisitor extends ClientSideBaseVisitor<RTKQueryRawPluginConf
2525
super(schema, fragments, rawConfig, {
2626
documentMode: DocumentMode.string,
2727
importBaseApiFrom: getConfigValue(rawConfig.importBaseApiFrom, ''),
28+
importBaseApiAlternateName: getConfigValue(rawConfig.importBaseApiAlternateName, 'api'),
2829
exportHooks: getConfigValue(rawConfig.exportHooks, false),
2930
overrideExisting: getConfigValue(rawConfig.overrideExisting, ''),
3031
});
@@ -49,7 +50,10 @@ export class RTKQueryVisitor extends ClientSideBaseVisitor<RTKQueryRawPluginConf
4950
return baseImports;
5051
}
5152

52-
return [...baseImports, `import { api } from '${this.config.importBaseApiFrom}';`];
53+
return [
54+
...baseImports,
55+
`import { ${this.config.importBaseApiAlternateName} } from '${this.config.importBaseApiFrom}';`,
56+
];
5357
}
5458

5559
public getInjectCall() {

packages/plugins/typescript/rtk-query/tests/__snapshots__/rtk-query.spec.ts.snap

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,75 @@ const injectedRtkApi = api.injectEndpoints({
7878
export { injectedRtkApi as api };
7979
export const { useCommentQuery, useLazyCommentQuery, useFeedQuery, useLazyFeedQuery, useSubmitRepositoryMutation } = injectedRtkApi;
8080
81+
"
82+
`;
83+
84+
exports[`RTK Query With importBaseApiAlternateName 1`] = `
85+
"
86+
export const CommentDocument = \`
87+
query Comment($repoFullName: String!, $limit: Int, $offset: Int) {
88+
currentUser {
89+
login
90+
html_url
91+
}
92+
entry(repoFullName: $repoFullName) {
93+
id
94+
postedBy {
95+
login
96+
html_url
97+
}
98+
createdAt
99+
comments(limit: $limit, offset: $offset) {
100+
...CommentsPageComment
101+
}
102+
commentCount
103+
repository {
104+
full_name
105+
html_url
106+
... on Repository {
107+
description
108+
open_issues_count
109+
stargazers_count
110+
}
111+
}
112+
}
113+
}
114+
\${CommentsPageCommentFragmentDoc}\`;
115+
export const FeedDocument = \`
116+
query Feed($type: FeedType!, $offset: Int, $limit: Int) {
117+
currentUser {
118+
login
119+
}
120+
feed(type: $type, offset: $offset, limit: $limit) {
121+
...FeedEntry
122+
}
123+
}
124+
\${FeedEntryFragmentDoc}\`;
125+
export const SubmitRepositoryDocument = \`
126+
mutation submitRepository($repoFullName: String!) {
127+
submitRepository(repoFullName: $repoFullName) {
128+
createdAt
129+
}
130+
}
131+
\`;
132+
133+
const injectedRtkApi = api.injectEndpoints({
134+
endpoints: (build) => ({
135+
Comment: build.query<CommentQuery, CommentQueryVariables>({
136+
query: (variables) => ({ document: CommentDocument, variables })
137+
}),
138+
Feed: build.query<FeedQuery, FeedQueryVariables>({
139+
query: (variables) => ({ document: FeedDocument, variables })
140+
}),
141+
submitRepository: build.mutation<SubmitRepositoryMutation, SubmitRepositoryMutationVariables>({
142+
query: (variables) => ({ document: SubmitRepositoryDocument, variables })
143+
}),
144+
}),
145+
});
146+
147+
export { injectedRtkApi as api };
148+
149+
81150
"
82151
`;
83152

packages/plugins/typescript/rtk-query/tests/rtk-query.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ describe('RTK Query', () => {
4545

4646
expect(content.content).toMatchSnapshot();
4747
});
48+
4849
test('With hooks', async () => {
4950
const documents = parse(gql.commentQuery + gql.feedQuery + gql.newEntryMutation);
5051
const docs = [{ location: '', document: documents }];
@@ -65,6 +66,7 @@ describe('RTK Query', () => {
6566

6667
expect(content.content).toMatchSnapshot();
6768
});
69+
6870
test('With overrideExisting', async () => {
6971
const documents = parse(gql.commentQuery + gql.feedQuery + gql.newEntryMutation);
7072
const docs = [{ location: '', document: documents }];
@@ -87,6 +89,28 @@ describe('RTK Query', () => {
8789

8890
expect(content.content).toMatchSnapshot();
8991
});
92+
93+
test('With importBaseApiAlternateName', async () => {
94+
const documents = parse(gql.commentQuery + gql.feedQuery + gql.newEntryMutation);
95+
const docs = [{ location: '', document: documents }];
96+
97+
const content = (await plugin(
98+
schema,
99+
docs,
100+
{
101+
importBaseApiFrom: './baseApi',
102+
importBaseApiAlternateName: 'alternateApiName',
103+
},
104+
{
105+
outputFile: 'graphql.ts',
106+
}
107+
)) as Types.ComplexPluginOutput;
108+
109+
expect(content.prepend).toContain("import { alternateApiName } from './baseApi';");
110+
111+
expect(content.content).toMatchSnapshot();
112+
});
113+
90114
test('For fragment', async () => {
91115
const documents = parse(gql.voteButtonsFragment);
92116
const docs = [{ location: '', document: documents }];

0 commit comments

Comments
 (0)