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 3 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
29 changes: 21 additions & 8 deletions packages/next-swc/crates/core/src/relay.rs
Expand Up @@ -85,12 +85,13 @@ 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,
real_file_name: &Path,
definition_name: &str,
) -> Result<PathBuf, BuildRequirePathError> {
let filename = match &config.language {
Expand All @@ -102,8 +103,16 @@ fn path_for_artifact(

if let Some(artifact_directory) = &config.artifact_directory {
Ok(root_dir.join(artifact_directory).join(filename))
} else if real_file_name.starts_with(root_dir.join("./pages")) {
alunyov marked this conversation as resolved.
Show resolved Hide resolved
Err(BuildRequirePathError::ArtifactDirectoryExpected {
file_name: real_file_name.display().to_string(),
})
} else {
Err(BuildRequirePathError::ArtifactDirectoryExpected)
Ok(real_file_name
.parent()
.unwrap()
.join("__generated__")
.join(filename))
}
}

Expand All @@ -113,8 +122,8 @@ impl<'a> Relay<'a> {
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) => {
path_for_artifact(&self.root_dir, self.config, 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 build. Try moving the \
alunyov marked this conversation as resolved.
Show resolved Hide resolved
file outside of `pages` or set the `artifactDirectory` in the \
Relay config file.",
file_name
)
}
};

Expand Down
Expand Up @@ -8,7 +8,8 @@ import {
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 +42,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',
}