Skip to content

Commit

Permalink
Merge pull request #2 from Airstack-xyz/fix/unable-to-add-cursor-to-s…
Browse files Browse the repository at this point in the history
…ocials-queries

Fixed: unable to add cursor to socials queries
  • Loading branch information
aadilhasan authored Oct 9, 2023
2 parents 6568888 + cc783db commit 9661b83
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 26 deletions.
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airstack/node",
"version": "0.0.4",
"version": "0.0.5",
"description": "",
"sideEffects": false,
"main": "dist/cjs/index.js",
Expand Down
118 changes: 96 additions & 22 deletions src/__tests__/fetchQueryWithPagination.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

import { init } from "../init";
import { init } from '../init';
import { fetchQueryWithPagination } from '../apis/fetchQueryWithPagination';
import { FetchQuery } from "../types";
import { FetchQuery } from '../types';

const demoAPIKey = "ef3d1cdeafb642d3a8d6a44664ce566c";
const demoAPIKey = 'ef3d1cdeafb642d3a8d6a44664ce566c';
const demoQuery = `query tokens($address: Identity!, $limit: Int!) {
erc20: TokenBalances(
input: {filter: {owner: {_in: [$address]}, tokenType: {_in: [ERC20]}}, limit: $limit, blockchain: ethereum}
Expand All @@ -25,9 +24,9 @@ const demoQuery = `query tokens($address: Identity!, $limit: Int!) {
}`;

const demoVariables = {
"address": "vitalik.eth",
"limit": 1
}
address: 'vitalik.eth',
limit: 1,
};

const multiQuery = `query tokens($address: Identity!, $address2: Identity!) {
erc20: TokenBalances(
Expand Down Expand Up @@ -74,30 +73,65 @@ const multiQuery = `query tokens($address: Identity!, $address2: Identity!) {
}
}`;

describe('fetchQueryWithPagination', () => {
it('should fail if no api key is porivded', async () => {
const socialFollowersQuery = `query FollowersDetails($identity: Identity!) {
SocialFollowers(input: {filter: {identity: {_eq: $identity}}, blockchain: ALL, limit: 2}) {
Follower {
id
blockchain
followerProfileId
followerTokenId
followingProfileId
followerAddress {
identity
addresses
socials {
blockchain
dappName
profileName
profileTokenId
profileTokenAddress
}
primaryDomain {
name
}
domains {
dappName
name
}
xmtp {
isXMTPEnabled
}
}
}
}
}`;

const { data, error } = await fetchQueryWithPagination(demoQuery, demoVariables);
describe('fetchQueryWithPagination', () => {
it('should fail if no api key is provided', async () => {
const { data, error } = await fetchQueryWithPagination(
demoQuery,
demoVariables
);
expect(error).toBeTruthy();
expect(data).toBeNull();
});

it('should return a promise with the data and error properties', async () => {

init(demoAPIKey);

const { data, error, hasNextPage, hasPrevPage } = await fetchQueryWithPagination(demoQuery, demoVariables);
const { data, error, hasNextPage, hasPrevPage } =
await fetchQueryWithPagination(demoQuery, demoVariables);
expect(error).toBeNull();
expect(data.erc20.data).toHaveLength(1);
expect(hasNextPage).toBe(true);
expect(hasPrevPage).toBe(false);
});

it('should fetch next page on getNextPage', async () => {

init(demoAPIKey);

const { data, error, hasNextPage, hasPrevPage, getNextPage } = await fetchQueryWithPagination(demoQuery, demoVariables);
const { data, error, hasNextPage, hasPrevPage, getNextPage } =
await fetchQueryWithPagination(demoQuery, demoVariables);
expect(error).toBeNull();
expect(data.erc20.data).toHaveLength(1);
expect(hasNextPage).toBe(true);
Expand All @@ -114,13 +148,14 @@ describe('fetchQueryWithPagination', () => {
it('should return hasNextPage as false if no next page, and getNextPage should return null', async () => {
// this address has only 1 erc20 token, if this test fails, it means that the address has more than 1 erc20 token
const variables = {
"address": "betashop.eth",
"limit": 2
address: 'betashop.eth',
limit: 2,
};

init(demoAPIKey);

const { data, error, hasNextPage, hasPrevPage, getNextPage } = await fetchQueryWithPagination(demoQuery, variables);
const { data, error, hasNextPage, hasPrevPage, getNextPage } =
await fetchQueryWithPagination(demoQuery, variables);
expect(error).toBeNull();
expect(data.erc20.data).toHaveLength(1);
expect(hasPrevPage).toBe(false);
Expand All @@ -129,16 +164,17 @@ describe('fetchQueryWithPagination', () => {
expect(nextResponse).toBeNull();
});

it('should do pagination backward and farward', async () => {
it('should do pagination backward and forward', async () => {
// this address has only 1 erc20 token, if this test fails, it means that the address has more than 1 erc20 token
const variables = {
"address": "betashop.eth",
"address2": "vitalik.eth"
address: 'betashop.eth',
address2: 'vitalik.eth',
};

init(demoAPIKey);

const { data, error, hasNextPage, hasPrevPage, getNextPage } = await fetchQueryWithPagination(multiQuery, variables);
const { data, error, hasNextPage, hasPrevPage, getNextPage } =
await fetchQueryWithPagination(multiQuery, variables);
// page => 0
expect(error).toBeNull();
expect(data.erc20.data).toHaveLength(1);
Expand All @@ -147,7 +183,7 @@ describe('fetchQueryWithPagination', () => {
expect(hasNextPage).toBe(true);

let response = {
getNextPage
getNextPage,
} as FetchQuery | null;

// page => 0 => 1 => 2 => 3
Expand All @@ -169,4 +205,42 @@ describe('fetchQueryWithPagination', () => {
response = await response!.getPrevPage();
expect(response?.data.erc20).toBeTruthy();
}, 15000);

it('should do pagination backward and forward for queries with a mismatched schema', async () => {
// this address has only 1 erc20 token, if this test fails, it means that the address has more than 1 erc20 token
const variables = {
identity: 'betashop.eth',
};

init(demoAPIKey);

const { data, error, hasNextPage, hasPrevPage, getNextPage } =
await fetchQueryWithPagination(socialFollowersQuery, variables);
// page => 0
expect(error).toBeNull();
expect(data.SocialFollowers.Follower).toHaveLength(2);
expect(hasPrevPage).toBe(false);
expect(hasNextPage).toBe(true);

let response = {
getNextPage,
} as FetchQuery | null;

// page => 0 => 1 => 2 => 3
for (let i = 0; i < 3; i++) {
response = await response!.getNextPage();
if (response) {
expect(response?.data.SocialFollowers.Follower).toHaveLength(2);
}
}

// page 3 => 2 => 1
for (let i = 0; i < 2; i++) {
response = await response!.getPrevPage();
expect(response?.data.SocialFollowers.Follower).toHaveLength(2);
}
// page => 0
response = await response!.getPrevPage();
expect(response?.data.SocialFollowers.Follower).toBeTruthy();
}, 15000);
});
21 changes: 20 additions & 1 deletion src/utils/query/getIntrospectionQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { AIRSTACK_ENDPOINT } from '../../constants';
import { introspectionQuery } from '../../constants/introspectionQuery';
import fetch from '../fetch';

const mismatchedQueryMap = {
socialfollowingsinput: 'socialfollowinginput',
socialfollowersinput: 'socialfollowerinput',
} as const;

export type SchemaMap = Record<string, IntrospectionType>;
const cache: {
schema: SchemaMap | null;
Expand All @@ -26,7 +31,7 @@ export async function getIntrospectionQueryMap(): Promise<SchemaMap> {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': config.authKey
Authorization: config.authKey,
},
body: JSON.stringify({
query: introspectionQuery,
Expand All @@ -41,6 +46,20 @@ export async function getIntrospectionQueryMap(): Promise<SchemaMap> {
schemaMap[type.name.toLowerCase()] = type;
});

// fix for known query-name to schema-input-name mismatch
for (const expectedQueryInputName in mismatchedQueryMap) {
const actualQueryInputNameInResponse =
mismatchedQueryMap[
expectedQueryInputName as keyof typeof mismatchedQueryMap
];
const value =
schemaMap[expectedQueryInputName] ||
schemaMap[actualQueryInputNameInResponse];
if (value) {
schemaMap[expectedQueryInputName] = value;
}
}

cache.schema = schemaMap;
return schemaMap;
});
Expand Down

0 comments on commit 9661b83

Please sign in to comment.