From a2ba8d6a9a77c645ec681e64a3c2df7ad2c7a477 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 13 Jul 2023 13:42:51 +0100 Subject: [PATCH] feat: use gql tag function for all graphql queries --- src/app/api/redirection/route.ts | 115 +++++++++++---------- src/models/home.model.ts | 15 +++ src/models/page.model.ts | 171 ++++++++++++++++--------------- src/models/site.model.ts | 27 ++--- 4 files changed, 180 insertions(+), 148 deletions(-) diff --git a/src/app/api/redirection/route.ts b/src/app/api/redirection/route.ts index 4b596d27..e9619882 100644 --- a/src/app/api/redirection/route.ts +++ b/src/app/api/redirection/route.ts @@ -1,6 +1,8 @@ import type { NoteEntity } from "crossbell" import { redirect } from "next/navigation" +import { gql } from "@urql/core" + import { IS_DEV, IS_VERCEL_PREVIEW } from "~/lib/constants" import { getNoteSlug, getSiteLink } from "~/lib/helpers" import { NextServerResponse, getQuery } from "~/lib/server-helper" @@ -8,53 +10,58 @@ import { checkDomainServer } from "~/models/site.model" import { client } from "~/queries/graphql" async function getOriginalNote( - characterId: string, - noteId: string, + characterId: number, + noteId: number, ): Promise { const result = await client .query( - `query getNote { - note( - where: { - note_characterId_noteId_unique: { - characterId: ${characterId}, - noteId: ${noteId}, - }, - }, - ) { - characterId - noteId - deleted - toNote { - characterId - noteId - deleted - toNote { - characterId - noteId - deleted - toNote { + gql` + query getNote($characterId: Int!, $noteId: Int!) { + note( + where: { + note_characterId_noteId_unique: { + characterId: $characterId + noteId: $noteId + } + } + ) { characterId noteId deleted + toNote { + characterId + noteId + deleted + toNote { + characterId + noteId + deleted + toNote { + characterId + noteId + deleted + metadata { + content + } + } + metadata { + content + } + } + metadata { + content + } + } metadata { content } } - metadata { - content - } } - metadata { - content - } - } - metadata { - content - } - } - }`, - {}, + `, + { + characterId, + noteId, + }, ) .toPromise() const note = result.data?.note @@ -71,36 +78,38 @@ async function getOriginalNote( } export async function GET(req: Request): Promise { - let { characterId, noteId } = getQuery(req) + const query = getQuery(req) + let characterId = parseInt(query.characterId) + const noteId = parseInt(query.noteId) const res = new NextServerResponse() - if (!characterId || typeof characterId !== "string") { + if (!characterId) { return res.status(400).json({ error: "Missing characterId" }) } let character let note - if (noteId && typeof noteId === "string") { + if (noteId) { note = await getOriginalNote(characterId, noteId) - characterId = note.characterId + "" + characterId = note.characterId } const result = await client .query( - `query getCharacter { - character( - where: { - characterId: ${characterId}, + gql` + query getCharacter($characterId: Int!) { + character(where: { characterId: $characterId }) { + handle + metadata { + content + } + } + } + `, + { + characterId, }, - ) { - handle - metadata { - content - } - } - }`, - {}, ) .toPromise() character = result.data?.character @@ -121,7 +130,7 @@ export async function GET(req: Request): Promise { link += `/${encodeURIComponent(slug)}` - if (note.noteId + "" !== noteId) { + if (note.noteId !== noteId) { link += `#comments` } } diff --git a/src/models/home.model.ts b/src/models/home.model.ts index 6cb9c388..ea5fc7df 100644 --- a/src/models/home.model.ts +++ b/src/models/home.model.ts @@ -88,6 +88,9 @@ export async function getFeed({ characterId: { notIn: $filter }, + deleted: { + equals: false, + }, metadata: { content: { path: "sources", @@ -147,6 +150,9 @@ export async function getFeed({ characterId: { notIn: $filter }, + deleted: { + equals: false, + }, metadata: { AND: [{ content: { @@ -296,6 +302,9 @@ export async function getFeed({ characterId: { notIn: $filter }, + deleted: { + equals: false, + }, metadata: { AND: [{ content: { @@ -368,6 +377,9 @@ export async function getFeed({ characterId: { notIn: $filter }, + deleted: { + equals: false, + }, metadata: { AND: [{ content: { @@ -437,6 +449,9 @@ export async function getFeed({ characterId: { notIn: $filter }, + deleted: { + equals: false, + }, ${ time ? ` diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 5d742954..6016401f 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -11,6 +11,7 @@ import type { Address } from "viem" import { GeneralAccount } from "@crossbell/connect-kit" import { indexer } from "@crossbell/indexer" import { extractCharacterAttribute } from "@crossbell/util-metadata" +import { gql } from "@urql/core" import { expandCrossbellNote } from "~/lib/expand-unit" import { filterCommentCharacter } from "~/lib/filter-character" @@ -320,115 +321,121 @@ export async function getPage(input: { if (!input.noteId) { const result = await client .query( - ` - query getNotes { - notes( - where: { - characterId: { - equals: ${input.characterId}, - }, - deleted: { - equals: false, - }, - metadata: { - AND: [ - { + gql` + query getNotes($characterId: Int!, $slug: JSON!) { + notes( + where: { + characterId: { + equals: $characterId, + }, + deleted: { + equals: false, + }, + metadata: { content: { path: ["sources"], array_contains: ["xlog"] }, - }, - { OR: [ { content: { path: ["attributes"], array_contains: [{ trait_type: "xlog_slug", - value: "${decodeURIComponent(input.slug!)}", + value: $slug, }] } }, { content: { path: ["title"], - equals: "${decodeURIComponent(input.slug!)}" + equals: $slug }, } ] - } - ] - }, - }, - orderBy: [{ createdAt: desc }], - take: 1, - ) { - characterId - noteId - uri - metadata { - uri - content + }, + }, + orderBy: [{ createdAt: desc }], + take: 1, + ) { + characterId + noteId + uri + metadata { + uri + content + } + owner + createdAt + updatedAt + publishedAt + transactionHash + blockNumber + updatedTransactionHash + updatedBlockNumber + ${ + input.useStat + ? ` + stat { + viewDetailCount + } + ` + : "" + } + } } - owner - createdAt - updatedAt - publishedAt - transactionHash - blockNumber - updatedTransactionHash - updatedBlockNumber - ${ - input.useStat - ? `stat { - viewDetailCount - }` - : "" - } - } - }`, - {}, + `, + { + characterId: input.characterId, + slug: decodeURIComponent(input.slug!), + }, ) .toPromise() page = result.data.notes[0] } else { const result = await client .query( - ` - query getNote { - note( - where: { - note_characterId_noteId_unique: { - characterId: ${input.characterId}, - noteId: ${input.noteId}, - }, - }, - ) { - characterId - noteId - uri - metadata { - uri - content + gql` + query getNote($characterId: Int!, $noteId: Int!) { + note( + where: { + note_characterId_noteId_unique: { + characterId: $characterId, + noteId: $noteId, + }, + }, + ) { + characterId + noteId + uri + metadata { + uri + content + } + owner + createdAt + updatedAt + publishedAt + transactionHash + blockNumber + updatedTransactionHash + updatedBlockNumber + ${ + input.useStat + ? ` + stat { + viewDetailCount + } + ` + : "" + } + } } - owner - createdAt - updatedAt - publishedAt - transactionHash - blockNumber - updatedTransactionHash - updatedBlockNumber - ${ - input.useStat - ? `stat { - viewDetailCount - }` - : "" - } - } - }`, - {}, + `, + { + characterId: input.characterId, + noteId: input.noteId, + }, ) .toPromise() page = result.data.note diff --git a/src/models/site.model.ts b/src/models/site.model.ts index 138345c7..9b44fe77 100644 --- a/src/models/site.model.ts +++ b/src/models/site.model.ts @@ -3,6 +3,7 @@ import type { Address } from "viem" import type { useContract } from "@crossbell/contract" import { indexer } from "@crossbell/indexer" +import { gql } from "@urql/core" import { expandCrossbellCharacter } from "~/lib/expand-unit" import { client } from "~/queries/graphql" @@ -32,19 +33,19 @@ export const getSubscriptionsFromList = async ( ) => { const response = await client .query( - ` - query getFollows($list: [Int!], $fromCharacterId: Int!) { - links( - where: { - linkType: { equals: "follow" }, - fromCharacterId: { equals: $fromCharacterId }, - toCharacterId: { in: $list } - }, - ) { - toCharacterId - } - } - `, + gql` + query getFollows($list: [Int!], $fromCharacterId: Int!) { + links( + where: { + linkType: { equals: "follow" } + fromCharacterId: { equals: $fromCharacterId } + toCharacterId: { in: $list } + } + ) { + toCharacterId + } + } + `, { list, fromCharacterId,