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(graphql): rewrite GraphQL data provider #6357

Merged
merged 34 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
aa7bc90
feat(graphql): add getData and getCount options
BatuhanW Sep 19, 2024
7584f1a
fix: review comments
BatuhanW Sep 20, 2024
956abe0
fix: typescript errors
BatuhanW Sep 24, 2024
4fba40a
chore: remove redundant changes
BatuhanW Sep 24, 2024
c7dac57
feat: updates WIP
BatuhanW Sep 25, 2024
e9d0e90
fix: incorrect versions, also use latest nock for appwrite and graphql
BatuhanW Sep 26, 2024
91deb12
feat: update create, createMany and getList specs
BatuhanW Sep 26, 2024
9c83e83
feat: update create, createMany, getMany, deleteOne, deleteMany methods
BatuhanW Sep 26, 2024
e202f2b
Merge branch 'releases/october' into RK-780-refine-improve-graphql-data
BatuhanW Oct 7, 2024
000fa3e
fix: add lodash to dependencies
BatuhanW Oct 7, 2024
1b6f103
feat: implement missing methods
BatuhanW Oct 7, 2024
7c55d5a
fix(graphql): remove unnecessary dependencies
BatuhanW Oct 7, 2024
d9eec77
feat(graphql): extract buildSorters and buildPagination from options …
BatuhanW Oct 7, 2024
1d4a44a
fix(graphql): issues on graphql util and also cleanup
BatuhanW Oct 7, 2024
a6c185a
feat(graphql): live provider impl
BatuhanW Oct 7, 2024
c4f26c2
feat(graphql): update index exports and swizzle config
BatuhanW Oct 7, 2024
2195d44
fix(graphql): typings for CreateMany, UpdateMany, DeleteMany in defau…
BatuhanW Oct 7, 2024
cda97b8
chore(graphql): update changeset and format syncpackrc
BatuhanW Oct 7, 2024
b1b0871
chore(graphql): revert unnecessary changes
BatuhanW Oct 7, 2024
122b697
feat(graphql): add tests for default options and remove unnecessary ones
BatuhanW Oct 8, 2024
5e6cf74
feat(graphql): specs for getOne and deleteOne
BatuhanW Oct 8, 2024
32637ff
fix(graphql): wrap deleteMany and updateMany responses into an array :(
BatuhanW Oct 9, 2024
a3158dd
fix(graphql): update buildVariables issue
BatuhanW Oct 9, 2024
a916a7c
feat(graphql): finalize specs for update, updateMany, deleteMany, get…
BatuhanW Oct 9, 2024
36c60eb
feat(graphql): custom method specs, also respect to meta.gqlVariables…
BatuhanW Oct 9, 2024
c28e73e
docs(graphql): update docs
BatuhanW Oct 10, 2024
936e9fc
refactor(graphql): adjust exports, peer deps, some types and exported…
BatuhanW Oct 10, 2024
62e33ac
feat(graphql): address review comments
BatuhanW Oct 10, 2024
7581b9a
feat(graphql): make options.ts and options.spec.ts files swizzleable??
BatuhanW Oct 10, 2024
fdb6e59
docs(graphql): update docs
BatuhanW Oct 10, 2024
02c84f6
chore(graphql): add deepmerge to package.json and swizzle deps
BatuhanW Oct 10, 2024
45bc720
chore(graphql): return ids amount of objects from deleteMany and upda…
BatuhanW Oct 14, 2024
0d5947f
fix(graphql): add missing gqlVariables destructures also remove build…
BatuhanW Oct 14, 2024
1fc9b01
chore(graphql): update changeset
BatuhanW Oct 14, 2024
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
10 changes: 10 additions & 0 deletions .changeset/hot-hats-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@refinedev/graphql": major
---

feat: add default options that can be overriden piece by piece.

drop support for usage with `meta.fields`.

[Resolves #5942](https://github.com/refinedev/refine/issues/5942)
[Resolves #5943](https://github.com/refinedev/refine/issues/5943)
2 changes: 1 addition & 1 deletion .syncpackrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
"dependencies": ["nock"],
"dependencyTypes": ["dev"],
"packages": ["@refinedev/appwrite"],
"packages": ["@refinedev/appwrite", "@refinedev/graphql"],
"isIgnored": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion documentation/blog/2024-03-22-useeffect-cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ This is where the `useEffect` cleaning function comes in. In the cleanup functio

## When to use the `useEffect` cleanup function

There are various scenarios which will prompt the use of the `useEffect` cleanup function. They are as follows:
There are various scenarios which will prompt the use of the `useEffect` cleanup function. They are as follows:

### Fetch requests

Expand Down
45 changes: 45 additions & 0 deletions documentation/docs/data/packages/graphql/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,51 @@ const App = () => (
);
```

### Options

It's also possible to pas a 2nd parameter to GraphQL data provider. These options are `getCount` and `getData`.

You can use them to extract response from your GraphQL response.

Let's say you have the following query:

```graphql
query PostList($where: JSON, $sort: String) {
allPosts(where: $where, sort: $sort) {
id
title
content
category {
id
}
}
}
```

By default, our data provider expects a plural form of the resource in the response, so if you have `allPosts`, you would need to swizzle GraphQL data provider and customize it yourself. With these options, we help you extract data from your response. So you don't need to create custom data provider for relatively simple cases.

```ts
import dataProvider, {
GraphQLClient,
defaultGetDataFn,
} from "@refinedev/graphql";
import camelCase from "camelcase";

const client = new GraphQLClient("https://api.example.com/graphql");

const dp = dataProvider(client, {
getData: ({ method, params, response }) => {
if (method === "getList") {
const key = camelCase(`all-${params.resource}`); // -> allPosts

return response[key];
}

return defaultGetDataFn({ method, params, response });
},
});
```

## Realtime

`@refinedev/graphql` also provides a `liveProvider` to enable realtime features of Refine. These features are powered by GraphQL subscriptions and uses [`graphql-ws`](https://the-guild.dev/graphql/ws) to handle the connections.
Expand Down
2 changes: 1 addition & 1 deletion packages/appwrite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/jest": "^29.2.4",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"nock": "14.0.0-beta.8",
"nock": "14.0.0-beta.14",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
"tsup": "^6.7.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
rootDir: "./",
displayName: "graphql",
setupFilesAfterEnv: ["<rootDir>/test/jest.setup.ts"],
testEnvironment: "jsdom",
testEnvironment: "node",
moduleNameMapper: {
"^(..?/.+).js?$": "$1",
},
Expand Down
12 changes: 5 additions & 7 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,30 @@
"types": "node ../shared/generate-declarations.js"
},
"dependencies": {
"@urql/core": "^5.0.6",
"camelcase": "^6.2.0",
"gql-query-builder": "^3.5.5",
"graphql": "^15.6.1",
"graphql-request": "^5.2.0",
"graphql-tag": "^2.12.6",
"graphql-ws": "^5.9.1",
"lodash": "^4.17.21",
"pluralize": "^8.0.0"
},
"devDependencies": {
"@esbuild-plugins/node-resolve": "^0.1.4",
"@refinedev/cli": "^2.16.33",
"@refinedev/core": "^4.52.0",
"@types/jest": "^29.2.4",
"@types/lodash": "^4.14.171",
"@types/pluralize": "^0.0.29",
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"nock": "^13.4.0",
"nock": "14.0.0-beta.14",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
"tsup": "^6.7.0",
"typescript": "^5.4.2"
},
"peerDependencies": {
"@refinedev/core": "^4.46.1",
"gql-query-builder": "^3.5.5",
"graphql-request": "^5.2.0",
"@refinedev/core": "^4.54.1",
"graphql-ws": "^5.9.1"
},
"engines": {
Expand Down
43 changes: 23 additions & 20 deletions packages/graphql/refine.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module.exports = {
label: "GraphQL",
requiredPackages: [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
Expand All @@ -24,32 +22,28 @@ module.exports = {
dest: "./providers/graphql/dataProvider/index.ts",
},
{
src: "./src/liveProvider/index.ts",
dest: "./providers/graphql/liveProvider/index.ts",
src: "./src/dataProvider/options.ts",
dest: "./providers/graphql/dataProvider/options.ts",
},
{
src: "./src/utils/index.ts",
dest: "./providers/graphql/utils/index.ts",
},
{
src: "./src/utils/generateFilter.ts",
dest: "./providers/graphql/utils/generateFilter.ts",
src: "./src/liveProvider/index.ts",
dest: "./providers/graphql/liveProvider/index.ts",
},
{
src: "./src/utils/generateSort.ts",
dest: "./providers/graphql/utils/generateSort.ts",
src: "./src/liveProvider/helpers.ts",
dest: "./providers/graphql/liveProvider/helpers.ts",
},
{
src: "./src/utils/generateUseListSubscription.ts",
dest: "./providers/graphql/utils/generateUseListSubscription.ts",
src: "./src/utils/index.ts",
dest: "./providers/graphql/utils/index.ts",
},
{
src: "./src/utils/generateUseManySubscription.ts",
dest: "./providers/graphql/utils/generateUseManySubscription.ts",
src: "./src/utils/graphql.ts",
dest: "./providers/graphql/utils/graphql.ts",
},
{
src: "./src/utils/generateUseOneSubscription.ts",
dest: "./providers/graphql/utils/generateUseOneSubscription.ts",
src: "./src/utils/getListHelpers.ts",
dest: "./providers/graphql/utils/getListHelpers.ts",
},
],
message: `
Expand All @@ -58,12 +52,21 @@ module.exports = {
\`\`\`
// title: App.tsx
import dataProvider, { liveProvider } from "providers/graphql";
import { Client, fetchExchange } from "@urql/core";
import { createClient } from "graphql-ws";

const API_URL = "https://api.nestjs-query.refine.dev/graphql";
const WS_URL = "wss://api.nestjs-query.refine.dev/graphql";

const client = new Client({ url: API_URL, exchanges: [fetchExchange] });

const wsClient = createClient({ url: WS_URL })

const App = () => {
return (
<Refine
dataProvider={dataProvider}
liveProvider={liveProvider}
dataProvider={dataProvider(client)}
liveProvider={liveProvider(wsClient)}
/* ... */
/>
);
Expand Down
Loading
Loading