From 4ac4374ef2bb5c3839693fb66f56989b29ffea42 Mon Sep 17 00:00:00 2001 From: "Kindnesss.eth" <134568982+technicallykind@users.noreply.github.com> Date: Thu, 26 Sep 2024 07:17:13 -0500 Subject: [PATCH] Added raw graphql query parameter --- index.js | 15 +++++++++++++++ package-lock.json | 4 ++-- test/tkn.test.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index a582532..63d087a 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ import { ethers } from 'ethers'; import { Record, Profile } from '@resolverworks/enson'; import { mock_list } from './mock_data'; +const graphUrl = `https://gateway.thegraph.com/api/1e800956ee244eeda0ad15fbad7a8c67/subgraphs/id/HxdDjjtznb8VFwqxAsHrfKUgjAUdziisERQdC1UcCr5U` + const rpcs = [ // 'https://eth-mainnet.g.alchemy.com/v2/9T5n0ljpi0uGhLhyGnQNQ0ZJ8aU9awlQ' 'https://eth-mainnet.rpc.grove.city/v1/298e23fd' @@ -67,11 +69,24 @@ function list(prefix) { throw new Error('Production list data not yet configured'); } +// Allows the user to perform raw custom graphQL queries +async function graphQuery(query) { + const response = await fetch(graphUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ query }), + }); + const data = await response.json(); + return data; +} const tkn = { lookup, list, setMockupMode, + graphQuery, // Add graphData to the exported object // other utilities }; diff --git a/package-lock.json b/package-lock.json index 3dd9f83..2c303f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "tkn", - "version": "0.0.1", + "version": "0.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "tkn", - "version": "0.0.1", + "version": "0.1.4", "license": "ISC", "dependencies": { "@resolverworks/enson": "^0.0.12", diff --git a/test/tkn.test.js b/test/tkn.test.js index 2580069..653bbe6 100644 --- a/test/tkn.test.js +++ b/test/tkn.test.js @@ -27,3 +27,37 @@ describe('tkn.list', () => { }); }); +describe('tkn.graphQuery', () => { + it('should fetch data from the GraphQL endpoint', async () => { + const query = ` + { + tokens(first: 5) { + id + name + symbol + decimals + } + } + `; + + const result = await tkn.graphQuery(query); + + expect(result).toBeDefined(); + expect(result.data).toBeDefined(); + expect(result.data.tokens).toBeDefined(); + + // Check if tokens is an array and has elements + expect(Array.isArray(result.data.tokens)).toBe(true); + expect(result.data.tokens.length).toBeGreaterThan(0); + + // Check the structure of the first token + const firstToken = result.data.tokens[0]; + expect(firstToken).toHaveProperty('id'); + expect(firstToken).toHaveProperty('name'); + expect(firstToken).toHaveProperty('symbol'); + expect(firstToken).toHaveProperty('decimals'); + + console.log(JSON.stringify(result, null, 2)); + }); +}); +