Skip to content

Commit 0551099

Browse files
committed
first working version
1 parent 5db099f commit 0551099

11 files changed

+340
-2531
lines changed

.eslintrc.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ module.exports = {
2929
},
3030
},
3131
],
32+
'no-underscore-dangle': [
33+
'error',
34+
{
35+
allowAfterThis: true,
36+
allowAfterSuper: true,
37+
allowAfterThisConstructor: true,
38+
},
39+
],
3240
'no-restricted-imports': 'off',
41+
'@typescript-eslint/no-explicit-any': 'off',
3342
'@typescript-eslint/explicit-module-boundary-types': 'off',
3443
},
3544
settings: {
@@ -38,13 +47,19 @@ module.exports = {
3847
},
3948
'import/resolver': {
4049
typescript: {
41-
project: 'packages/*/tsconfig.json',
50+
project: './tsconfig.json',
4251
},
4352
node: {
4453
extensions: ['.js', '.ts', '.jsx', '.tsx', '.json'],
4554
},
4655
},
4756
},
4857
},
58+
{
59+
files: ['./src/lib/**/*.ts'],
60+
rules: {
61+
'import/prefer-default-export': 'off',
62+
},
63+
},
4964
],
5065
};

.vscode/settings.json

+1-13
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,5 @@
3737
},
3838
"[typescriptreact]": {
3939
"editor.defaultFormatter": "esbenp.prettier-vscode"
40-
},
41-
"eslint.validate": [
42-
"javascript",
43-
"javascriptreact",
44-
"typescript",
45-
"typescriptreact",
46-
"graphql"
47-
],
48-
"eslint.options": {
49-
"extentions": [".js", ".graphql"]
50-
},
51-
"typescript.tsdk": "node_modules/typescript/lib",
52-
"typescript.enablePromptUseWorkspaceTsdk": true
40+
}
5341
}

package.json

+13-28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-codegen-plugin-typescript-swr",
3-
"version": "1.0.0",
3+
"version": "0.0.1",
44
"description": "A GraphQL code generator plug-in that automatically generates utility functions for SWR.",
55
"main": "build/main/index.js",
66
"typings": "build/main/index.d.ts",
@@ -36,37 +36,29 @@
3636
"cov:html": "nyc report --reporter=html",
3737
"cov:lcov": "nyc report --reporter=lcov",
3838
"cov:send": "run-s cov:lcov && codecov",
39-
"cov:check": "nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100",
40-
"doc": "run-s doc:html && open-cli build/docs/index.html",
41-
"doc:html": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --out build/docs",
42-
"doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
43-
"doc:publish": "gh-pages -m \"[ci skip] Updates\" -d build/docs",
44-
"version": "standard-version",
45-
"reset-hard": "git clean -dfx && git reset --hard && yarn",
46-
"prepare-release": "run-s reset-hard test cov:check doc:html version doc:publish"
47-
},
48-
"engines": {
49-
"node": ">=10"
39+
"cov:check": "nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100"
5040
},
5141
"dependencies": {
5242
"@graphql-codegen/plugin-helpers": "^1.17.9",
5343
"@graphql-codegen/visitor-plugin-common": "^1.17.15",
5444
"auto-bind": "^4.0.0",
55-
"camel-case": "^4.1.1",
5645
"graphql": "^15.3.0",
46+
"graphql-request": "^3.1.0",
5747
"pascal-case": "^3.1.1",
5848
"swr": "^0.3.5",
5949
"tslib": "^2.0.3"
6050
},
51+
"peerDependencies": {
52+
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0",
53+
"graphql-tag": "^2.0.0"
54+
},
6155
"devDependencies": {
6256
"@ava/typescript": "^1.1.1",
6357
"@istanbuljs/nyc-config-typescript": "^1.0.1",
6458
"@types/node": "^14.11.8",
6559
"@typescript-eslint/eslint-plugin": "^4.4.1",
6660
"ava": "^3.13.0",
6761
"codecov": "^3.8.0",
68-
"cspell": "^4.1.0",
69-
"cz-conventional-changelog": "^3.3.0",
7062
"eslint": "^7.11.0",
7163
"eslint-config-airbnb": "^18.2.0",
7264
"eslint-config-airbnb-typescript": "^11.0.0",
@@ -76,23 +68,24 @@
7668
"eslint-plugin-jsx-a11y": "^6.3.1",
7769
"eslint-plugin-react": "^7.21.4",
7870
"eslint-plugin-react-hooks": "^4.1.2",
79-
"gh-pages": "^3.1.0",
8071
"npm-run-all": "^4.1.5",
8172
"nyc": "^15.1.0",
8273
"open-cli": "^6.0.1",
8374
"prettier": "^2.1.2",
8475
"react": "^16.13.1",
85-
"standard-version": "^9.0.0",
86-
"ts-node": "^9.0.0",
87-
"typedoc": "^0.19.2",
8876
"typescript": "^4.0.3"
8977
},
78+
"resolutions": {
79+
"graphql": "15.3.0",
80+
"graphql-language-service-interface": "2.4.0",
81+
"**/apollo-language-server/graphql": "^15.0.0",
82+
"**/@types/graphql-upload/graphql": "^15.0.0"
83+
},
9084
"files": [
9185
"build/main",
9286
"build/module",
9387
"!**/*.spec.*",
9488
"!**/*.json",
95-
"CHANGELOG.md",
9689
"LICENSE",
9790
"README.md"
9891
],
@@ -108,14 +101,6 @@
108101
"!build/module/**"
109102
]
110103
},
111-
"config": {
112-
"commitizen": {
113-
"path": "cz-conventional-changelog"
114-
}
115-
},
116-
"prettier": {
117-
"singleQuote": true
118-
},
119104
"nyc": {
120105
"extends": "@istanbuljs/nyc-config-typescript",
121106
"exclude": [

src/config.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common';
1+
import { RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-common'
22

33
/**
44
* @description This plugin generates [`graphql-request`](https://www.npmjs.com/package/graphql-request) ready-to-use SDK, which is fully-typed.
@@ -12,14 +12,15 @@ export interface RawSWRPluginConfig extends RawClientSideBasePluginConfig {
1212
* @exampleMarkdown
1313
* ```yml
1414
* generates:
15-
* path/to/file.ts:
16-
* plugins:
17-
* - typescript
18-
* - typescript-operations
19-
* - typescript-graphql-request
20-
* config:
21-
* rawRequest: true
15+
* path/to/file.ts:
16+
* plugins:
17+
* - typescript
18+
* - typescript-operations
19+
* - typescript-graphql-request
20+
* - graphql-codegen-plugin-typescript-swr
21+
* config:
22+
* rawRequest: true
2223
* ```
2324
*/
24-
rawRequest?: boolean;
25+
rawRequest?: boolean
2526
}

src/index.ts

+34-18
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,58 @@
1-
import { extname } from 'path';
1+
import { extname } from 'path'
22

33
import {
44
Types,
55
PluginValidateFn,
66
PluginFunction,
7-
} from '@graphql-codegen/plugin-helpers';
8-
import {
9-
RawClientSideBasePluginConfig,
10-
LoadedFragment,
11-
} from '@graphql-codegen/visitor-plugin-common';
7+
} from '@graphql-codegen/plugin-helpers'
8+
import { LoadedFragment } from '@graphql-codegen/visitor-plugin-common'
129
import {
1310
visit,
1411
GraphQLSchema,
1512
concatAST,
1613
Kind,
1714
FragmentDefinitionNode,
18-
} from 'graphql';
15+
} from 'graphql'
1916

20-
import { RawSWRPluginConfig } from './config';
21-
import { SWRVisitor } from './visitor';
17+
import { RawSWRPluginConfig } from './config'
18+
import { SWRVisitor } from './visitor'
2219

2320
export const plugin: PluginFunction<RawSWRPluginConfig> = (
2421
schema: GraphQLSchema,
2522
documents: Types.DocumentFile[],
26-
config: RawGraphQLRequestPluginConfig
23+
config: RawSWRPluginConfig
2724
) => {
28-
const allAst = concatAST(documents.map((v) => v.document));
29-
};
25+
const allAst = concatAST(documents.map((v) => v.document))
26+
27+
const allFragments: LoadedFragment[] = [
28+
...(allAst.definitions.filter(
29+
(d) => d.kind === Kind.FRAGMENT_DEFINITION
30+
) as FragmentDefinitionNode[]).map((fragmentDef) => ({
31+
node: fragmentDef,
32+
name: fragmentDef.name.value,
33+
onType: fragmentDef.typeCondition.name.value,
34+
isExternal: false,
35+
})),
36+
...(config.externalFragments || []),
37+
]
38+
39+
const visitor = new SWRVisitor(schema, allFragments, config)
40+
visit(allAst, { leave: visitor })
41+
return {
42+
prepend: visitor.getImports(),
43+
content: visitor.sdkContent,
44+
}
45+
}
3046

3147
export const validate: PluginValidateFn<any> = async (
32-
schema: GraphQLSchema,
33-
documents: Types.DocumentFile[],
34-
config: RawClientSideBasePluginConfig,
48+
_schema: GraphQLSchema,
49+
_documents: Types.DocumentFile[],
50+
_config: RawSWRPluginConfig,
3551
outputFile: string
3652
) => {
3753
if (extname(outputFile) !== '.ts') {
38-
throw new Error(`Plugin "typescript-swr" requires extension to be ".ts"!`);
54+
throw new Error(`Plugin "typescript-swr" requires extension to be ".ts"!`)
3955
}
40-
};
56+
}
4157

42-
export { SWRVisitor };
58+
export { SWRVisitor }

src/lib/number.spec.ts

-11
This file was deleted.

src/lib/number.ts

-51
This file was deleted.

src/types/example.d.ts

-24
This file was deleted.

0 commit comments

Comments
 (0)