Skip to content

Commit

Permalink
Allow language plugin to specify how modules should be named. (facebo…
Browse files Browse the repository at this point in the history
…ok#2866)

Summary:
This allows the language plugin to specify how modules should be named.

Context: In Reason, `.` is not allowed in file names except for with the file extension. This means that the language plugin I'm working on for Reason needs a way to have modules/generated files be named `OperationName_graphql.re` instead of `OperationName.graphql.js` in order for it to work.

Please let me know what you think!
Pull Request resolved: facebook#2866

Reviewed By: jstejada

Differential Revision: D19815165

Pulled By: alunyov

fbshipit-source-id: 5291023ea72fc276e6f958aa1ef7a42f10b4ee12
  • Loading branch information
zth authored and facebook-github-bot committed Feb 11, 2020
1 parent fe16830 commit e9e4fbb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/relay-compiler/bin/RelayCompilerMain.js
Expand Up @@ -431,6 +431,7 @@ function getRelayFileWriter(
documents,
reporter,
sourceControl,
languagePlugin,
});
if (queryMap != null && persistedQueryPath != null) {
let object = {};
Expand Down
4 changes: 4 additions & 0 deletions packages/relay-compiler/codegen/RelayFileWriter.js
Expand Up @@ -35,6 +35,7 @@ const {Map: ImmutableMap} = require('immutable');
import type {Schema} from '../core/Schema';
import type {
FormatModule,
PluginInterface,
TypeGenerator,
} from '../language/RelayLanguagePluginInterface';
import type {ScalarTypeMapping} from '../language/javascript/RelayFlowTypeTransformers';
Expand Down Expand Up @@ -141,6 +142,7 @@ function writeAll({
schema,
reporter,
sourceControl,
languagePlugin,
}: {|
config: WriterConfig,
onlyValidate: boolean,
Expand All @@ -149,6 +151,7 @@ function writeAll({
schema: Schema,
reporter: Reporter,
sourceControl: ?SourceControl,
languagePlugin?: ?PluginInterface,
|}): Promise<Map<string, CodegenDirectory>> {
return Profiler.asyncContext('RelayFileWriter.writeAll', async () => {
const {
Expand Down Expand Up @@ -314,6 +317,7 @@ function writeAll({
writerConfig.printModuleDependency,
writerConfig.repersist ?? false,
writerConfig.writeQueryParameters ?? function noop() {},
languagePlugin,
);
}),
);
Expand Down
15 changes: 11 additions & 4 deletions packages/relay-compiler/codegen/writeRelayGeneratedFile.js
Expand Up @@ -24,7 +24,10 @@ const {RelayConcreteNode} = require('relay-runtime');

import type {GeneratedDefinition} from '../core/IR';
import type {Schema} from '../core/Schema';
import type {FormatModule} from '../language/RelayLanguagePluginInterface';
import type {
FormatModule,
PluginInterface,
} from '../language/RelayLanguagePluginInterface';
import type CodegenDirectory from './CodegenDirectory';
import type {GeneratedNode, RequestParameters} from 'relay-runtime';

Expand Down Expand Up @@ -64,14 +67,18 @@ async function writeRelayGeneratedFile(
moduleName: string,
params: RequestParameters,
) => void,
languagePlugin: ?PluginInterface,
): Promise<?GeneratedNode> {
let generatedNode = _generatedNode;
// Copy to const so Flow can refine.
const persistQuery = _persistQuery;
const moduleName =
(generatedNode.kind === 'Request'
const operationName =
generatedNode.kind === 'Request'
? generatedNode.params.name
: generatedNode.name) + '.graphql';
: generatedNode.name;
const moduleName = languagePlugin?.getModuleName
? languagePlugin.getModuleName(operationName)
: operationName + '.graphql';

const filename = moduleName + '.' + extension;
const queryParametersFilename =
Expand Down
Expand Up @@ -39,6 +39,7 @@ export type PluginInterface = {
formatModule: FormatModule,
typeGenerator: TypeGenerator,
schemaExtensions?: $ReadOnlyArray<string>,
getModuleName?: (operationName: string) => string,
...
};

Expand Down

0 comments on commit e9e4fbb

Please sign in to comment.