From 4424e44416da3811940be39327266a7735a926b5 Mon Sep 17 00:00:00 2001 From: CounterFire2023 <136581895+CounterFire2023@users.noreply.github.com> Date: Thu, 29 Jun 2023 18:36:35 +0800 Subject: [PATCH] change sth --- src/models/NftHolder.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/schema/index.ts | 10 +++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/models/NftHolder.ts b/src/models/NftHolder.ts index f488cb1..77fc4bc 100644 --- a/src/models/NftHolder.ts +++ b/src/models/NftHolder.ts @@ -22,6 +22,46 @@ export class NftHolderClass extends BaseModule { public version: number @prop() public lastTxHash: string + + public toQlModel() { + return { + id: this.tokenId, + address: this.user, + chain: this.chain + '', + contract: this.address, + } + } + + public static async queryNFT({ + address, + chain, + contract, + id, + }: { + address?: string + chain?: string + contract?: string + id?: string + }) { + chain = chain || process.env.CHAIN_DEFAULT + const chainNum = parseInt(chain) + const query: any = { chain: chainNum } + if (address) { + query.user = address.toLowerCase() + } + if (contract) { + query.address = contract.toLowerCase() + } + if (id) { + query.tokenId = id + } + let records = await NftHolder.find(query) + let results: any = [] + for (let record of records) { + results.push(record.toQlModel()) + } + return results + } } export const NftHolder = getModelForClass(NftHolderClass, { diff --git a/src/schema/index.ts b/src/schema/index.ts index c8232b9..d424d4a 100644 --- a/src/schema/index.ts +++ b/src/schema/index.ts @@ -1,3 +1,4 @@ +import { NftHolder } from 'models/NftHolder' import { RequestTask } from 'models/RequestTask' const graphql = require('graphql') @@ -11,6 +12,7 @@ const nftType = new GraphQLObjectType({ address: { type: GraphQLString }, contract: { type: GraphQLString }, id: { type: GraphQLString }, + chain: { type: GraphQLString }, }), }) @@ -27,8 +29,14 @@ const RootQuery = new GraphQLObjectType({ }, nfts: { type: new GraphQLList(nftType), + args: { + address: { type: GraphQLString }, + contract: { type: GraphQLString }, + id: { type: GraphQLString }, + chain: { type: GraphQLString }, + }, async resolve(parent, args) { - return await RequestTask.find(args) + return await NftHolder.queryNFT(args) }, }, },