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

Add support for devOnlyAssignments #94

Open
wants to merge 1 commit 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
15 changes: 12 additions & 3 deletions src/formatGeneratedModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import addAnyTypeCast from "./addAnyTypeCast";
export const formatterFactory = (
compilerOptions: ts.CompilerOptions = {}
): FormatModule => ({
moduleName,
documentType,
docText,
concreteText,
typeText,
hash,
relayRuntimeModule = "relay-runtime",
sourceHash
sourceHash,
devOnlyAssignments
}) => {
const documentTypeImport = documentType
? `import { ${documentType} } from "${relayRuntimeModule}";`
Expand All @@ -23,13 +22,23 @@ export const formatterFactory = (
if (compilerOptions.noImplicitAny) {
nodeStatement = addAnyTypeCast(nodeStatement).trim();
}
const devOnlyAssignmentsText =
devOnlyAssignments != null && devOnlyAssignments.length > 0
? `\nif (process.env.NODE_ENV !== 'production') {\n ${devOnlyAssignments.replace(
// `devOnlyAssignments` uses a flow comment, we can simply replace it with a
// ts cast.
"(node/*: any*/)",
"(node as any)"
)}\n}`
: "";
Copy link
Member

@alloy alloy Mar 12, 2019

Choose a reason for hiding this comment

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

I think we should just mimic the upstream behaviour, which is to always do this when queries are being persisted https://github.com/facebook/relay/blob/42033ebcc298af7e8ed90632b70077b7545a282d/packages/relay-compiler/codegen/writeRelayGeneratedFile.js#L107-L133

return `/* tslint:disable */

${documentTypeImport}
${typeText || ""}

${docTextComment}
${nodeStatement}
${devOnlyAssignmentsText}
(node as any).hash = '${sourceHash}';
export default node;
`;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/generated-module/complete-example-no-cast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export type CompleteExample = { readonly id: string }


const node: ConcreteFragment = {"the":{"fragment":{"data":42}}};

(node as any).hash = 'edcba';
export default node;
1 change: 1 addition & 0 deletions test/fixtures/generated-module/complete-example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export type CompleteExample = { readonly id: string }


const node: ConcreteFragment = ({ "the": { "fragment": { "data": 42 } } } as any);

(node as any).hash = 'edcba';
export default node;
13 changes: 13 additions & 0 deletions test/fixtures/generated-module/dev-only-assignments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* tslint:disable */

import { ConcreteFragment } from "relay-runtime";
export type CompleteExample = { readonly id: string }


const node: ConcreteFragment = {"the":{"fragment":{"data":42}}};

if (process.env.NODE_ENV !== 'production') {
(node as any).params.text = "query CompleteExampleQuery { id }";
}
(node as any).hash = 'edcba';
export default node;
14 changes: 14 additions & 0 deletions test/formatGeneratedModule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,18 @@ describe('formatGeneratedModule', () => {
sourceHash: 'edcba',
})).toMatchFile(join(__dirname, 'fixtures/generated-module/complete-example-no-cast.ts'))
})

it('works with devOnlyAssignments', () => {
const formatGeneratedModule = formatterFactory();
expect(formatGeneratedModule({
moduleName: 'complete-example',
documentType: 'ConcreteFragment',
docText: null,
concreteText: JSON.stringify({ the: { fragment: { data: 42 } }}),
typeText: 'export type CompleteExample = { readonly id: string }',
hash: 'abcde',
sourceHash: 'edcba',
devOnlyAssignments: '(node/*: any*/).params.text = "query CompleteExampleQuery { id }";',
Copy link
Member

Choose a reason for hiding this comment

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

As per the upstream behaviour, the actual query text should be taken from the generated AST node: https://github.com/facebook/relay/blob/42033ebcc298af7e8ed90632b70077b7545a282d/packages/relay-compiler/codegen/writeRelayGeneratedFile.js#L110

Copy link
Member

Choose a reason for hiding this comment

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

Scratch this, I wasn’t paying attention to the filename, as per my below comment.

})).toMatchFile(join(__dirname, 'fixtures/generated-module/dev-only-assignments.ts'))
})
})