Skip to content

Commit dd129c3

Browse files
committed
allow to overwrite the API origin
1 parent deb8005 commit dd129c3

15 files changed

Lines changed: 66 additions & 37 deletions

.changeset/common-parks-fix.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
3+
---
4+
5+
6+

packages/hypergraph-react/src/HypergraphAppContext.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Repo } from '@automerge/automerge-repo/slim';
77
import { RepoContext } from '@automerge/automerge-repo-react-hooks';
88
import { Graph } from '@graphprotocol/grc-20';
99
import {
10+
Config,
1011
Connect,
1112
type ConnectCallbackResult,
1213
Identity,
@@ -232,6 +233,7 @@ export type HypergraphAppProviderProps = Readonly<{
232233
children: ReactNode;
233234
appId: string;
234235
logInvalidResults?: boolean;
236+
apiOrigin?: string;
235237
}>;
236238

237239
const mockStorage = {
@@ -249,6 +251,7 @@ export function HypergraphAppProvider({
249251
appId,
250252
children,
251253
logInvalidResults = true,
254+
apiOrigin,
252255
}: HypergraphAppProviderProps) {
253256
const [websocketConnection, setWebsocketConnection] = useState<WebSocket>();
254257
const [isConnecting, setIsConnecting] = useState(true);
@@ -259,6 +262,12 @@ export function HypergraphAppProvider({
259262
const identity = useSelectorStore(store, (state) => state.context.identity);
260263
const privyIdentity = useSelectorStore(store, (state) => state.context.privyIdentity);
261264

265+
useEffect(() => {
266+
if (apiOrigin) {
267+
Config.setApiOrigin(apiOrigin);
268+
}
269+
}, [apiOrigin]);
270+
262271
const logout = useCallback(() => {
263272
websocketConnection?.close();
264273
setWebsocketConnection(undefined);

packages/hypergraph-react/src/hooks/use-spaces.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Graph } from '@graphprotocol/grc-20';
2-
import { store } from '@graphprotocol/hypergraph';
1+
import { Config, store } from '@graphprotocol/hypergraph';
32
import { useQuery } from '@tanstack/react-query';
43
import { useSelector } from '@xstate/store/react';
54
import { gql, request } from 'graphql-request';
@@ -38,7 +37,7 @@ export const useSpaces = (params: { mode: 'public' | 'private' }) => {
3837
queryKey: ['hypergraph-public-spaces', params.mode],
3938
queryFn: async () => {
4039
const result = await request<PublicSpacesQueryResult>(
41-
`${Graph.TESTNET_API_ORIGIN}/v2/graphql`,
40+
`${Config.getApiOrigin()}/v2/graphql`,
4241
publicSpacesQueryDocument,
4342
{
4443
accountAddress,

packages/hypergraph-react/src/internal/generate-delete-ops.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Graph, type Op } from '@graphprotocol/grc-20';
1+
import type { Op } from '@graphprotocol/grc-20';
2+
import { Config } from '@graphprotocol/hypergraph';
23
import { gql, request } from 'graphql-request';
34

45
const deleteEntityQueryDocument = gql`
@@ -44,13 +45,9 @@ type DeleteEntityResult = {
4445
};
4546

4647
export const generateDeleteOps = async ({ id }: { id: string; space: string }) => {
47-
const result = await request<DeleteEntityResult>(
48-
`${Graph.TESTNET_API_ORIGIN}/v2/graphql`,
49-
deleteEntityQueryDocument,
50-
{
51-
entityId: id,
52-
},
53-
);
48+
const result = await request<DeleteEntityResult>(`${Config.getApiOrigin()}/v2/graphql`, deleteEntityQueryDocument, {
49+
entityId: id,
50+
});
5451
if (result.entity === null) {
5552
throw new Error('Entity not found');
5653
}

packages/hypergraph-react/src/internal/use-delete-entity-public.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Graph, type Op } from '@graphprotocol/grc-20';
22
import type { Connect } from '@graphprotocol/hypergraph';
3-
import { Constants } from '@graphprotocol/hypergraph';
3+
import { Config, Constants } from '@graphprotocol/hypergraph';
44
import { useQueryClient } from '@tanstack/react-query';
55
import * as Option from 'effect/Option';
66
import type * as Schema from 'effect/Schema';
@@ -45,7 +45,7 @@ export const useDeleteEntityPublic = <S extends Schema.Schema.AnyNoContext>(
4545
return async ({ id, walletClient }: { id: string; walletClient: Connect.SmartSessionClient }) => {
4646
try {
4747
const result = await request<EntityToDeleteQueryResult>(
48-
`${Graph.TESTNET_API_ORIGIN}/v2/graphql`,
48+
`${Config.getApiOrigin()}/v2/graphql`,
4949
deleteEntityQueryDocument,
5050
{
5151
spaceId: space,

packages/hypergraph-react/src/internal/use-public-space.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Graph } from '@graphprotocol/grc-20';
1+
import { Config } from '@graphprotocol/hypergraph';
22
import { useQuery } from '@tanstack/react-query';
33
import { gql, request } from 'graphql-request';
44

@@ -24,7 +24,7 @@ export const usePublicSpace = ({ spaceId, enabled }: { spaceId: string; enabled:
2424
const result = useQuery({
2525
queryKey: ['hypergraph-public-space', spaceId],
2626
queryFn: async () => {
27-
const result = await request<SpaceQueryResult>(`${Graph.TESTNET_API_ORIGIN}/v2/graphql`, spaceQueryDocument, {
27+
const result = await request<SpaceQueryResult>(`${Config.getApiOrigin()}/v2/graphql`, spaceQueryDocument, {
2828
spaceId,
2929
});
3030
return result?.space?.page

packages/hypergraph-react/src/prepare-publish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Graph, type Id, type Op, type PropertiesParam, type RelationsParam } from '@graphprotocol/grc-20';
2-
import { Constants, type Entity, Utils } from '@graphprotocol/hypergraph';
2+
import { Config, Constants, type Entity, Utils } from '@graphprotocol/hypergraph';
33
import * as Option from 'effect/Option';
44
import type * as Schema from 'effect/Schema';
55
import * as SchemaAST from 'effect/SchemaAST';
@@ -49,7 +49,7 @@ export const preparePublish = async <S extends Schema.Schema.AnyNoContext>({
4949
publicSpace,
5050
}: PreparePublishParams<S>) => {
5151
const data = await request<EntityToPublishQueryResult>(
52-
`${Graph.TESTNET_API_ORIGIN}/v2/graphql`,
52+
`${Config.getApiOrigin()}/v2/graphql`,
5353
entityToPublishQueryDocument,
5454
{
5555
entityId: entity.id,

packages/hypergraph-react/src/publish-ops.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Op } from '@graphprotocol/grc-20';
2-
import { Graph, Ipfs } from '@graphprotocol/grc-20';
3-
import { Connect } from '@graphprotocol/hypergraph';
2+
import { Ipfs } from '@graphprotocol/grc-20';
3+
import { Config, Connect } from '@graphprotocol/hypergraph';
44
import type { Hash } from 'viem';
55

66
type PublishParams = {
@@ -34,7 +34,7 @@ export const publishOps = async ({ name, ops, walletClient, space }: PublishPara
3434
const cid = publishResult.cid;
3535

3636
// This returns the correct contract address and calldata depending on the space id
37-
const result = await fetch(`${Graph.TESTNET_API_ORIGIN}/space/${space}/edit/calldata`, {
37+
const result = await fetch(`${Config.getApiOrigin()}/space/${space}/edit/calldata`, {
3838
method: 'POST',
3939
body: JSON.stringify({ cid }),
4040
});

packages/hypergraph-react/test/prepare-publish.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Repo } from '@automerge/automerge-repo';
22
import { Graph, Id } from '@graphprotocol/grc-20';
3-
import { Entity, store, Type } from '@graphprotocol/hypergraph';
3+
import { Config, Entity, store, Type } from '@graphprotocol/hypergraph';
44
import '@testing-library/jest-dom/vitest';
55
import request from 'graphql-request';
66
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -133,7 +133,7 @@ describe('preparePublish', () => {
133133

134134
const result = await preparePublish(params);
135135

136-
expect(mockRequest).toHaveBeenCalledWith(`${Graph.TESTNET_API_ORIGIN}/v2/graphql`, expect.any(String), {
136+
expect(mockRequest).toHaveBeenCalledWith(`${Config.getApiOrigin()}/v2/graphql`, expect.any(String), {
137137
entityId: entity.id,
138138
spaceId: publicSpaceId,
139139
});

packages/hypergraph/src/config.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Graph } from '@graphprotocol/grc-20';
2+
3+
let apiOrigin: string | null = null;
4+
5+
/**
6+
* Sets the API origin globally for all hypergraph API calls.
7+
* @param origin - The API origin URL (e.g., "https://api.mainnet.graphprotocol.io")
8+
*/
9+
export const setApiOrigin = (origin: string) => {
10+
apiOrigin = origin;
11+
};
12+
13+
/**
14+
* Gets the configured API origin, or defaults to Graph.TESTNET_API_ORIGIN if not set.
15+
* @returns The API origin URL
16+
*/
17+
export const getApiOrigin = (): string => {
18+
if (apiOrigin) {
19+
return apiOrigin;
20+
}
21+
// Default to testnet
22+
return Graph.TESTNET_API_ORIGIN;
23+
};

0 commit comments

Comments
 (0)