Skip to content

feat(c-event-mutation): add eventCreateMutation #50

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions src/components/event/Root.tsx → src/components/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import Box from "@mui/material/Box";

import { Outlet } from "react-router-dom";

import EventCreateDialog from "./event/eventCreate/EventCreateDialog";

const Root = () => {

return (
<>
<Box sx={{ flexGrow: 1 }}>
Expand All @@ -19,9 +22,7 @@ const Root = () => {
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
GraphQL Relay Web
</Typography>
<Button color="inherit" variant="outlined">
New Event
</Button>
<EventCreateDialog />
</Toolbar>
</AppBar>
<Box
Expand Down
32 changes: 32 additions & 0 deletions src/components/event/eventCreate/mutations/EventCreateMutation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { graphql } from "react-relay";
import { ROOT_ID, SelectorStoreUpdater } from "relay-runtime";

import { connectionUpdater } from "../../../../relay";

export const EventCreate = graphql`
mutation EventCreateMutation($input: EventCreateInput!) {
EventCreate(input: $input) {
event {
id
name
start
end
allDay
}
error
success
}
}
`;

export const updater: SelectorStoreUpdater = (store) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the new directives from relay to update connections https://relay.dev/docs/guided-tour/list-data/updating-connections/

const newEdge = store.getRootField("EventCreate").getLinkedRecord("event");

connectionUpdater({
store,
parentId: ROOT_ID,
//connectionName: 'Feed_posts',
edge: newEdge,
before: true,
});
};
1 change: 1 addition & 0 deletions src/relay/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as Environment } from './RelayEnvironment';
export { connectionUpdater, connectionDeleteEdgeUpdater } from './mutationUtils';
73 changes: 73 additions & 0 deletions src/relay/mutationUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { ConnectionHandler, RecordProxy, RecordSourceProxy } from 'relay-runtime';

type ConnectionUpdater = {
store: RecordSourceProxy;
parentId: string;
connectionName: string;
edge: RecordProxy;
before?: boolean;
};

export function connectionUpdater({ store, parentId, connectionName, edge, before = false }: ConnectionUpdater) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do not need these helpers since now has https://relay.dev/docs/guided-tour/list-data/updating-connections/

if (edge) {
if (!parentId) {
// eslint-disable-next-line
console.log('maybe you forgot to pass a parentId: ');
return;
}

const parentProxy = store.get(parentId);

if (!parentProxy) {
// eslint-disable-next-line
console.log(`Parent proxy not found for "${parentId}"`);
return;
}

const conn = ConnectionHandler.getConnection(parentProxy, connectionName);
if (!conn) {
// eslint-disable-next-line
console.log('maybe this connection is not in relay store: ', connectionName);
return;
}

if (before) {
ConnectionHandler.insertEdgeBefore(conn, edge);
} else {
ConnectionHandler.insertEdgeAfter(conn, edge);
}
}
}

type ConnectionDeleteEdgeUpdaterOptions = {
parentId: string;
connectionName: string;
nodeId: string;
store: RecordSourceProxy;
filters?: object;
};

export function connectionDeleteEdgeUpdater({
parentId,
connectionName,
nodeId,
store,
}: ConnectionDeleteEdgeUpdaterOptions) {
const parentProxy = store.get(parentId);

if (!parentProxy) {
// eslint-disable-next-line
console.log(`Parent proxy not found for "${parentId}"`);
return;
}

const conn = ConnectionHandler.getConnection(parentProxy, connectionName);

if (!conn) {
// eslint-disable-next-line
console.warn(`Connection ${connectionName} not found on ${parentId}`);
return;
}

ConnectionHandler.deleteNode(conn, nodeId);
}
2 changes: 1 addition & 1 deletion src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BrowserRouter, Route, Routes as Router } from 'react-router-dom';

import EventHome from './components/event/EventHome';

import Root from './components/event/Root';
import Root from './components/Root';

function Routes() {
return (
Expand Down