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

fix(relay): Fix interop with server actions of next.js #306

Merged
merged 5 commits into from
May 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions packages/relay/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::Deserialize;
use swc_atoms::JsWord;
use swc_common::{FileName, Mark, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::{quote_ident, ExprFactory};
use swc_ecma_utils::{prepend_stmts, quote_ident, ExprFactory};
use swc_ecma_visit::{Fold, FoldWith};

#[derive(Copy, Clone, Debug, Deserialize)]
Expand Down Expand Up @@ -165,16 +165,17 @@ impl<'a> Fold for Relay<'a> {
}

fn fold_module_items(&mut self, items: Vec<ModuleItem>) -> Vec<ModuleItem> {
let items = items
let mut items = items
.into_iter()
.map(|item| item.fold_children_with(self))
.collect::<Vec<_>>();

self.imports
.iter()
.map(|import| import.as_module_item())
.chain(items)
.collect()
prepend_stmts(
&mut items,
self.imports.iter().map(|import| import.as_module_item()),
);

items
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { graphql } from "relay-runtime";
import { revalidatePath } from "next/cache";

export async function setValue(string) {
"use server";

// comment out the next two statements to successfully compile
const taggedNode = graphql`
mutation actionsSetThingMutation($value: String!) {
setRocket(value: $value) {
name
}
}
`
console.log({ taggedNode })

const response = await fetch("http://localhost:3000/graphql", {
body: JSON.stringify({
// replace this query with the taggedNode value
query: `
mutation actionsSetThingMutation($value: String!) {
setThing(value: $value) {
name
}
}
`,
variables: {
value: string,
},
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});

const data = await response.json();

// the ui should flush this value when revalidated
console.log(data);

revalidatePath("/", 'page');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import __actionsSetThingMutation from "./__generated__/actionsSetThingMutation.graphql.ts";
import { graphql } from "relay-runtime";
import { revalidatePath } from "next/cache";
export async function setValue(string) {
"use server";
// comment out the next two statements to successfully compile
const taggedNode = __actionsSetThingMutation;
console.log({
taggedNode
});
const response = await fetch("http://localhost:3000/graphql", {
body: JSON.stringify({
// replace this query with the taggedNode value
query: `
mutation actionsSetThingMutation($value: String!) {
setThing(value: $value) {
name
}
}
`,
variables: {
value: string
}
}),
headers: {
"Content-Type": "application/json"
},
method: "POST"
});
const data = await response.json();
// the ui should flush this value when revalidated
console.log(data);
revalidatePath("/", 'page');
}
;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use server";

import { graphql } from "relay-runtime";
import { revalidatePath } from "next/cache";

export async function setValue(string) {
// comment out the next two statements to successfully compile
const taggedNode = graphql`
mutation actionsSetThingMutation($value: String!) {
setRocket(value: $value) {
name
}
}
`
console.log({ taggedNode })

const response = await fetch("http://localhost:3000/graphql", {
body: JSON.stringify({
// replace this query with the taggedNode value
query: `
mutation actionsSetThingMutation($value: String!) {
setThing(value: $value) {
name
}
}
`,
variables: {
value: string,
},
}),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});

const data = await response.json();

// the ui should flush this value when revalidated
console.log(data);

revalidatePath("/", 'page');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use server";
import __actionsSetThingMutation from "./__generated__/actionsSetThingMutation.graphql.ts";
import { graphql } from "relay-runtime";
import { revalidatePath } from "next/cache";
export async function setValue(string) {
// comment out the next two statements to successfully compile
const taggedNode = __actionsSetThingMutation;
console.log({
taggedNode
});
const response = await fetch("http://localhost:3000/graphql", {
body: JSON.stringify({
// replace this query with the taggedNode value
query: `
mutation actionsSetThingMutation($value: String!) {
setThing(value: $value) {
name
}
}
`,
variables: {
value: string
}
}),
headers: {
"Content-Type": "application/json"
},
method: "POST"
});
const data = await response.json();
// the ui should flush this value when revalidated
console.log(data);
revalidatePath("/", 'page');
}
;
Loading