Skip to content

Commit 866eb85

Browse files
Lenz Weberbenjie
Lenz Weber
andauthored
Add ExtractSmartTagsPlugin example (#243)
Co-authored-by: Benjie Gillam <[email protected]>
1 parent bde6a48 commit 866eb85

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* This plugin will create a file `smartTags.json` containing all of the smart
3+
* tags gathered from all of the various sources (smart comments, smart tags,
4+
* plugins, etc etc). This provides a relatively easy migration path from using
5+
* smart comments to using smart tags instead. Neither Smart Comments nor Smart
6+
* Tags are "better" - they each have trade offs - you can use which ever one
7+
* matches your teams development flow better (or even mix and match!).
8+
*
9+
* Author phryneas (https://github.com/graphile/graphile.github.io/pull/243)
10+
*/
11+
const { writeFile } = require("fs");
12+
13+
module.exports = builder => {
14+
builder.hook("init", (_, build) => {
15+
function sortStuff(a, b) {
16+
const aSchema =
17+
"namespaceName" in a ? a.namespaceName : a.class.namespaceName;
18+
const bSchema =
19+
"namespaceName" in b ? b.namespaceName : b.class.namespaceName;
20+
return (
21+
aSchema.localeCompare(bSchema) * 100 + a.name.localeCompare(b.name)
22+
);
23+
}
24+
const smart = {
25+
version: 1,
26+
config: {
27+
class: [...build.pgIntrospectionResultsByKind.class]
28+
.sort(sortStuff)
29+
.reduce((acc, pgClass) => {
30+
let attribute = [...pgClass.attributes]
31+
.sort((a, b) => a.name.localeCompare(b.name))
32+
.reduce((acc, pgAttr) => {
33+
const tags =
34+
Object.keys(pgAttr.tags).length > 0 ? pgAttr.tags : undefined;
35+
if (pgAttr.description || tags) {
36+
acc[pgAttr.name] = {
37+
...(pgAttr.description
38+
? { description: pgAttr.description }
39+
: {}),
40+
tags,
41+
};
42+
}
43+
return acc;
44+
}, {});
45+
if (Object.keys(attribute).length === 0) {
46+
attribute = undefined;
47+
}
48+
let constraint = [...pgClass.constraints]
49+
.sort(sortStuff)
50+
.reduce((acc, pgConst) => {
51+
if (pgConst.name.startsWith("FAKE_")) {
52+
return acc;
53+
}
54+
const tags =
55+
Object.keys(pgConst.tags).length > 0
56+
? pgConst.tags
57+
: undefined;
58+
if (pgConst.class && (pgConst.description || tags)) {
59+
acc[pgConst.class.namespaceName + "." + pgConst.name] = {
60+
...(pgConst.description
61+
? { description: pgConst.description }
62+
: {}),
63+
tags,
64+
};
65+
}
66+
return acc;
67+
}, {});
68+
if (Object.keys(constraint).length === 0) {
69+
constraint = undefined;
70+
}
71+
const tags =
72+
Object.keys(pgClass.tags).length > 0 ? pgClass.tags : undefined;
73+
if (pgClass.description || tags || attribute)
74+
acc[pgClass.namespaceName + "." + pgClass.name] = {
75+
...(pgClass.description
76+
? { description: pgClass.description }
77+
: {}),
78+
tags,
79+
attribute,
80+
constraint,
81+
};
82+
return acc;
83+
}, {}),
84+
procedure: [...build.pgIntrospectionResultsByKind.procedure]
85+
.sort(sortStuff)
86+
.reduce((acc, pgProc) => {
87+
if (pgProc.name.startsWith("FAKE_")) {
88+
return acc;
89+
}
90+
const tags =
91+
Object.keys(pgProc.tags).length > 0 ? pgProc.tags : undefined;
92+
if (pgProc.description || tags) {
93+
acc[pgProc.namespaceName + "." + pgProc.name] = {
94+
...(pgProc.description
95+
? { description: pgProc.description }
96+
: {}),
97+
tags,
98+
};
99+
}
100+
return acc;
101+
}, {}),
102+
},
103+
};
104+
writeFile(
105+
__dirname + "/smartTags.json",
106+
JSON.stringify(smart, undefined, 2),
107+
e => {
108+
console.log(e);
109+
}
110+
);
111+
return _;
112+
});
113+
};

examples/plugins/config.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
const fs = require("fs");
2+
const path = require("path");
23
const vm = require("vm");
34
const graphqlDiff = require("../_diff");
45

5-
exports.fileFilter = f => f.match(/^[^.].*\.js/);
6+
exports.fileFilter = f => f.match(/^[^.].*\.js$/);
67

78
exports.processFile = async (
89
exampleFilePath,
910
{ prettify, postgraphileSchema, getPostGraphileSchemaWithOptions }
1011
) => {
1112
const source = fs.readFileSync(exampleFilePath, "utf8");
1213
const fakeModule = { exports: {} };
13-
vm.runInThisContext(`((module, exports, require) => {${source}})`)(
14+
vm.runInThisContext(`((module, exports, require, __dirname) => {${source}})`)(
1415
fakeModule,
1516
fakeModule.exports,
16-
require
17+
require,
18+
path.dirname(exampleFilePath)
1719
);
1820
const plugin = fakeModule.exports;
1921
const newSchema = await getPostGraphileSchemaWithOptions({

src/data/examples.json

+13
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@
118118
}
119119
]
120120
},
121+
{
122+
"category": "plugins",
123+
"title": "Other",
124+
"examples": [
125+
{
126+
"title": "ExtractSmartTagsPlugin",
127+
"example": "/**\n * This plugin will create a file `smartTags.json` containing all of the smart\n * tags gathered from all of the various sources (smart comments, smart tags,\n * plugins, etc etc). This provides a relatively easy migration path from using\n * smart comments to using smart tags instead. Neither Smart Comments nor Smart\n * Tags are \"better\" - they each have trade offs - you can use which ever one\n * matches your teams development flow better (or even mix and match!).\n *\n * Author phryneas (https://github.com/graphile/graphile.github.io/pull/243)\n */\nconst { writeFile } = require(\"fs\");\n\nmodule.exports = builder => {\n builder.hook(\"init\", (_, build) => {\n function sortStuff(a, b) {\n const aSchema =\n \"namespaceName\" in a\n ? a.namespaceName\n : a.class.namespaceName;\n const bSchema =\n \"namespaceName\" in b\n ? b.namespaceName\n : b.class.namespaceName;\n return (\n aSchema.localeCompare(\n bSchema\n ) *\n 100 +\n a.name.localeCompare(b.name)\n );\n }\n const smart = {\n version: 1,\n config: {\n class: [\n ...build\n .pgIntrospectionResultsByKind\n .class,\n ]\n .sort(sortStuff)\n .reduce((acc, pgClass) => {\n let attribute = [\n ...pgClass.attributes,\n ]\n .sort((a, b) =>\n a.name.localeCompare(\n b.name\n )\n )\n .reduce(\n (acc, pgAttr) => {\n const tags =\n Object.keys(\n pgAttr.tags\n ).length > 0\n ? pgAttr.tags\n : undefined;\n if (\n pgAttr.description ||\n tags\n ) {\n acc[\n pgAttr.name\n ] = {\n ...(pgAttr.description\n ? {\n description:\n pgAttr.description,\n }\n : {}),\n tags,\n };\n }\n return acc;\n },\n {}\n );\n if (\n Object.keys(attribute)\n .length === 0\n ) {\n attribute = undefined;\n }\n let constraint = [\n ...pgClass.constraints,\n ]\n .sort(sortStuff)\n .reduce(\n (acc, pgConst) => {\n if (\n pgConst.name.startsWith(\n \"FAKE_\"\n )\n ) {\n return acc;\n }\n const tags =\n Object.keys(\n pgConst.tags\n ).length > 0\n ? pgConst.tags\n : undefined;\n if (\n pgConst.class &&\n (pgConst.description ||\n tags)\n ) {\n acc[\n pgConst.class\n .namespaceName +\n \".\" +\n pgConst.name\n ] = {\n ...(pgConst.description\n ? {\n description:\n pgConst.description,\n }\n : {}),\n tags,\n };\n }\n return acc;\n },\n {}\n );\n if (\n Object.keys(constraint)\n .length === 0\n ) {\n constraint = undefined;\n }\n const tags =\n Object.keys(\n pgClass.tags\n ).length > 0\n ? pgClass.tags\n : undefined;\n if (\n pgClass.description ||\n tags ||\n attribute\n )\n acc[\n pgClass.namespaceName +\n \".\" +\n pgClass.name\n ] = {\n ...(pgClass.description\n ? {\n description:\n pgClass.description,\n }\n : {}),\n tags,\n attribute,\n constraint,\n };\n return acc;\n }, {}),\n procedure: [\n ...build\n .pgIntrospectionResultsByKind\n .procedure,\n ]\n .sort(sortStuff)\n .reduce((acc, pgProc) => {\n if (\n pgProc.name.startsWith(\n \"FAKE_\"\n )\n ) {\n return acc;\n }\n const tags =\n Object.keys(pgProc.tags)\n .length > 0\n ? pgProc.tags\n : undefined;\n if (\n pgProc.description ||\n tags\n ) {\n acc[\n pgProc.namespaceName +\n \".\" +\n pgProc.name\n ] = {\n ...(pgProc.description\n ? {\n description:\n pgProc.description,\n }\n : {}),\n tags,\n };\n }\n return acc;\n }, {}),\n },\n };\n writeFile(\n __dirname + \"/smartTags.json\",\n JSON.stringify(\n smart,\n undefined,\n 2\n ),\n e => {\n console.log(e);\n }\n );\n return _;\n });\n};\n",
128+
"exampleLanguage": "javascript",
129+
"result": "",
130+
"resultLanguage": "diff"
131+
}
132+
]
133+
},
121134
{
122135
"category": "queries",
123136
"title": "Basic",

0 commit comments

Comments
 (0)