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) }, }, },