Unverified Commit 831d45d7 authored by Andrey Lunyov's avatar Andrey Lunyov Committed by GitHub

Add support for Relay projects without `artifactDirectory` (#33918)

* update plugin

* Update the test

* remove dbg

* Use pages_dir in Relay

* copy edits

* cargo test
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent e3e62aa7
...@@ -112,7 +112,7 @@ pub fn custom_before_pass( ...@@ -112,7 +112,7 @@ pub fn custom_before_pass(
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
let relay_plugin = { let relay_plugin = {
if let Some(config) = &opts.relay { if let Some(config) = &opts.relay {
Either::Left(relay::relay(config, file.name.clone())) Either::Left(relay::relay(config, file.name.clone(), opts.pages_dir.clone()))
} else { } else {
Either::Right(noop()) Either::Right(noop())
} }
......
...@@ -24,6 +24,7 @@ impl Default for RelayLanguageConfig { ...@@ -24,6 +24,7 @@ impl Default for RelayLanguageConfig {
struct Relay<'a> { struct Relay<'a> {
root_dir: PathBuf, root_dir: PathBuf,
pages_dir: PathBuf,
file_name: FileName, file_name: FileName,
config: &'a Config, config: &'a Config,
} }
...@@ -85,36 +86,44 @@ impl<'a> Fold for Relay<'a> { ...@@ -85,36 +86,44 @@ impl<'a> Fold for Relay<'a> {
#[derive(Debug)] #[derive(Debug)]
enum BuildRequirePathError { enum BuildRequirePathError {
FileNameNotReal, FileNameNotReal,
ArtifactDirectoryExpected, ArtifactDirectoryExpected { file_name: String },
} }
fn path_for_artifact( impl<'a> Relay<'a> {
root_dir: &Path, fn path_for_artifact(
config: &Config, &self,
real_file_name: &Path,
definition_name: &str, definition_name: &str,
) -> Result<PathBuf, BuildRequirePathError> { ) -> Result<PathBuf, BuildRequirePathError> {
let filename = match &config.language { let filename = match &self.config.language {
RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name), RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name),
RelayLanguageConfig::TypeScript => { RelayLanguageConfig::TypeScript => {
format!("{}.graphql.ts", definition_name) format!("{}.graphql.ts", definition_name)
} }
}; };
if let Some(artifact_directory) = &config.artifact_directory { if let Some(artifact_directory) = &self.config.artifact_directory {
Ok(root_dir.join(artifact_directory).join(filename)) Ok(self.root_dir.join(artifact_directory).join(filename))
} else if real_file_name.starts_with(&self.pages_dir) {
Err(BuildRequirePathError::ArtifactDirectoryExpected {
file_name: real_file_name.display().to_string(),
})
} else { } else {
Err(BuildRequirePathError::ArtifactDirectoryExpected) Ok(real_file_name
.parent()
.unwrap()
.join("__generated__")
.join(filename))
}
} }
}
impl<'a> Relay<'a> {
fn build_require_path( fn build_require_path(
&mut self, &mut self,
operation_name: &str, operation_name: &str,
) -> Result<PathBuf, BuildRequirePathError> { ) -> Result<PathBuf, BuildRequirePathError> {
match &self.file_name { match &self.file_name {
FileName::Real(_real_file_name) => { FileName::Real(real_file_name) => {
path_for_artifact(&self.root_dir, self.config, operation_name) self.path_for_artifact(real_file_name, operation_name)
} }
_ => Err(BuildRequirePathError::FileNameNotReal), _ => Err(BuildRequirePathError::FileNameNotReal),
} }
...@@ -140,10 +149,14 @@ impl<'a> Relay<'a> { ...@@ -140,10 +149,14 @@ impl<'a> Relay<'a> {
file. This is likely a bug and \ file. This is likely a bug and \
should be reported to Next.js" should be reported to Next.js"
.to_string(), .to_string(),
BuildRequirePathError::ArtifactDirectoryExpected => { BuildRequirePathError::ArtifactDirectoryExpected { file_name } => {
"The `artifactDirectory` is expected to be set in the Relay config \ format!(
file to work correctly with Next.js." "The generated file for `{}` will be created in `pages` \
.to_string() directory, which will break production builds. Try moving the \
file outside of `pages` or set the `artifactDirectory` in the \
Relay config file.",
file_name
)
} }
}; };
...@@ -161,10 +174,15 @@ impl<'a> Relay<'a> { ...@@ -161,10 +174,15 @@ impl<'a> Relay<'a> {
} }
} }
pub fn relay<'a>(config: &'a Config, file_name: FileName) -> impl Fold + '_ { pub fn relay<'a>(
config: &'a Config,
file_name: FileName,
pages_dir: Option<PathBuf>,
) -> impl Fold + '_ {
Relay { Relay {
root_dir: std::env::current_dir().unwrap(), root_dir: std::env::current_dir().unwrap(),
file_name, file_name,
pages_dir: pages_dir.unwrap_or_else(|| panic!("pages_dir is expected.")),
config, config,
} }
} }
...@@ -160,7 +160,13 @@ fn relay_no_artifact_dir_fixture(input: PathBuf) { ...@@ -160,7 +160,13 @@ fn relay_no_artifact_dir_fixture(input: PathBuf) {
}; };
test_fixture( test_fixture(
syntax(), syntax(),
&|_tr| relay(&config, FileName::Real(PathBuf::from("input.tsx"))), &|_tr| {
relay(
&config,
FileName::Real(PathBuf::from("input.tsx")),
Some(PathBuf::from("src/pages")),
)
},
&input, &input,
&output, &output,
); );
......
...@@ -2,13 +2,13 @@ import { ...@@ -2,13 +2,13 @@ import {
Environment, Environment,
FetchFunction, FetchFunction,
fetchQuery, fetchQuery,
graphql,
Network, Network,
RecordSource, RecordSource,
Store, Store,
} from 'relay-runtime' } from 'relay-runtime'
import { GetServerSideProps } from 'next' import { GetServerSideProps } from 'next'
import { pagesQuery } from '../__generated__/pagesQuery.graphql' import { pagesQuery as pagesQueryType } from '../queries/__generated__/pagesQuery.graphql'
import pagesQuery from '../queries/pagesQuery'
type Props = { greeting: string } type Props = { greeting: string }
...@@ -41,13 +41,9 @@ export const getServerSideProps: GetServerSideProps = async ({ req }) => { ...@@ -41,13 +41,9 @@ export const getServerSideProps: GetServerSideProps = async ({ req }) => {
network: Network.create(createGraphQLFetcher(req.headers.host)), network: Network.create(createGraphQLFetcher(req.headers.host)),
}) })
const result = await fetchQuery<pagesQuery>( const result = await fetchQuery<pagesQueryType>(
environment, environment,
graphql` pagesQuery,
query pagesQuery {
greeting
}
`,
{} {}
).toPromise() ).toPromise()
......
import { graphql } from 'relay-runtime'
export default graphql`
query pagesQuery {
greeting
}
`
module.exports = { module.exports = {
src: './pages', src: './pages',
schema: './schema.graphql', schema: './schema.graphql',
artifactDirectory: './__generated__',
language: 'typescript', language: 'typescript',
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment