-
-
Notifications
You must be signed in to change notification settings - Fork 7
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 #122 from dolthub/taylor/graphql-host
web: Support GraphQL api url environment variable
- Loading branch information
Showing
8 changed files
with
127 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import ErrorMsg from "@components/ErrorMsg"; | ||
import Loader from "@components/Loader"; | ||
import { createContextWithDisplayName } from "@dolthub/react-contexts"; | ||
import { ReactNode, useContext } from "react"; | ||
import useSWR from "swr"; | ||
import { ServerConfig } from "../pages/api/config"; | ||
|
||
export type ServerConfigContextValue = Partial<ServerConfig>; | ||
|
||
export const ServerConfigContext = | ||
createContextWithDisplayName<ServerConfigContextValue>( | ||
{}, | ||
"ServerConfigContext", | ||
); | ||
|
||
type Props = { | ||
children: ReactNode; | ||
}; | ||
|
||
// ServerConfigProvider needs to wrap every page, and is only used in _app | ||
export function ServerConfigProvider({ children }: Props): JSX.Element { | ||
const { data, error } = useSWR<ServerConfigContextValue>("/api/config"); | ||
|
||
if (error) { | ||
return ( | ||
<> | ||
<ErrorMsg err={error} /> | ||
{children} | ||
</> | ||
); | ||
} | ||
|
||
return data ? ( | ||
<ServerConfigContext.Provider value={{ ...data }}> | ||
{children} | ||
</ServerConfigContext.Provider> | ||
) : ( | ||
<Loader loaded={false} /> | ||
); | ||
} | ||
|
||
export function useServerConfig(): ServerConfigContextValue { | ||
return useContext(ServerConfigContext); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { NextApiHandler } from "next"; | ||
|
||
const cfg = { | ||
graphqlApiUrl: process.env.GRAPHQLAPI_URL, | ||
}; | ||
|
||
export type ServerConfig = typeof cfg; | ||
|
||
const handler: NextApiHandler = (_req, res) => { | ||
res.statusCode = 200; | ||
res.setHeader("Content-Type", "application/json"); | ||
res.end(JSON.stringify(cfg)); | ||
}; | ||
|
||
export default handler; |
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