Skip to content

Commit

Permalink
feat: new create query method
Browse files Browse the repository at this point in the history
A new createQuery method now allows you to create statically typed queries and is how we will internally write most of our subgraph queries for v3
  • Loading branch information
jackmellis committed Nov 22, 2023
1 parent b5c3566 commit 0704f6e
Show file tree
Hide file tree
Showing 16 changed files with 29,227 additions and 478 deletions.
19,579 changes: 19,579 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions packages/subgraph/codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
schema: https://api.thegraph.com/subgraphs/name/nftx-project/nftx-v3-uniswap-goerli
generates:
./src/types/nftx-v3-uniswap.d.ts:
config:
useTypeImports: true
strictScalars: true
scalars:
Bytes:
input: string
output: string
BigInt:
input: string|bigint
output: string|bigint
BigDecimal:
input: string
output: string
Int8: string|number
plugins:
- typescript
10 changes: 8 additions & 2 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"scripts": {
"lint": "eslint --fix -c ../../.eslintrc.js --ext ts,tsx src && tsc --noEmit",
"test": "jest --config ../../jest.config.js",
"build": "rm -rf dist && rollup -c && tsc -d --outDir dist/ts --emitDeclarationOnly",
"prepublishOnly": "yarn build"
"build": "rm -rf dist && yarn codegen && rollup -c && tsc -d --outDir dist/ts --emitDeclarationOnly",
"prepublishOnly": "yarn build",
"codegen": "graphql-codegen"
},
"bugs": {
"url": "https://github.com/NFTX-project/nftxjs/issues"
Expand All @@ -30,5 +31,10 @@
"dependencies": {
"@nftx/config": "^0.9.5",
"@nftx/constants": "^0.9.5"
},
"devDependencies": {
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.1",
"graphql": "^16.7.1"
}
}
32 changes: 27 additions & 5 deletions packages/subgraph/src/buildWhere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,51 @@ const buildArray = (key: string, arr: any[]): [string, string] => {
// return [key, JSON.stringify(arr.map((el) => buildPrimative(el)))];
return [key, `[${arr.map((el) => buildPrimative(el))}]`];
};
const isOperatorObject = (obj: any) => {
const operators = ['is', 'isNot', 'gt', 'gte', 'lt', 'lte', 'in'];

return Object.keys(obj).every((key) => operators.includes(key));
};
const buildOperatorObject = (key: string, obj: any): [string, string][] => {
return Object.entries(obj).map(([operator, value]): [string, string] => {
switch (operator) {
case 'is':
return [key, buildPrimative(value)];
case 'isNot':
return [`${key}_ne`, buildPrimative(value)];
default:
return [`${key}_${operator}`, buildPrimative(value)];
}
});
};
const buildKeyValuePair = (
key: string,
value: any
): [string, string] | null => {
): [string, string][] | null => {
// Strip out null
if (value == null) {
return null;
}
if (Array.isArray(value)) {
return buildArray(key, value);
return [buildArray(key, value)];
}
if (typeof value === 'object') {
return buildNestedFilter(key, value);
if (isOperatorObject(value)) {
return buildOperatorObject(key, value);
} else {
const pair = buildNestedFilter(key, value);
return pair && [pair];
}
}
return [key, buildPrimative(value)];
return [[key, buildPrimative(value)]];
};
const buildObject = (obj: any) => {
const keyValues = Object.entries(obj).reduce((acc, [key, value]) => {
const pair = buildKeyValuePair(key, value);
if (!pair) {
return acc;
}
return [...acc, pair];
return [...acc, ...pair];
}, [] as [string, string][]);

return ['{', keyValues.map((pair) => pair.join(': ')).join(', '), '}'].join(
Expand Down
Loading

0 comments on commit 0704f6e

Please sign in to comment.