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

Relay support does not work with multiple artifact directories #64890

Closed
mmmulani opened this issue Apr 22, 2024 · 11 comments · Fixed by vercel/turborepo#8218, #67378, vercel/turborepo#8646 or vercel/turborepo#8706
Assignees
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked SWC Related to minification/transpilation in Next.js.

Comments

@mmmulani
Copy link

mmmulani commented Apr 22, 2024

Link to the code that reproduces this issue

https://github.com/mmmulani/relay-multi/tree/main

To Reproduce

Note: The linked github repo has a workaround in place.

This issue is about using Relay's multi-project configuration to have graphQL queries in different "projects" where one project can reference another. The most common example of this is something like a "web" folder that uses graphQL fragments in a "shared" folder.

This is

  1. Create a NextJS project in web-app.
  2. Add a multi-project Relay config to your web-app package.json, e.g.
  "relay": {
    "root": "..",
    "sources": {
      "shared": "shared",
      "web-app": "web-app"
    },
    "projects": {
      "shared": {
        "language": "typescript",
        "output": "shared/__generated__",
        "schema": "shared/schema.graphql"
      },
      "web-app": {
        "base": "shared",
        "language": "typescript",
        "output": "web-app/__generated__",
        "schema": "shared/schema.graphql"
      }
    }
  },
  1. Create a component in shared/ that uses graphQL, e.g. TestComponent.tsx with:
const countryFragment = graphql`
  fragment TestComponent_country on Country {
    name
    phone
    currency
    emoji
  }
`
  1. Try importing shared/TestComponent inside web-app/

Current vs. Expected behavior

Current behaviour: the import fails as the Relay SWC plugin expects the Relay generated graphQL file to live in web-app/__generated__ rather than shared/__generated__

Module not found: Can't resolve '/relay-multi/web-app/./__generated__/TestComponent_country.graphql.ts'

You would expect there to be some way to specify the multi project config inside the next.config.js file so that the plugin knows to look in shared/__generated__ rather than web-app/__generated__.

Workaround

You can workaround this by modifying module resolution in next.config.js with:

const path = require("path");
const webpack = require("webpack");

function graphQLRewrite() {
  // This plugin works around an issue with Next.js's Relay support.
  // Next.js's Relay plugin will convert the code like:
  //   const userFragment = graphql`...`
  // into:
  //   const userFragment = import('<artifactDirectory>/UserFragment.graphql.ts')
  // which works for Next.js projects with a single Relay folder. However, in our setup we have Relay files in
  // @cv/shared-web, with generated artifacts in that folder as well.
  // This plugin will rewrite the import statements for those graphql usages to match the correct artifactDirectory.
  return new webpack.NormalModuleReplacementPlugin(/\.graphql\.ts$/, (resource) => {
    const repoRoot = path.resolve(__dirname, '..'); // relay-multi/
    const sourceFile = resource.contextInfo.issuer; // relay-multi/shared/TestComponent.tsx

    const fileProjectDir = sourceFile.slice(repoRoot.length).split('/')[1]; // shared
    const currentRequest = path.resolve(resource.request); // relay-multi/<some dir>/__generated__/TestComponent_country.graphql.ts
    const fileName = path.basename(currentRequest); // TestComponent_country.graphql.ts

    const correctGraphQLPath = path.resolve(repoRoot, fileProjectDir, '__generated__', fileName);
    resource.request = correctGraphQLPath;
  });
}

const nextConfig = {
  compiler: {
    relay: {
      src: './',
      artifactDirectory: './__generated__',
      language: 'typescript',
    },
  },
  webpack: (config) => {
    config.plugins.push(graphQLRewrite());
    return config;
  },
};

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:34 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 21.6.2
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.2.2 // Latest available version is detected (14.2.2).
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

SWC

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local)

Additional context

No response

PACK-2986

@mmmulani mmmulani added the bug Issue was opened via the bug report template. label Apr 22, 2024
@github-actions github-actions bot added the SWC Related to minification/transpilation in Next.js. label Apr 22, 2024
@mmmulani mmmulani changed the title Relay support does not work with multiple artifiact directories Relay support does not work with multiple artifact directories Apr 22, 2024
@mmmulani
Copy link
Author

cc @tbezman based on #33240 and @alunyov in case he has any ideas for how best to interpret a Relay config to determine the generated artifact's path

@kdy1 kdy1 self-assigned this Apr 23, 2024
@kdy1 kdy1 added the linear: turbopack Confirmed issue that is tracked by the Turbopack team. label Apr 23, 2024
@kdy1
Copy link
Member

kdy1 commented May 8, 2024

What's the expected "fix"? Should the relay plugin accept multiple artifact paths?

@mmmulani
Copy link
Author

mmmulani commented May 8, 2024

yeah I think having the relay plugin allow for some multiple artifact paths would work. alternatively, some way to provide a function to generate the artifact path though i'm not sure if this is possible given the relay plugin is written in rust (and probably would be much slower)

the ideal solution would be parsing the relay config, and determining the expected artifact path from that.

@mmmulani
Copy link
Author

hey, how does this work now? was looking through https://github.com/swc-project/plugins/commits/main/packages/relay for some example usage but couldn't find any

Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 12, 2024
@kdy1
Copy link
Member

kdy1 commented Jun 13, 2024

Oops this is not fixed

@kdy1 kdy1 reopened this Jun 13, 2024
@vercel vercel unlocked this conversation Jun 13, 2024
@kdy1 kdy1 reopened this Jun 16, 2024
@kdy1
Copy link
Member

kdy1 commented Jun 29, 2024

swc-project/plugins#318 adds multi-project support to swc_relay.
I'll close this issue once I update next.js

@mmmulani
Copy link
Author

whoa awesome, looks like a lot of work so thanks a ton!

is there any way to use this with older next versions? or is upgrading just the best way? (in case, maybe the swc plugin api changed?)

@kdy1
Copy link
Member

kdy1 commented Jul 1, 2024

One plugin API is added and it's documented at https://github.com/swc-project/plugins/tree/aff55e5c6e1aceb18cfab49addb946f573fd13a4/packages/relay#example

You may configure @swc/relay Wasm plugin directly via experimental.swcPlugins like https://nextjs.org/docs/architecture/nextjs-compiler#swc-plugins-experimental

@kdy1
Copy link
Member

kdy1 commented Jul 10, 2024

Reopening as it's reverted

@kdy1 kdy1 reopened this Jul 10, 2024
kdy1 added a commit to vercel/turborepo that referenced this issue Jul 12, 2024
### Description

Update swc crates

### Testing Instructions


See [next.js counterpart](vercel/next.js#67378)


 - Closes vercel/next.js#64890
 - Closes vercel/next.js#63104
kdy1 added a commit that referenced this issue Jul 12, 2024
# Turbopack
* vercel/turborepo#8686 <!-- Tobias Koppers -
improve performance of the graph aggregation -->
* vercel/turborepo#8694 <!-- Benjamin Woodruff -
Replace `unreachable` with `bail` in `finalize_css` -->
* vercel/turborepo#8661 <!-- Benjamin Woodruff -
Create a minimum viable `ResolvedVc` type -->
* vercel/turborepo#8662 <!-- Benjamin Woodruff - Add
a minimum viable ResolvedValue marker trait -->
* vercel/turborepo#8678 <!-- Benjamin Woodruff - Add
derive macro for ResolvedValue -->
* vercel/turborepo#8715 <!-- Benjamin Woodruff -
Switch RcStr from std::sync::Arc to triomphe::Arc -->
* vercel/turborepo#8699 <!-- Donny/강동윤 - perf: Merge
multiple `EsmBinding` -->
* vercel/turborepo#8720 <!-- Benjamin Woodruff - Add
support for `#[turbo_tasks::value(resolved)]` and
`#[turbo_tasks::value_trait(resolved)]` -->
* vercel/turborepo#8706 <!-- Donny/강동윤 - build:
Update `swc_core` to `v0.96.9` -->

### What?

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3

### Why?

To keep in sync

### How?

 - Closes #64890
 - Closes #63104
ForsakenHarmony pushed a commit that referenced this issue Jul 25, 2024
### Description

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3



 - Closes #64890
 - Closes #63104


### Testing Instructions

See #67378
ForsakenHarmony pushed a commit that referenced this issue Jul 25, 2024
### Description

Update swc crates

### Testing Instructions


See [next.js counterpart](#67378)


 - Closes #64890
 - Closes #63104
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 26, 2024
ForsakenHarmony pushed a commit that referenced this issue Jul 29, 2024
### Description

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3



 - Closes #64890
 - Closes #63104


### Testing Instructions

See #67378
ForsakenHarmony pushed a commit that referenced this issue Jul 29, 2024
### Description

Update swc crates

### Testing Instructions


See [next.js counterpart](#67378)


 - Closes #64890
 - Closes #63104
ForsakenHarmony pushed a commit that referenced this issue Jul 29, 2024
### Description

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3



 - Closes #64890
 - Closes #63104


### Testing Instructions

See #67378
ForsakenHarmony pushed a commit that referenced this issue Jul 29, 2024
### Description

Update swc crates

### Testing Instructions


See [next.js counterpart](#67378)


 - Closes #64890
 - Closes #63104
ForsakenHarmony pushed a commit that referenced this issue Aug 1, 2024
### Description

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3



 - Closes #64890
 - Closes #63104


### Testing Instructions

See #67378
ForsakenHarmony pushed a commit that referenced this issue Aug 1, 2024
### Description

Update swc crates

### Testing Instructions


See [next.js counterpart](#67378)


 - Closes #64890
 - Closes #63104
ForsakenHarmony pushed a commit that referenced this issue Aug 14, 2024
* vercel/turborepo#8686 <!-- Tobias Koppers -
improve performance of the graph aggregation -->
* vercel/turborepo#8694 <!-- Benjamin Woodruff -
Replace `unreachable` with `bail` in `finalize_css` -->
* vercel/turborepo#8661 <!-- Benjamin Woodruff -
Create a minimum viable `ResolvedVc` type -->
* vercel/turborepo#8662 <!-- Benjamin Woodruff - Add
a minimum viable ResolvedValue marker trait -->
* vercel/turborepo#8678 <!-- Benjamin Woodruff - Add
derive macro for ResolvedValue -->
* vercel/turborepo#8715 <!-- Benjamin Woodruff -
Switch RcStr from std::sync::Arc to triomphe::Arc -->
* vercel/turborepo#8699 <!-- Donny/강동윤 - perf: Merge
multiple `EsmBinding` -->
* vercel/turborepo#8720 <!-- Benjamin Woodruff - Add
support for `#[turbo_tasks::value(resolved)]` and
`#[turbo_tasks::value_trait(resolved)]` -->
* vercel/turborepo#8706 <!-- Donny/강동윤 - build:
Update `swc_core` to `v0.96.9` -->

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3

To keep in sync

 - Closes #64890
 - Closes #63104
ForsakenHarmony pushed a commit that referenced this issue Aug 15, 2024
* vercel/turborepo#8686 <!-- Tobias Koppers -
improve performance of the graph aggregation -->
* vercel/turborepo#8694 <!-- Benjamin Woodruff -
Replace `unreachable` with `bail` in `finalize_css` -->
* vercel/turborepo#8661 <!-- Benjamin Woodruff -
Create a minimum viable `ResolvedVc` type -->
* vercel/turborepo#8662 <!-- Benjamin Woodruff - Add
a minimum viable ResolvedValue marker trait -->
* vercel/turborepo#8678 <!-- Benjamin Woodruff - Add
derive macro for ResolvedValue -->
* vercel/turborepo#8715 <!-- Benjamin Woodruff -
Switch RcStr from std::sync::Arc to triomphe::Arc -->
* vercel/turborepo#8699 <!-- Donny/강동윤 - perf: Merge
multiple `EsmBinding` -->
* vercel/turborepo#8720 <!-- Benjamin Woodruff - Add
support for `#[turbo_tasks::value(resolved)]` and
`#[turbo_tasks::value_trait(resolved)]` -->
* vercel/turborepo#8706 <!-- Donny/강동윤 - build:
Update `swc_core` to `v0.96.9` -->

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3

To keep in sync

 - Closes #64890
 - Closes #63104
ForsakenHarmony pushed a commit that referenced this issue Aug 16, 2024
* vercel/turborepo#8686 <!-- Tobias Koppers -
improve performance of the graph aggregation -->
* vercel/turborepo#8694 <!-- Benjamin Woodruff -
Replace `unreachable` with `bail` in `finalize_css` -->
* vercel/turborepo#8661 <!-- Benjamin Woodruff -
Create a minimum viable `ResolvedVc` type -->
* vercel/turborepo#8662 <!-- Benjamin Woodruff - Add
a minimum viable ResolvedValue marker trait -->
* vercel/turborepo#8678 <!-- Benjamin Woodruff - Add
derive macro for ResolvedValue -->
* vercel/turborepo#8715 <!-- Benjamin Woodruff -
Switch RcStr from std::sync::Arc to triomphe::Arc -->
* vercel/turborepo#8699 <!-- Donny/강동윤 - perf: Merge
multiple `EsmBinding` -->
* vercel/turborepo#8720 <!-- Benjamin Woodruff - Add
support for `#[turbo_tasks::value(resolved)]` and
`#[turbo_tasks::value_trait(resolved)]` -->
* vercel/turborepo#8706 <!-- Donny/강동윤 - build:
Update `swc_core` to `v0.96.9` -->

Update SWC crates to
swc-project/swc@226617e
and
swc-project/plugins@b7658c3

To keep in sync

 - Closes #64890
 - Closes #63104
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. linear: turbopack Confirmed issue that is tracked by the Turbopack team. locked SWC Related to minification/transpilation in Next.js.
Projects
None yet
2 participants