[codegen-typemap] Initial import
This commit is contained in:
Родитель
e009f3c9cf
Коммит
f7fa1524dc
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"extends": ["../../.eslintrc.json"],
|
||||
"root": true
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
/src/
|
||||
**/__tests__/
|
||||
.eslintrc.json
|
||||
tsconfig.json
|
||||
.tsbuildinfo
|
||||
CHANGELOG.json
|
|
@ -0,0 +1,3 @@
|
|||
# graphql-codegen-typescript-typemap-plugin
|
||||
|
||||
This plugin will emit a `TypeMap` object type that returns the schema types, emitted by the `typescript` plugin, keyed by their name.
|
|
@ -0,0 +1,40 @@
|
|||
{
|
||||
"name": "@graphitation/graphql-codegen-typescript-typemap-plugin",
|
||||
"license": "MIT",
|
||||
"version": "0.0.1",
|
||||
"main": "./src/index.ts",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/microsoft/graphitation.git",
|
||||
"directory": "packages/template"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "monorepo-scripts build",
|
||||
"lint": "monorepo-scripts lint",
|
||||
"test": "monorepo-scripts test",
|
||||
"types": "monorepo-scripts types",
|
||||
"just": "monorepo-scripts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@graphql-codegen/plugin-helpers": "^1.18.2",
|
||||
"@types/jest": "^26.0.22",
|
||||
"graphql": "^15.0.0",
|
||||
"monorepo-scripts": "*"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"graphql": "^15.0.0"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"access": "public",
|
||||
"publishConfig": {
|
||||
"main": "./lib/index",
|
||||
"types": "./lib/index.d.ts",
|
||||
"module": "./lib/index.mjs",
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./lib/index.mjs",
|
||||
"require": "./lib/index.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
import { typeMapPlugin } from ".";
|
||||
import { buildSchema } from "graphql";
|
||||
|
||||
describe(typeMapPlugin, () => {
|
||||
it("only includes public schema types", () => {
|
||||
const schema = buildSchema(`
|
||||
type Query {
|
||||
foo: Foo!
|
||||
}
|
||||
type Foo {
|
||||
id: ID!
|
||||
}
|
||||
`);
|
||||
const result = typeMapPlugin(schema);
|
||||
expect(result).toMatchInlineSnapshot(`
|
||||
"export type TypeMap = {
|
||||
\\"Boolean\\": Scalars[\\"Boolean\\"];
|
||||
\\"Foo\\": Foo;
|
||||
\\"ID\\": Scalars[\\"ID\\"];
|
||||
\\"Query\\": Query;
|
||||
\\"String\\": Scalars[\\"String\\"];
|
||||
};
|
||||
"
|
||||
`);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,28 @@
|
|||
import type { CodegenPlugin } from "@graphql-codegen/plugin-helpers";
|
||||
import { isScalarType } from "graphql";
|
||||
import type { GraphQLSchema } from "graphql";
|
||||
|
||||
export const typeMapPlugin = (schema: GraphQLSchema): string => {
|
||||
const typesMap = schema.getTypeMap();
|
||||
return [
|
||||
"export type TypeMap = {",
|
||||
...Object.keys(typesMap)
|
||||
.sort()
|
||||
.filter((typeName) => !typeName.startsWith("__"))
|
||||
.map(
|
||||
(typeName) =>
|
||||
` "${typeName}": ${
|
||||
isScalarType(typesMap[typeName])
|
||||
? `Scalars["${typeName}"]`
|
||||
: typeName
|
||||
};`,
|
||||
),
|
||||
"};\n",
|
||||
].join("\n");
|
||||
};
|
||||
|
||||
const config: CodegenPlugin = {
|
||||
plugin: typeMapPlugin,
|
||||
};
|
||||
|
||||
export default config;
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"tsBuildInfoFile": ".tsbuildinfo",
|
||||
"rootDir": "src",
|
||||
"outDir": "lib"
|
||||
},
|
||||
"include": ["src"],
|
||||
"references": []
|
||||
}
|
25
yarn.lock
25
yarn.lock
|
@ -1517,6 +1517,17 @@
|
|||
"@n1ru4l/push-pull-async-iterable-iterator" "^3.1.0"
|
||||
meros "^1.1.4"
|
||||
|
||||
"@graphql-codegen/plugin-helpers@^1.18.2":
|
||||
version "1.18.8"
|
||||
resolved "https://registry.yarnpkg.com/@graphql-codegen/plugin-helpers/-/plugin-helpers-1.18.8.tgz#39aac745b9e22e28c781cc07cf74836896a3a905"
|
||||
integrity sha512-mb4I9j9lMGqvGggYuZ0CV+Hme08nar68xkpPbAVotg/ZBmlhZIok/HqW2BcMQi7Rj+Il5HQMeQ1wQ1M7sv/TlQ==
|
||||
dependencies:
|
||||
"@graphql-tools/utils" "^7.9.1"
|
||||
common-tags "1.8.0"
|
||||
import-from "4.0.0"
|
||||
lodash "~4.17.0"
|
||||
tslib "~2.3.0"
|
||||
|
||||
"@graphql-eslint/eslint-plugin@^3.7.0":
|
||||
version "3.7.0"
|
||||
resolved "https://registry.yarnpkg.com/@graphql-eslint/eslint-plugin/-/eslint-plugin-3.7.0.tgz#2fb4d1fb50fbabd9c421ac83570b5f689134b4e8"
|
||||
|
@ -1692,7 +1703,7 @@
|
|||
dependencies:
|
||||
tslib "~2.3.0"
|
||||
|
||||
"@graphql-tools/utils@^7.1.2":
|
||||
"@graphql-tools/utils@^7.1.2", "@graphql-tools/utils@^7.9.1":
|
||||
version "7.10.0"
|
||||
resolved "https://registry.yarnpkg.com/@graphql-tools/utils/-/utils-7.10.0.tgz#07a4cb5d1bec1ff1dc1d47a935919ee6abd38699"
|
||||
integrity sha512-d334r6bo9mxdSqZW6zWboEnnOOFRrAPVQJ7LkU8/6grglrbcu6WhwCLzHb90E94JI3TD3ricC3YGbUqIi9Xg0w==
|
||||
|
@ -3667,6 +3678,11 @@ commander@^8.3.0:
|
|||
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
|
||||
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==
|
||||
|
||||
common-tags@1.8.0:
|
||||
version "1.8.0"
|
||||
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.8.0.tgz#8e3153e542d4a39e9b10554434afaaf98956a937"
|
||||
integrity sha512-6P6g0uetGpW/sdyUy/iQQCbFF0kWVMSIVSyYz7Zgjcgh8mgw8PQzDNZeyZ5DQ2gM7LBoZPHmnjz8rUthkBG5tw==
|
||||
|
||||
component-bind@1.0.0:
|
||||
version "1.0.0"
|
||||
resolved "https://registry.yarnpkg.com/component-bind/-/component-bind-1.0.0.tgz#00c608ab7dcd93897c0009651b1d3a8e1e73bbd1"
|
||||
|
@ -5284,6 +5300,11 @@ import-fresh@^3.0.0, import-fresh@^3.1.0, import-fresh@^3.2.1:
|
|||
parent-module "^1.0.0"
|
||||
resolve-from "^4.0.0"
|
||||
|
||||
import-from@4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/import-from/-/import-from-4.0.0.tgz#2710b8d66817d232e16f4166e319248d3d5492e2"
|
||||
integrity sha512-P9J71vT5nLlDeV8FHs5nNxaLbrpfAV5cF5srvbZfpwpcJoM/xZR3hiv+q+SAnuSmuGbXMWud063iIMx/V/EWZQ==
|
||||
|
||||
import-lazy@~4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/import-lazy/-/import-lazy-4.0.0.tgz#e8eb627483a0a43da3c03f3e35548be5cb0cc153"
|
||||
|
@ -6522,7 +6543,7 @@ lodash.toarray@^4.4.0:
|
|||
resolved "https://registry.yarnpkg.com/lodash.toarray/-/lodash.toarray-4.4.0.tgz#24c4bfcd6b2fba38bfd0594db1179d8e9b656561"
|
||||
integrity sha1-JMS/zWsvuji/0FlNsRedjptlZWE=
|
||||
|
||||
lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4, lodash@^4.7.0:
|
||||
lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4, lodash@^4.7.0, lodash@~4.17.0:
|
||||
version "4.17.21"
|
||||
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
|
||||
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
|
||||
|
|
Загрузка…
Ссылка в новой задаче