Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

feat: generated auto-typed hooks #305

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./lib/hooks");
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
],
"main": "lib/index.js",
"files": [
"lib"
"lib",
"hooks.js"
],
"scripts": {
"build": "rm -rf lib && tsc --project tsconfig.build.json",
Expand Down Expand Up @@ -83,8 +84,8 @@
"@types/jest": "^26.0.20",
"@types/node": "14.14.28",
"@types/relay-compiler": "^8.0.0",
"@types/relay-runtime": "^10.0.1",
"babel-plugin-relay": "^10.0.0",
"@types/relay-runtime": "^10.1.10",
"babel-plugin-relay": "^11.0.0",
"chokidar-cli": "^2.0.0",
"concurrently": "^5.0.0",
"glob": "^7.1.6",
Expand All @@ -94,20 +95,20 @@
"jest-cli": "^26.6.3",
"lint-staged": "^10.5.3",
"prettier": "^2.2.1",
"relay-compiler": "^10.0.1",
"relay-runtime": "^10.0.1",
"relay-test-utils-internal": "^10.0.1",
"relay-compiler": "^11.0.0",
"relay-runtime": "^11.0.0",
"relay-test-utils-internal": "^11.0.0",
"ts-jest": "^26.4.4",
"ts-node": "^9.1.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0",
"typescript": "4.1.5"
},
"peerDependencies": {
"@types/react-relay": ">=7.0.0",
"@types/relay-runtime": ">=6.0.7",
"relay-compiler": ">=9.0.0",
"relay-runtime": ">=9.0.0",
"@types/react-relay": "^11.0.0",
"@types/relay-runtime": ">=10.1.10",
"relay-compiler": ">=11.0.0",
"relay-runtime": ">=11.0.0",
Comment on lines +110 to +111
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this I'm assuming this change will likely need to be apart of a major version bump.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can ship this and the remaining breaking changes I have in my branch as a new major version next month while also dropping node 10

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, yeah. Given that there's an extra entry point (a sort of opt-in if you will) I feel like we could likely loosen this requirement with the caveat of course that if you're using hooks version you'll need to be on v11. Not sure if it's worth encoding that as a dependency or runtime constraint vs just documenting it.

"typescript": ">=3.6.4"
},
"publishConfig": {
Expand Down
47 changes: 30 additions & 17 deletions src/formatGeneratedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,49 @@ import { FormatModule } from "relay-compiler";
import * as ts from "typescript";
import addAnyTypeCast from "./addAnyTypeCast";

interface FormatterOptions {
makeImports: (moduleDetails: Parameters<FormatModule>[0]) => string;
append?: string;
}

const defaultFormatOptions: FormatterOptions = {
makeImports({ documentType }) {
return documentType
? `import { ${documentType} } from "relay-runtime";`
: "";
},
};

export const formatterFactory = (
compilerOptions: ts.CompilerOptions = {}
): FormatModule => ({
moduleName,
documentType,
docText,
concreteText,
typeText,
hash,
sourceHash
}) => {
const documentTypeImport = documentType
? `import { ${documentType} } from "relay-runtime";`
: "";
compilerOptions: ts.CompilerOptions = {},
{ makeImports, append }: FormatterOptions = defaultFormatOptions
): FormatModule => (details) => {
const {
documentType,
docText,
concreteText,
typeText,
hash,
sourceHash,
} = details;

const docTextComment = docText ? "\n/*\n" + docText.trim() + "\n*/\n" : "";
let nodeStatement = `const node: ${documentType ||
"never"} = ${concreteText};`;
let nodeStatement = `const node: ${
documentType || "never"
} = ${concreteText};`;
if (compilerOptions.noImplicitAny) {
nodeStatement = addAnyTypeCast(nodeStatement).trim();
}
return `/* tslint:disable */
/* eslint-disable */
// @ts-nocheck
${hash ? `/* ${hash} */\n` : ""}
${documentTypeImport}
${makeImports(details)}
${typeText || ""}

${docTextComment}
${nodeStatement}
(node as any).hash = '${sourceHash}';
export default node;
`;
${append ? `\n${append}\n` : ""}`;
};
Loading