-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from swan-io/query-optimization
Experimentation: query optimization
- Loading branch information
Showing
15 changed files
with
627 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const path = require("path"); | ||
|
||
module.exports = { | ||
parser: "@typescript-eslint/parser", | ||
plugins: ["react", "react-hooks"], | ||
|
||
extends: [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
], | ||
|
||
parserOptions: { | ||
sourceType: "module", | ||
project: path.resolve(__dirname + "/tsconfig.json"), | ||
}, | ||
|
||
env: { | ||
browser: true, | ||
es2022: true, | ||
}, | ||
|
||
overrides: [ | ||
{ | ||
files: ["**/__{mocks,tests}__/**/*.{ts,tsx}"], | ||
rules: { | ||
"no-empty": ["error", { allowEmptyCatch: true }], | ||
}, | ||
}, | ||
{ | ||
files: ["*.d.ts"], | ||
rules: { | ||
"@typescript-eslint/consistent-type-definitions": "off", | ||
"@typescript-eslint/no-unused-vars": "off", | ||
}, | ||
}, | ||
{ | ||
files: ["clients/**/src/graphql/**/*.{ts,tsx}"], | ||
rules: { | ||
"@typescript-eslint/ban-types": "off", | ||
"@typescript-eslint/no-explicit-any": "off", | ||
}, | ||
}, | ||
], | ||
|
||
rules: { | ||
"no-implicit-coercion": "error", | ||
"no-param-reassign": "error", | ||
"no-var": "error", | ||
"object-shorthand": "warn", | ||
"prefer-const": "error", | ||
|
||
"no-extra-boolean-cast": "off", | ||
|
||
"react/jsx-boolean-value": ["error", "always"], | ||
|
||
"react-hooks/rules-of-hooks": "error", | ||
"react-hooks/exhaustive-deps": "warn", | ||
}, | ||
|
||
ignorePatterns: [".eslintrc.js"], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { AsyncData, Result } from "@swan-io/boxed"; | ||
import { useState } from "react"; | ||
import { P, match } from "ts-pattern"; | ||
import { ClientError, useQuery } from "../../src"; | ||
import { graphql } from "../graphql"; | ||
import { UserCard } from "./UserCard"; | ||
|
||
export const accountMembershipDetailQuery = graphql(` | ||
query AccountMembershipDetail($accountMembershipId: ID!) { | ||
accountMembership(id: $accountMembershipId) { | ||
id | ||
user { | ||
id | ||
firstName | ||
lastName | ||
} | ||
} | ||
} | ||
`); | ||
|
||
type Props = { | ||
accountMembershipId: string; | ||
onPressClose: () => void; | ||
}; | ||
|
||
export const AccountMembershipDetail = ({ | ||
accountMembershipId, | ||
onPressClose, | ||
}: Props) => { | ||
const [data] = useQuery( | ||
accountMembershipDetailQuery, | ||
{ accountMembershipId }, | ||
{ optimize: true }, | ||
); | ||
|
||
const [showDetails, setShowDetails] = useState(false); | ||
|
||
return match(data) | ||
.with(AsyncData.P.NotAsked, () => "Nothing") | ||
.with(AsyncData.P.Loading, () => "Loading") | ||
.with(AsyncData.P.Done(Result.P.Error(P.select())), (error) => { | ||
ClientError.forEach(error, (error) => console.error(error)); | ||
return "Error"; | ||
}) | ||
.with( | ||
AsyncData.P.Done(Result.P.Ok(P.select())), | ||
({ accountMembership }) => { | ||
if (accountMembership == null) { | ||
return <div>No membership</div>; | ||
} | ||
return ( | ||
<dialog open={true}> | ||
<button onClick={onPressClose}>Close</button> | ||
<div> | ||
<strong>Membership: {accountMembership.id}</strong> | ||
|
||
{showDetails ? ( | ||
<UserCard accountMembershipId={accountMembership.id} /> | ||
) : ( | ||
<button onClick={() => setShowDetails(true)}> | ||
Show user info | ||
</button> | ||
)} | ||
</div> | ||
</dialog> | ||
); | ||
}, | ||
) | ||
.exhaustive(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { AsyncData, Result } from "@swan-io/boxed"; | ||
import { P, match } from "ts-pattern"; | ||
import { ClientError, useQuery } from "../../src"; | ||
import { graphql } from "../graphql"; | ||
|
||
export const accountMembershipUserDetailQuery = graphql(` | ||
query AccountMembershipUserDetail($accountMembershipId: ID!) { | ||
accountMembership(id: $accountMembershipId) { | ||
id | ||
user { | ||
id | ||
firstName | ||
lastName | ||
birthDate | ||
mobilePhoneNumber | ||
} | ||
} | ||
} | ||
`); | ||
|
||
type Props = { | ||
accountMembershipId: string; | ||
}; | ||
|
||
const formatter = new Intl.DateTimeFormat(); | ||
|
||
export const UserCard = ({ accountMembershipId }: Props) => { | ||
const [data] = useQuery( | ||
accountMembershipUserDetailQuery, | ||
{ | ||
accountMembershipId, | ||
}, | ||
{ optimize: true }, | ||
); | ||
|
||
return match(data) | ||
.with(AsyncData.P.NotAsked, () => "Nothing") | ||
.with(AsyncData.P.Loading, () => "Loading") | ||
.with(AsyncData.P.Done(Result.P.Error(P.select())), (error) => { | ||
ClientError.forEach(error, (error) => console.error(error)); | ||
return "Error"; | ||
}) | ||
.with( | ||
AsyncData.P.Done(Result.P.Ok(P.select())), | ||
({ accountMembership }) => { | ||
if (accountMembership == null) { | ||
return null; | ||
} | ||
const user = accountMembership.user; | ||
if (user == null) { | ||
return <div>No user</div>; | ||
} | ||
return ( | ||
<div className="User"> | ||
<ul> | ||
<li> | ||
<strong>ID:</strong> {user.id} | ||
</li> | ||
<li> | ||
<strong>First name:</strong> {user.firstName} | ||
</li> | ||
<li> | ||
<strong>Last name:</strong> {user.lastName} | ||
</li> | ||
{user.birthDate != null ? ( | ||
<li> | ||
<strong>Birthdate:</strong>{" "} | ||
{formatter.format(new Date(user.birthDate))} | ||
</li> | ||
) : null} | ||
<li> | ||
<strong>Mobile phone number:</strong> {user.mobilePhoneNumber} | ||
</li> | ||
</ul> | ||
</div> | ||
); | ||
}, | ||
) | ||
.exhaustive(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.