feat: use gql tag function for all graphql queries

This commit is contained in:
DIYgod
2023-07-13 13:42:51 +01:00
parent 07420fd7e7
commit a2ba8d6a9a
4 changed files with 180 additions and 148 deletions
+62 -53
View File
@@ -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<NoteEntity> {
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<Response> {
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<Response> {
link += `/${encodeURIComponent(slug)}`
if (note.noteId + "" !== noteId) {
if (note.noteId !== noteId) {
link += `#comments`
}
}
+15
View File
@@ -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
? `
+89 -82
View File
@@ -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<TRender extends boolean = false>(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
+14 -13
View File
@@ -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,