-
Notifications
You must be signed in to change notification settings - Fork 16
fix: use graphql directly #102
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
Open
wa0x6e
wants to merge
6
commits into
master
Choose a base branch
from
call-hub-directly
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fae4a17
fix: use graphql directly
wa0x6e 69bc769
Merge branch 'master' into call-hub-directly
wa0x6e cc03083
Merge branch 'master' into call-hub-directly
wa0x6e 900cf90
chore: remove unused dependencies
wa0x6e d5e76d5
chore: remove unused dependencies
wa0x6e b4c6b1b
fix: fix wrong variable name
wa0x6e File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,190 @@ | ||
import { gql, ApolloClient, InMemoryCache, HttpLink } from '@apollo/client/core'; | ||
import { capture } from '@snapshot-labs/snapshot-sentry'; | ||
import { fetchWithKeepAlive } from './utils'; | ||
|
||
const HUB_URL = `${process.env.HUB_URL || 'https://hub.snapshot.org'}/graphql`; | ||
|
||
const client = new ApolloClient({ | ||
link: new HttpLink({ uri: HUB_URL, fetch: fetchWithKeepAlive as any }), | ||
cache: new InMemoryCache({ | ||
addTypename: false | ||
}), | ||
defaultOptions: { | ||
query: { | ||
fetchPolicy: 'no-cache' | ||
} | ||
} | ||
}); | ||
|
||
type Message = { | ||
mci: number; | ||
id: string; | ||
ipfs: string; | ||
type: string; | ||
timestamp: number; | ||
space: string; | ||
}; | ||
|
||
type Space = { | ||
id: string; | ||
name: string; | ||
avatar?: string; | ||
}; | ||
|
||
type Proposal = { | ||
space: Space; | ||
id: string; | ||
type: string; | ||
author: string; | ||
title: string; | ||
body: string; | ||
choices: string[]; | ||
created: number; | ||
start: number; | ||
end: number; | ||
link: string; | ||
snapshot: string; | ||
}; | ||
|
||
type Subscriber = { | ||
address: string; | ||
}; | ||
|
||
const MESSAGES_QUERY = gql` | ||
query Messages( | ||
$type_in: [String] | ||
$first: Int | ||
$mci: Int | ||
$orderDirection: OrderDirection | ||
$orderBy: String | ||
) { | ||
messages( | ||
where: { mci_gt: $mci, type_in: $type_in } | ||
first: $first | ||
orderBy: $orderBy | ||
orderDirection: $orderDirection | ||
) { | ||
mci | ||
id | ||
ipfs | ||
type | ||
timestamp | ||
space | ||
} | ||
} | ||
`; | ||
|
||
const SPACE_QUERY = gql` | ||
query Space($id: String) { | ||
space(id: $id) { | ||
id | ||
name | ||
} | ||
} | ||
`; | ||
|
||
const PROPOSAL_QUERY = gql` | ||
query Proposal($id: String) { | ||
proposal(id: $id) { | ||
space { | ||
id | ||
name | ||
avatar | ||
} | ||
id | ||
type | ||
author | ||
title | ||
body | ||
choices | ||
created | ||
start | ||
end | ||
link | ||
snapshot | ||
} | ||
} | ||
`; | ||
|
||
const SUBSCRIPTIONS_QUERY = gql` | ||
query Subscriptions($space: String) { | ||
subscriptions(where: { space: $space }) { | ||
address | ||
} | ||
} | ||
`; | ||
|
||
export async function getNextMessages(mci: number) { | ||
try { | ||
const { | ||
data: { messages } | ||
}: { data: { messages: Message[] } } = await client.query({ | ||
query: MESSAGES_QUERY, | ||
variables: { | ||
mci, | ||
type_in: ['proposal', 'delete-proposal'], | ||
first: 10, | ||
orderBy: 'mci', | ||
orderDirection: 'asc' | ||
} | ||
}); | ||
|
||
return messages; | ||
} catch (e: any) { | ||
capture(e); | ||
return []; | ||
} | ||
} | ||
|
||
export async function getSpace(id: string) { | ||
try { | ||
const { | ||
data: { space } | ||
}: { data: { space: Space | null } } = await client.query({ | ||
query: SPACE_QUERY, | ||
variables: { | ||
id | ||
} | ||
}); | ||
|
||
return space; | ||
} catch (e: any) { | ||
capture(e); | ||
return null; | ||
} | ||
} | ||
|
||
export async function getProposal(id: string) { | ||
try { | ||
const { | ||
data: { proposal } | ||
}: { data: { proposal: Proposal | null } } = await client.query({ | ||
query: PROPOSAL_QUERY, | ||
variables: { | ||
id | ||
} | ||
}); | ||
|
||
return proposal; | ||
} catch (e: any) { | ||
capture(e); | ||
return null; | ||
} | ||
} | ||
|
||
export async function getSubscribers(space: string) { | ||
try { | ||
const { | ||
data: { subscribers } | ||
}: { data: { subscribers: Subscriber[] | null } } = await client.query({ | ||
query: SUBSCRIPTIONS_QUERY, | ||
variables: { | ||
space | ||
} | ||
}); | ||
return (subscribers || []).map(subscriber => subscriber.address); | ||
} catch (e: any) { | ||
capture(e); | ||
return []; | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.