Skip to content

Commit

Permalink
Move check for graphql string in source files to language plugin (#2811)
Browse files Browse the repository at this point in the history
Summary:
This moves the check for the string `graphql` in source files to the JS language plugin, away from `RelaySourceModuleParser`. This has two reasons:

1. Language plugins might not require/use the string 'graphql'. I have that case when emitting ReasonML types - Writing GraphQL there looks like `[%relay.mutation ...]` and `[%relay.query ...]`, so `graphql` is not naturally inside any of the source files I want the compiler to pick up.
2. The requirement was quite "hidden" and it took me a few hours to understand why my files weren't picked up even though they seemed to satisfy all requirements of the compiler + configuration. So, also reducing confusion.

If this lands I'll PR the TypeScript language plugin to add the same type of check into that as well.
Pull Request resolved: #2811

Reviewed By: jstejada

Differential Revision: D19856119

Pulled By: alunyov

fbshipit-source-id: 864c75020db6993b0a3702c38a8ab22b7933073a
  • Loading branch information
zth authored and Juan Tejada committed Feb 13, 2020
1 parent 435ffd2 commit 3a6998e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/relay-compiler/bin/RelayCompilerMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ function getCodegenRunner(config: Config): CodegenRunner {
const sourceWriterName = outputExtension;
const sourceModuleParser = RelaySourceModuleParser(
languagePlugin.findGraphQLTags,
languagePlugin.getFileFilter,
);
const providedArtifactDirectory = config.artifactDirectory;
const artifactDirectory =
Expand Down
13 changes: 9 additions & 4 deletions packages/relay-compiler/core/RelaySourceModuleParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ import type {FileFilter} from '../codegen/CodegenWatcher';
import type {GraphQLTagFinder} from '../language/RelayLanguagePluginInterface';
import type {DocumentNode} from 'graphql';

export type GetFileFilter = (baseDir: string) => FileFilter;

export type SourceModuleParser = {|
getFileFilter: (baseDir: string) => FileFilter,
getFileFilter: GetFileFilter,
getParser: (baseDir: string) => ASTCache,
parseFile: (baseDir: string, file: File) => ?DocumentNode,
parseFileWithSources: (
Expand All @@ -42,7 +44,10 @@ export type SourceModuleParser = {|

const parseGraphQL = Profiler.instrument(GraphQL.parse, 'GraphQL.parse');

module.exports = (tagFinder: GraphQLTagFinder): SourceModuleParser => {
module.exports = (
tagFinder: GraphQLTagFinder,
getFileFilter?: GetFileFilter,
): SourceModuleParser => {
const memoizedTagFinder = memoizedFind.bind(null, tagFinder);

function parseFile(baseDir: string, file: File): ?DocumentNode {
Expand Down Expand Up @@ -104,7 +109,7 @@ module.exports = (tagFinder: GraphQLTagFinder): SourceModuleParser => {
});
}

function getFileFilter(baseDir: string): FileFilter {
function defaultGetFileFilter(baseDir: string): FileFilter {
return (file: File) => {
const filePath = path.join(baseDir, file.relPath);
let text = '';
Expand All @@ -123,7 +128,7 @@ module.exports = (tagFinder: GraphQLTagFinder): SourceModuleParser => {

return {
getParser,
getFileFilter,
getFileFilter: getFileFilter ?? defaultGetFileFilter,
parseFile,
parseFileWithSources,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
} from '../codegen/CodegenRunner';
import type {IRTransform} from '../core/CompilerContext';
import type {GeneratedDefinition, Root, Fragment} from '../core/IR';
import type {GetFileFilter} from '../core/RelaySourceModuleParser';
import type {Schema} from '../core/Schema';
import type {ScalarTypeMapping} from './javascript/RelayFlowTypeTransformers';
import type {GeneratedNode} from 'relay-runtime';
Expand All @@ -46,6 +47,7 @@ export type PluginInterface = {
keepExtraFile?: KeepExtraFileFn,
schemaExtensions?: $ReadOnlyArray<string>,
getModuleName?: (operationName: string) => string,
getFileFilter?: GetFileFilter,
...
};

Expand Down

0 comments on commit 3a6998e

Please sign in to comment.