Skip to content

Commit

Permalink
Added raw graphql query parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallykind committed Sep 26, 2024
1 parent 395e1c8 commit 4ac4374
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
};

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions test/tkn.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
});

0 comments on commit 4ac4374

Please sign in to comment.