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

feat: allow passing cacheExchange and other fixes #24

Closed
wants to merge 5 commits into from
Closed
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ The graphql query to perform.

The cache policy to use with this mutation request.

#### `cacheExchange`

Replace `cacheExchange` from `@urql/core` with provided one.

```MARKO
import { cacheExchange } from '@urql/exchange-graphcache';

<gql-client
...input
cacheExchange=cacheExchange({
resolvers: {
Todo: {
updatedAt: parent => new Date(parent.updatedAt),
},
},
})
/>
```

#### `name`

The name of cooresponding Urql client.
Expand Down
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/node": "^20.10.4",
"@typescript-eslint/eslint-plugin": "^6.14.0",
"@typescript-eslint/parser": "^6.14.0",
"@urql/exchange-graphcache": "^7.1.0",
"convert-source-map": "^2.0.0",
"esbuild": "^0.19.9",
"eslint": "^8.55.0",
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ import "./alias-virtual-modules";
const schema = buildSchema(`
type Query {
hello(name: String): String
todo(id: String): Todo
messages: [String]
}

type Todo {
id: String
name: String
}

type Mutation {
addMessage(text: String): String
doError: String
Expand All @@ -32,6 +38,13 @@ const createRoot = (contentPostfix = "", delay = 0) => {
await new Promise((r) => setTimeout(r, delay));
return `Hello ${name}!${contentPostfix}`;
},
todo: async ({ id }: { id: string }) => {
await new Promise((r) => setTimeout(r, delay));
return {
id,
name: "Todo " + id + contentPostfix,
};
},
messages: async () => {
await new Promise((r) => setTimeout(r, delay));
return messages;
Expand Down
7 changes: 7 additions & 0 deletions src/components/gql-client/marko-tag.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@
"description": "Default cache policy for requests."
}
]
},
"@cacheExchange": {
"autocomplete": [
{
"description": "Replace default cache exchange."
}
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<noscript
id="GENERATED-0"
/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div>
<noscript
id="GENERATED-0"
/>
</div>
<div
id="GENERATED-1"
style="display:none"
>
<span>
Todo John
</span>
<button>
Toggle
</button>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<span>
Todo John
</span>
<button>
Toggle
</button>
</div>
<div
id="GENERATED-0"
style="display:none"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<span>
Todo John from custom resolver
</span>
<button>
Toggle
</button>
</div>
<div
id="GENERATED-0"
style="display:none"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div>
<span>
Stale
</span>
<span>
Todo John from custom resolver
</span>
<button>
Toggle
</button>
</div>
<div
id="GENERATED-0"
style="display:none"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<span>
Todo Jake from custom resolver
</span>
<button>
Toggle
</button>
</div>
<div
id="GENERATED-0"
style="display:none"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<span>
Todo John from custom resolver
</span>
<button>
Toggle
</button>
</div>
<div
id="GENERATED-0"
style="display:none"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<span>
Todo Jake from custom resolver
</span>
<button>
Toggle
</button>
</div>
<div
id="GENERATED-0"
style="display:none"
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { gql } from "../../../../../../index";
import { cacheExchange } from '@urql/exchange-graphcache';

static const QUERY = gql`
query($id: String) {
todo(id: $id) {
id
name
}
}
`;
class {
onCreate() {
this.state = { id: "John" }
}
handleClick() {
this.state.id = this.state.id === "John" ? "Jake" : "John"
}
}

<gql-client url=input.graphqlURL cacheExchange=cacheExchange({
resolvers: {
Todo: {
name: (parent) => {
return `${parent.name} from custom resolver`;
}
},
},
}) />
<gql-query query=QUERY variables={ id: state.id }>
<@then|{ data, fetching, error }|>
<if(fetching)>
<span>Stale</span>
</if>
<span>${data?.todo.name}</span>
<button on-click("handleClick")>Toggle</button>
</@then>
</gql-query>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Example</title>
<esbuild-assets/>
</head>
<body>
<app ...input />
</body>
</html>
9 changes: 9 additions & 0 deletions src/components/gql-query/__tests__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,12 @@ describe(
async (page) => await page.click("text=Toggle"),
]),
);

describe(
"use-graphcache-exchange",
fixture(path.join(__dirname, "fixtures/use-graphcache-exchange"), [
async (page) => await page.click("text=Toggle"),
async (page) => await page.click("text=Toggle"),
async (page) => await page.click("text=Toggle"),
]),
);
4 changes: 3 additions & 1 deletion src/node_modules/@internal/client/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ssrExchange,
type ClientOptions,
type Client,
type Exchange,
} from "@urql/core";

import { devtoolsExchange } from "@internal/client/devtools";
Expand All @@ -32,14 +33,15 @@ export function configureClient(
...params: [...Parameters<typeof fetch>, typeof fetch]
) => ReturnType<typeof fetch>;
name?: string;
cacheExchange?: Exchange;
},
_out: any,
) {
const clientProvider: ClientProvider = {};
clientProvider.ssr = ssrExchange({ isClient: true });
let exchanges = [
config.cacheExchange ?? cacheExchange,
dedupExchange,
cacheExchange,
clientProvider.ssr,
fetchExchange,
];
Expand Down
2 changes: 1 addition & 1 deletion src/node_modules/@internal/client/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export function configureClient(
out: any,
) {
let exchanges = [
dedupExchange,
cacheExchange,
dedupExchange,
ssrExchange({ isClient: false }),
fetchExchange,
];
Expand Down