Amowe Olubusayo
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # A guide on the Graph Client Library and integration with Next.js **What is the Graph client?** The Graph Client is a tool/library from the Graph aimed at making network-related data consumption simpler for dApps thus offering everything a dApp requires. Here are the some of the features of the Graph Client 1. Multiple indexers 2. Client-Side Composition 3. Cross-chain Subgraph Handling 4. Automatic Block Tracking 5. Client-Side Composition 6. Automatic Pagination **Installing the Graph Client CLI** You add the Graph Client CLI as a dependency to your dApp. The Graph Client CLI comes with a built-in GraphiQL so you can experiment with queries in real time. You can that using either Yarn or Npm, run either of the commands below **WITH YARN** ```yarn add -D @graphprotocol/client-cli ``` **WITH NPM** ```npm install --save-dev @graphprotocol/client-cli``` In your root folder, create a ```yaml``` config file and name it ```.graphclientrc.yml``` Point it to your deployed/published GraphQL endpoints provided by The Graph. It should look like this **.graphclientrc.yml** ```javascript sources: - name: uniswapv2 handler: graphql: endpoint: https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v2 ``` Now, go ahead to run the command ```graphclient build``` or specify as a command in your script command in your package.json incase you want to run with ```npm``` or ```yarn```. If you wish to also use the built-in devtool GraphiQL, you can add the command ```graphclient serve-dev``` **package.json** ```json "scripts": { "build": "graphclient build", "dev": "graphclient serve-dev" } ``` The result of this command will generate the .graphclient artifact as typescript files as shown below: ```cmake GraphClient: Cleaning existing artifacts GraphClient: Reading the configuration 🕸️: Generating the unified schema 🕸️: Generating artifacts 🕸️: Generating index file in TypeScript 🕸️: Writing index.ts for ESM to the disk. 🕸️: Cleanup 🕸️: Done! => .graphclient ``` To use the artifact, you can import it directly in your dApp like this ```javascript import { execute } from '../.graphclient' //DEFINE YOUR QUERY async function main() { const result = await execute(myQuery, {}) console.log(result) } ``` You will notice as stated earlier above, the artifacts files are generated in typescript. You can decide to use JavaScript instead. The CLI allows for js customization so you can generate JavaScript and JSON files alongside additional TypeScript definition files by using ```--fileType js``` or ```--fileType json```. # A practical approach to using the Graph Client with Next.js We shall be taking a look on how to query two subgraphs ([Enzyme](https://thegraph.com/hosted-service/subgraph/enzymefinance/enzyme) and [Balancer](https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer)) from a simple frontend. You can start by creating a [Next.js + TS](https://nextjs.org/docs/basic-features/typescript) application from scratch or simply clone the project at [https://app.radicle.network/seeds/pine.radicle.garden/rad:git:hnrkp9gcfqg3fqt5u1am1fhh97ggf619aihgy](https://app.radicle.network/seeds/pine.radicle.garden/rad:git:hnrkp9gcfqg3fqt5u1am1fhh97ggf619aihgy) **Cloning the Starter Project from Radicle** If you will like to clone the project, you need to have the **Rad CLI** installed then proceed to run the following command: `rad clone rad://pine.radicle.garden/hnrkp9gcfqg3fqt5u1am1fhh97ggf619aihgy` Here is a sample of what the clone view is like ![](https://i.imgur.com/pFPVKt3.png) Proceed to **`cd`** into the project folder. **Installing all required dependencies and devDependencies** With either `YARN` or `NPM`, make sure the following dependencies are installed Dependencies "graphql" "next": "react", "react-dom" DevDependecies "@graphprotocol/client-cli", "@types/node", "@types/react", "eslint", "eslint-config-next", "typescript" Your package.json file should look similar to this ```json { "name": "nextjs-example", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint", "build-client": "graphclient build", "graphiql": "graphclient serve-dev" }, "dependencies": { "graphql": "^16.6.0", "next": "12.2.5", "react": "18.2.0", "react-dom": "18.2.0" }, "devDependencies": { "@graphprotocol/client-cli": "2.2.3", "@types/node": "17.0.26", "@types/react": "18.0.17", "eslint": "8.22.0", "eslint-config-next": "12.2.5", "typescript": "4.7.4" } } ``` If you cloned or forked this [repo](https://github.com/amoweolubusayo/graph-client-enzyme-balancer-nextjs-example), just run a `yarn` or an `npm i` Incase you encounter errors using **`yarn`** to install **`graphql`**, use **`npm`** instead to install **Creating and defining the `.graphclientrc.yml` file.** As mentioned earlier, in the root folder, create a `.graphclientrc.yml` file. We will be defining the two sources i.e. subgraphs here this way ```javascript sources: - name: enzyme handler: graphql: endpoint: https://api.thegraph.com/subgraphs/name/enzymefinance/enzyme - name: balancer handler: graphql: endpoint: https://api.thegraph.com/subgraphs/name/balancer-labs/balancer ``` To get the correct endpoint, navigate to the subgraph's hosted service e.g for Balancer, https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer and select the url under QUERIES(HTTP) which in this case is https://api.thegraph.com/subgraphs/name/balancer-labs/balancer. The same process applies to getting Enzyme's endpoint. ![](https://i.imgur.com/GAQJNIB.png) We will still be coming back to this `.graphclientrc.yml` file but for now let's go create our documents file. **Creating and defining the document file** Create another file that you can name with the .graphql extension. In the repo suggested for forking above, I named as `sample-query.graphql` Here is where you define your query. Now head back to your subgraph e.g for Enzyme: https://thegraph.com/hosted-service/subgraph/enzymefinance/enzyme, study the schema and pick whatever data you might need. For me, I need some information on the holdingStates, so I do the following in the `sample-query.graphql` ```javascript query SampleQuery { # Enzyme data holdingStates( where: { fund: "0x24f3b37934d1ab26b7bda7f86781c90949ae3a79" } orderBy: timestamp orderDirection: asc first: 1000 ) { timestamp asset { symbol } amount } } ``` Repeating the same process for Balancer: https://thegraph.com/hosted-service/subgraph/balancer-labs/balancer, I need information on pools, so I add this query as well. Now the `sample-query.graphql` now looks as what we have below: ```javascript query SampleQuery { # Enzyme data holdingStates( where: { fund: "0x24f3b37934d1ab26b7bda7f86781c90949ae3a79" } orderBy: timestamp orderDirection: asc first: 1000 ) { timestamp asset { symbol } amount } # Balancer Data pools(first: 5, where: { publicSwap: true }) { id finalized publicSwap swapFee totalWeight tokensList tokens { id address balance decimals symbol denormWeight } } } ``` At this point, we can then go back to `.graphclientrc.yml`. After sources, we define documents which references the `sample-query.graphql` file. ```javascript documents: - ./sample-query.graphql ``` **Querying from the frontend** Under pages, create an index.tsx file and add the following ```javascript import type { NextPage } from "next"; import Head from "next/head"; import Image from "next/image"; import styles from "../styles/Home.module.css"; import { SampleQuery, getBuiltGraphSDK } from "../.graphclient"; const Home: NextPage<{ data: SampleQuery }> = ({ data }) => { return ( <div className={styles.container}> <Head> <title>Graph Client Next.js Example</title> <meta name="description" content="Generated by create next app" /> <link rel="icon" href="/favicon.ico" /> </Head> <main className={styles.main}> <h6 className={styles.title}> Welcome to Graph Client <a href="https://nextjs.org">Next.js</a>{" "} (Enzyme Data and Balancer Data) </h6> <form> <br /> <br /> <textarea className={styles.dataTextArea} value={JSON.stringify(data, null, 2)} readOnly rows={55} /> </form> </main> <footer className={styles.footer}> <a href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" target="_blank" rel="noopener noreferrer" > Powered by{" "} <span className={styles.logo}> <Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} /> </span> </a> </footer> </div> ); }; const sdk = getBuiltGraphSDK(); export async function getServerSideProps() { const data = await sdk.SampleQuery(); return { props: { data, }, }; } export default Home; ``` Notice that we do ```javascript const sdk = getBuiltGraphSDK(); export async function getServerSideProps() { const data = await sdk.SampleQuery(); return { props: { data, }, }; } ``` ***This is the Graph Client Typed SDK for consuming the Graph API in Next.js.*** **Run your project** Start up your projects by running the following commands **`WITH YARN`** ```bash $ yarn build $ yarn build-client ``` **`WITH NPM`** ```bash $ npm run build $ npm run build-client ``` After running the `build-client` command, the output will be as follows: ![](https://i.imgur.com/bJlBD9G.png) Proceed to then run **`WITH YARN`** ```bash $ yarn dev ``` **`WITH NPM`** ```bash $ npm run dev ``` ![](https://i.imgur.com/w4EScQm.png) Here is how your output should finally look like ![](https://i.imgur.com/n5aDUg2.png) **Push your project to Radicle** If you are new to radicle and started your project using the Next.js + TS, run the following command to create a profile and identity `rad auth` ![](https://i.imgur.com/UPspEmv.png) Initialize your project by running `rad init` Provide the name, description and the default branch of your project ![](https://i.imgur.com/rZwCHha.png) Publish your project and select a seed node by running `rad push ` ![](https://i.imgur.com/6b8vxns.png) This project is available here at **Radicle** https://app.radicle.network/seeds/pine.radicle.garden/rad:git:hnrkp9gcfqg3fqt5u1am1fhh97ggf619aihgy **Github** https://github.com/amoweolubusayo/graph-client-enzyme-balancer-nextjs-example

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully