Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Relay projects without artifactDirectory #33918

Merged
merged 9 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/src/lib.rs
Expand Up @@ -112,7 +112,7 @@ pub fn custom_before_pass(
#[cfg(not(target_arch = "wasm32"))]
let relay_plugin = {
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 {
Either::Right(noop())
}
Expand Down
68 changes: 43 additions & 25 deletions packages/next-swc/crates/core/src/relay.rs
Expand Up @@ -24,6 +24,7 @@ impl Default for RelayLanguageConfig {

struct Relay<'a> {
root_dir: PathBuf,
pages_dir: PathBuf,
file_name: FileName,
config: &'a Config,
}
Expand Down Expand Up @@ -85,36 +86,44 @@ impl<'a> Fold for Relay<'a> {
#[derive(Debug)]
enum BuildRequirePathError {
FileNameNotReal,
ArtifactDirectoryExpected,
ArtifactDirectoryExpected { file_name: String },
}

fn path_for_artifact(
root_dir: &Path,
config: &Config,
definition_name: &str,
) -> Result<PathBuf, BuildRequirePathError> {
let filename = match &config.language {
RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name),
RelayLanguageConfig::TypeScript => {
format!("{}.graphql.ts", definition_name)
}
};
impl<'a> Relay<'a> {
fn path_for_artifact(
&self,
real_file_name: &Path,
definition_name: &str,
) -> Result<PathBuf, BuildRequirePathError> {
let filename = match &self.config.language {
RelayLanguageConfig::Flow => format!("{}.graphql.js", definition_name),
RelayLanguageConfig::TypeScript => {
format!("{}.graphql.ts", definition_name)
}
};

if let Some(artifact_directory) = &config.artifact_directory {
Ok(root_dir.join(artifact_directory).join(filename))
} else {
Err(BuildRequirePathError::ArtifactDirectoryExpected)
if let Some(artifact_directory) = &self.config.artifact_directory {
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 {
Ok(real_file_name
.parent()
.unwrap()
.join("__generated__")
.join(filename))
}
}
}

impl<'a> Relay<'a> {
fn build_require_path(
&mut self,
operation_name: &str,
) -> Result<PathBuf, BuildRequirePathError> {
match &self.file_name {
FileName::Real(_real_file_name) => {
path_for_artifact(&self.root_dir, self.config, operation_name)
FileName::Real(real_file_name) => {
self.path_for_artifact(real_file_name, operation_name)
}
_ => Err(BuildRequirePathError::FileNameNotReal),
}
Expand All @@ -140,10 +149,14 @@ impl<'a> Relay<'a> {
file. This is likely a bug and \
should be reported to Next.js"
.to_string(),
BuildRequirePathError::ArtifactDirectoryExpected => {
"The `artifactDirectory` is expected to be set in the Relay config \
file to work correctly with Next.js."
.to_string()
BuildRequirePathError::ArtifactDirectoryExpected { file_name } => {
format!(
"The generated file for `{}` will be created in `pages` \
directory, which will break production builds. Try moving the \
file outside of `pages` or set the `artifactDirectory` in the \
Relay config file.",
file_name
)
}
};

Expand All @@ -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 {
root_dir: std::env::current_dir().unwrap(),
file_name,
pages_dir: pages_dir.unwrap_or_else(|| panic!("pages_dir is expected.")),
config,
}
}
8 changes: 7 additions & 1 deletion packages/next-swc/crates/core/tests/fixture.rs
Expand Up @@ -160,7 +160,13 @@ fn relay_no_artifact_dir_fixture(input: PathBuf) {
};
test_fixture(
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,
&output,
);
Expand Down
Expand Up @@ -2,13 +2,13 @@ import {
Environment,
FetchFunction,
fetchQuery,
graphql,
Network,
RecordSource,
Store,
} from 'relay-runtime'
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 }

Expand Down Expand Up @@ -41,13 +41,9 @@ export const getServerSideProps: GetServerSideProps = async ({ req }) => {
network: Network.create(createGraphQLFetcher(req.headers.host)),
})

const result = await fetchQuery<pagesQuery>(
const result = await fetchQuery<pagesQueryType>(
environment,
graphql`
query pagesQuery {
greeting
}
`,
pagesQuery,
{}
).toPromise()

Expand Down
@@ -0,0 +1,7 @@
import { graphql } from 'relay-runtime'

export default graphql`
query pagesQuery {
greeting
}
`
@@ -1,6 +1,5 @@
module.exports = {
src: './pages',
schema: './schema.graphql',
artifactDirectory: './__generated__',
language: 'typescript',
}