Skip to content

Commit 2dbec88

Browse files
jamesjames
james
authored and
james
committed
Playground adventures
1 parent 67297dd commit 2dbec88

File tree

6 files changed

+280
-3
lines changed

6 files changed

+280
-3
lines changed

package-lock.json

+137
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
"start": "next start"
99
},
1010
"dependencies": {
11+
"@apollo/client": "^3.3.11",
12+
"@tailwindcss/forms": "^0.2.1",
1113
"autoprefixer": "^10.2.4",
14+
"graphql": "^15.5.0",
1215
"gray-matter": "^4.0.2",
1316
"next": "10.0.6",
1417
"postcss": "^8.2.6",

pages/playground/graphql/index.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { useState } from 'react'
2+
import { ApolloClient, ApolloProvider, InMemoryCache, useQuery, gql } from '@apollo/client';
3+
4+
const client = new ApolloClient({
5+
uri: 'https://graphql-api.mainnet.dandelion.link/',
6+
cache: new InMemoryCache()
7+
});
8+
9+
const GET_METADATA_BY_KEY = gql`
10+
query MetaTX($metadatakey: String!) {
11+
transactions(
12+
where: { metadata: { key: {_eq: $metadatakey} } }
13+
) {
14+
metadata {
15+
key
16+
value
17+
}
18+
}
19+
}
20+
`
21+
22+
function MetadataTX({ metadatakey}) {
23+
24+
const { loading, error, data } = useQuery(GET_METADATA_BY_KEY, {
25+
variables: { metadatakey }
26+
})
27+
28+
if (loading) return <p>Loading...</p>
29+
if (error) return <p>ERROR</p>
30+
// What's cool is that if we know that there's certain data at a certain key, our page can respond accordingly
31+
// Note that by looking at transactions[1], we can count on the blockchain to deliver us what we expect
32+
// There's plenty more we could do for security here!
33+
if (metadatakey === "100") return (
34+
<div>
35+
{data.transactions[1].metadata[0].value.additionalInformation[0].winners.map((i) => <p>{i.information.projectName}</p>)}
36+
</div>
37+
)
38+
if (metadatakey === "13") return (
39+
<div>
40+
<p>{data.transactions[0].metadata[0].value.about}</p>
41+
{data.transactions[0].metadata[0].value.cookies.map((i) => <p>{i.["Lucky Number"]} - {i.Fortune}</p>)}
42+
</div>
43+
)
44+
if (data.transactions.length === 0) return <p>There is no data at that key.</p>
45+
// For every other key:
46+
return (
47+
<div>
48+
{data.transactions.map((i) => <p>{JSON.stringify(i.metadata)}</p>)}
49+
</div>
50+
)
51+
}
52+
53+
export default function rewardsOutput(){
54+
55+
const [txMeta, setTxMeta] = useState(null)
56+
57+
return (
58+
<ApolloProvider client={client}>
59+
<div className="w-2/3 mx-auto mt-10">
60+
<h1>Graph QL Playground</h1>
61+
62+
<form>
63+
<input id="keyNumber" name="keyNumber" type="text" onChange={e => setTxMeta(e.target.value)} className="m-5" />
64+
</form>
65+
<MetadataTX metadatakey={txMeta} />
66+
</div>
67+
</ApolloProvider>
68+
)
69+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { ApolloClient, ApolloProvider, InMemoryCache, useQuery, gql } from '@apollo/client';
2+
3+
const client = new ApolloClient({
4+
uri: 'https://graphql-api.mainnet.dandelion.link/',
5+
cache: new InMemoryCache()
6+
});
7+
8+
function RewardAggregated() {
9+
10+
const { loading, error, data } = useQuery(gql`
11+
{
12+
rewards_aggregate {
13+
aggregate {
14+
avg {
15+
amount
16+
}
17+
count
18+
max {
19+
amount
20+
}
21+
min {
22+
amount
23+
}
24+
sum {
25+
amount
26+
}
27+
}
28+
}
29+
}
30+
`
31+
)
32+
33+
if (loading) return <p>Loading...</p>
34+
if (error) return <p>ERROR</p>
35+
return (
36+
<div>
37+
<p>{data.rewards_aggregate.aggregate.avg.amount}</p>
38+
<p>{data.rewards_aggregate.aggregate.count}</p>
39+
<p>{data.rewards_aggregate.aggregate.max.amount/1000000}</p>
40+
<p>{data.rewards_aggregate.aggregate.min.amount}</p>
41+
<p>{data.rewards_aggregate.aggregate.sum.amount/1000000}</p>
42+
</div>
43+
)
44+
}
45+
46+
export default function rewardsOutput(){
47+
48+
return (
49+
<ApolloProvider client={client}>
50+
<div className="w-2/3 mx-auto mt-10">
51+
<h1>Graph QL Playground</h1>
52+
<RewardAggregated />
53+
</div>
54+
</ApolloProvider>
55+
)
56+
}

pages/playground/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ function playground() {
88
Playground
99
</CardHeading>
1010
<ul>
11-
<li className="text-6xl m-12 hover:text-purple-700">
11+
<li className="text-2xl m-12 hover:text-purple-700">
1212
<Link href="playground/metadata">
13-
<a className="">Metadata</a>
13+
<a className="">Metadata Results via PostgREST</a>
14+
</Link>
15+
</li>
16+
<li className="text-2xl m-12 hover:text-purple-700">
17+
<Link href="playground/graphql/rewardsoutput">
18+
<a className="">GraphQL: Rewards Output</a>
19+
</Link>
20+
</li>
21+
<li className="text-2xl m-12 hover:text-purple-700">
22+
<Link href="playground/graphql/">
23+
<a className="">Metadata Results via GraphQL</a>
1424
</Link>
1525
</li>
1626
</ul>

0 commit comments

Comments
 (0)