This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finish replacing jotai-urql with bare urql
Mostly remaining was pagination related stuff & fixing tests
- Loading branch information
Showing
17 changed files
with
399 additions
and
454 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
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 |
---|---|---|
@@ -1,88 +0,0 @@ | ||
// Copyright 2023 The Matrix.org Foundation C.I.C. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { AnyVariables, CombinedError, OperationContext } from "@urql/core"; | ||
import { atom, WritableAtom } from "jotai"; | ||
import { useHydrateAtoms } from "jotai/utils"; | ||
import { AtomWithQuery, clientAtom } from "jotai-urql"; | ||
import type { ReactElement } from "react"; | ||
import { useQuery } from "urql"; | ||
|
||
import { graphql } from "./gql"; | ||
import { client } from "./graphql"; | ||
import { err, ok, Result } from "./result"; | ||
|
||
export type GqlResult<T> = Result<T, CombinedError>; | ||
export type GqlAtom<T> = WritableAtom< | ||
Promise<GqlResult<T>>, | ||
[context?: Partial<OperationContext>], | ||
void | ||
>; | ||
|
||
/** | ||
* Map the result of a query atom to a new value, making it a GqlResult | ||
* | ||
* @param queryAtom: An atom got from atomWithQuery | ||
* @param mapper: A function that takes the data from the query and returns a new value | ||
*/ | ||
export const mapQueryAtom = <Data, Variables extends AnyVariables, NewData>( | ||
queryAtom: AtomWithQuery<Data, Variables>, | ||
mapper: (data: Data) => NewData, | ||
): GqlAtom<NewData> => { | ||
return atom( | ||
async (get): Promise<GqlResult<NewData>> => { | ||
const result = await get(queryAtom); | ||
if (result.error) { | ||
return err(result.error); | ||
} | ||
|
||
if (result.data === undefined) { | ||
throw new Error("Query result is undefined"); | ||
} | ||
|
||
return ok(mapper(result.data)); | ||
}, | ||
|
||
(_get, set, context) => { | ||
set(queryAtom, context); | ||
}, | ||
); | ||
}; | ||
|
||
export const HydrateAtoms: React.FC<{ children: ReactElement }> = ({ | ||
children, | ||
}) => { | ||
useHydrateAtoms([[clientAtom, client]]); | ||
return children; | ||
}; | ||
|
||
const CURRENT_VIEWER_QUERY = graphql(/* GraphQL */ ` | ||
query CurrentViewerQuery { | ||
viewer { | ||
__typename | ||
... on User { | ||
id | ||
} | ||
} | ||
} | ||
`); | ||
|
||
export const useCurrentUserId = (): string | null => { | ||
const [result] = useQuery({ query: CURRENT_VIEWER_QUERY }); | ||
if (result.error) throw result.error; | ||
if (!result.data) throw new Error(); // Suspense mode is enabled | ||
return result.data.viewer.__typename === "User" | ||
? result.data.viewer.id | ||
: null; | ||
}; | ||
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
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.