feat: improve structure and error handling

This commit is contained in:
Michael Zetterberg
2024-05-14 15:55:46 +02:00
parent 01587d7fd5
commit f5108d1a8e
104 changed files with 1505 additions and 1570 deletions
+36 -41
View File
@@ -7,63 +7,58 @@
#import "../Fragments/Refs/LoyaltyPage.graphql"
#import "../Fragments/Refs/System.graphql"
query GetAccountPage($locale: String!, $url: String!) {
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
items {
url
title
content {
__typename
...AccountPageContentDynamicContent
...AccountPageContentShortcuts
...AccountPageContentTextContent
}
query GetAccountPage($locale: String!, $uid: String!) {
account_page(locale: $locale, uid: $uid) {
url
title
content {
__typename
...AccountPageContentDynamicContent
...AccountPageContentShortcuts
...AccountPageContentTextContent
}
total
}
}
query GetAccountPageRefs($locale: String!, $url: String!) {
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
items {
content {
... on AccountPageContentDynamicContent {
__typename
dynamic_content {
link {
linkConnection {
edges {
node {
__typename
...AccountPageRef
...LoyaltyPageRef
}
query GetAccountPageRefs($locale: String!, $uid: String!) {
account_page(locale: $locale, uid: $uid) {
content {
... on AccountPageContentDynamicContent {
__typename
dynamic_content {
link {
linkConnection {
edges {
node {
__typename
...AccountPageRef
...LoyaltyPageRef
}
}
}
}
}
... on AccountPageContentShortcuts {
__typename
}
... on AccountPageContentShortcuts {
__typename
shortcuts {
shortcuts {
shortcuts {
linkConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
linkConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
}
system {
...System
}
}
system {
...System
}
}
}
-11
View File
@@ -1,11 +0,0 @@
query GetContentTypeUid($locale: String!, $url: String!) {
all_content_page(where: { url: $url }, locale: $locale) {
total
}
all_current_blocks_page(where: { url: $url }, locale: $locale) {
total
}
all_loyalty_page(where: { url: $url }, locale: $locale) {
total
}
}
-16
View File
@@ -1,16 +0,0 @@
#import "../Fragments/Image.graphql"
query GetMyPagesLogo($locale: String!) {
all_header(limit: 1, locale: $locale) {
items {
logoConnection {
edges {
node {
...Image
}
}
totalCount
}
}
}
}
+206 -210
View File
@@ -8,222 +8,218 @@
#import "../Fragments/Refs/LoyaltyPage.graphql"
#import "../Fragments/Refs/System.graphql"
query GetLoyaltyPage($locale: String!, $url: String!) {
all_loyalty_page(where: { url: $url }, locale: $locale) {
items {
blocks {
__typename
... on LoyaltyPageBlocksShortcuts {
shortcuts {
title
preamble
shortcuts {
text
open_in_new_tab
linkConnection {
edges {
node {
__typename
...AccountPageLink
...LoyaltyPageLink
...ContentPageLink
}
}
totalCount
}
}
}
}
... on LoyaltyPageBlocksDynamicContent {
dynamic_content {
title
subtitle
component
link {
text
pageConnection {
edges {
node {
...ContentPageLink
...LoyaltyPageLink
}
}
totalCount
}
}
}
}
... on LoyaltyPageBlocksCardGrid {
card_grid {
title
subtitle
cards {
referenceConnection {
edges {
node {
__typename
...LoyaltyPageLink
...ContentPageLink
...AccountPageLink
}
}
totalCount
}
title
subtitle
open_in_new_tab
cta_text
}
}
}
... on LoyaltyPageBlocksContent {
content {
content {
json
embedded_itemsConnection {
edges {
node {
__typename
...Image
...LoyaltyPageLink
...ContentPageLink
}
}
totalCount
}
}
}
}
}
title
heading
sidebar {
__typename
... on LoyaltyPageSidebarJoinLoyaltyContact {
join_loyalty_contact {
title
preamble
contact {
... on LoyaltyPageSidebarJoinLoyaltyContactBlockContactContact {
__typename
contact {
display_text
contact_field
}
}
}
}
}
... on LoyaltyPageSidebarContent {
content {
content {
json
embedded_itemsConnection {
edges {
node {
__typename
...Image
...LoyaltyPageLink
...ContentPageLink
}
}
totalCount
}
}
}
}
}
web {
breadcrumbs {
query GetLoyaltyPage($locale: String!, $uid: String!) {
loyalty_page(uid: $uid, locale: $locale) {
blocks {
__typename
... on LoyaltyPageBlocksShortcuts {
shortcuts {
title
parents {
href
title
preamble
shortcuts {
text
open_in_new_tab
linkConnection {
edges {
node {
__typename
...AccountPageLink
...LoyaltyPageLink
...ContentPageLink
}
}
totalCount
}
}
}
}
system {
uid
created_at
updated_at
... on LoyaltyPageBlocksDynamicContent {
dynamic_content {
title
subtitle
component
link {
text
pageConnection {
edges {
node {
...ContentPageLink
...LoyaltyPageLink
}
}
totalCount
}
}
}
}
... on LoyaltyPageBlocksCardGrid {
card_grid {
title
subtitle
cards {
referenceConnection {
edges {
node {
__typename
...LoyaltyPageLink
...ContentPageLink
...AccountPageLink
}
}
totalCount
}
title
subtitle
open_in_new_tab
cta_text
}
}
}
... on LoyaltyPageBlocksContent {
content {
content {
json
embedded_itemsConnection {
edges {
node {
__typename
...Image
...LoyaltyPageLink
...ContentPageLink
}
}
totalCount
}
}
}
}
}
title
heading
sidebar {
__typename
... on LoyaltyPageSidebarJoinLoyaltyContact {
join_loyalty_contact {
title
preamble
contact {
... on LoyaltyPageSidebarJoinLoyaltyContactBlockContactContact {
__typename
contact {
display_text
contact_field
}
}
}
}
}
... on LoyaltyPageSidebarContent {
content {
content {
json
embedded_itemsConnection {
edges {
node {
__typename
...Image
...LoyaltyPageLink
...ContentPageLink
}
}
totalCount
}
}
}
}
}
web {
breadcrumbs {
title
parents {
href
title
}
}
}
system {
uid
created_at
updated_at
}
}
}
query GetLoyaltyPageRefs($locale: String!, $url: String!) {
all_loyalty_page(where: { url: $url }, locale: $locale) {
items {
blocks {
... on LoyaltyPageBlocksShortcuts {
__typename
query GetLoyaltyPageRefs($locale: String!, $uid: String!) {
loyalty_page(locale: $locale, uid: $uid) {
blocks {
... on LoyaltyPageBlocksShortcuts {
__typename
shortcuts {
shortcuts {
shortcuts {
linkConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
linkConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
... on LoyaltyPageBlocksDynamicContent {
__typename
dynamic_content {
link {
pageConnection {
edges {
node {
__typename
...ContentPageRef
...LoyaltyPageRef
}
}
... on LoyaltyPageBlocksDynamicContent {
__typename
dynamic_content {
link {
pageConnection {
edges {
node {
__typename
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
... on LoyaltyPageBlocksCardGrid {
__typename
card_grid {
cards {
referenceConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
... on LoyaltyPageBlocksCardGrid {
__typename
card_grid {
cards {
referenceConnection {
edges {
node {
__typename
...AccountPageRef
...ContentPageRef
...LoyaltyPageRef
}
}
}
}
}
... on LoyaltyPageBlocksContent {
__typename
}
... on LoyaltyPageBlocksContent {
__typename
content {
content {
content {
embedded_itemsConnection {
edges {
node {
# No fragments used since we want to include __typename for each type to avoid fetching SystemAsset
... on ContentPage {
__typename
system {
...System
}
embedded_itemsConnection {
edges {
node {
# No fragments used since we want to include __typename for each type to avoid fetching SystemAsset
... on ContentPage {
__typename
system {
...System
}
... on LoyaltyPage {
__typename
system {
...System
}
}
... on LoyaltyPage {
__typename
system {
...System
}
}
}
@@ -232,26 +228,26 @@ query GetLoyaltyPageRefs($locale: String!, $url: String!) {
}
}
}
sidebar {
... on LoyaltyPageSidebarContent {
__typename
}
sidebar {
... on LoyaltyPageSidebarContent {
__typename
content {
content {
content {
embedded_itemsConnection {
edges {
node {
# No fragments used since we want to include __typename for each type to avoid fetching SystemAsset
... on ContentPage {
__typename
system {
...System
}
embedded_itemsConnection {
edges {
node {
# No fragments used since we want to include __typename for each type to avoid fetching SystemAsset
... on ContentPage {
__typename
system {
...System
}
... on LoyaltyPage {
__typename
system {
...System
}
}
... on LoyaltyPage {
__typename
system {
...System
}
}
}
@@ -260,9 +256,9 @@ query GetLoyaltyPageRefs($locale: String!, $url: String!) {
}
}
}
system {
...System
}
}
system {
...System
}
}
}
+28
View File
@@ -0,0 +1,28 @@
#import "../Fragments/Refs/System.graphql"
query ResolveEntryByUrl($locale: String!, $url: String!) {
all_content_page(where: { url: $url }, locale: $locale) {
items {
system {
...System
}
}
total
}
all_current_blocks_page(where: { url: $url }, locale: $locale) {
items {
system {
...System
}
}
total
}
all_loyalty_page(where: { url: $url }, locale: $locale) {
items {
system {
...System
}
}
total
}
}
+39 -45
View File
@@ -24,54 +24,48 @@ export async function request<T>(
variables?: {},
options?: Pick<RequestInit, "cache" | "next">
): Promise<Data<T>> {
try {
if (options?.cache) {
client.requestConfig.cache = options.cache
}
if (options?.next) {
client.requestConfig.next = options.next
}
if (options?.cache) {
client.requestConfig.cache = options.cache
}
if (options?.next) {
client.requestConfig.next = options.next
}
if (env.PRINT_QUERY) {
const print = (await import("graphql/language/printer")).print
const rawResponse = await client.rawRequest<T>(
print(query as DocumentNode),
variables,
{
access_token: env.CMS_ACCESS_TOKEN,
"Content-Type": "application/json",
}
)
/**
* TODO: Send to Monitoring (Logging and Metrics)
*/
console.log({
complexityLimit: rawResponse.headers.get("x-query-complexity"),
})
console.log({
referenceDepth: rawResponse.headers.get("x-reference-depth"),
})
console.log({ resolverCost: rawResponse.headers.get("x-resolver-cost") })
return {
data: rawResponse.data,
}
}
const response = await client.request<T>({
document: query,
requestHeaders: {
if (env.PRINT_QUERY) {
const { print } = await import("graphql")
const rawResponse = await client.rawRequest<T>(
print(query as DocumentNode),
variables,
{
access_token: env.CMS_ACCESS_TOKEN,
"Content-Type": "application/json",
},
variables,
})
}
)
return { data: response }
} catch (error) {
console.info(`GraphQL Request Error`)
console.error(error)
throw new Error("Something went wrong")
/**
* TODO: Send to Monitoring (Logging and Metrics)
*/
console.log({
complexityLimit: rawResponse.headers.get("x-query-complexity"),
})
console.log({
referenceDepth: rawResponse.headers.get("x-reference-depth"),
})
console.log({ resolverCost: rawResponse.headers.get("x-resolver-cost") })
return {
data: rawResponse.data,
}
}
const response = await client.request<T>({
document: query,
requestHeaders: {
access_token: env.CMS_ACCESS_TOKEN,
"Content-Type": "application/json",
},
variables,
})
return { data: response }
}
+1 -1
View File
@@ -2,4 +2,4 @@ import { createTRPCReact } from "@trpc/react-query"
import type { AppRouter } from "@/server"
export const trpc = createTRPCReact<AppRouter>({})
export const trpc = createTRPCReact<AppRouter>()
+25 -1
View File
@@ -1,9 +1,33 @@
import { TRPCError } from "@trpc/server"
import { redirect } from "next/navigation"
import { Lang } from "@/constants/languages"
import { appRouter } from "@/server"
import { createContext } from "@/server/context"
import { internalServerError } from "@/server/errors/next"
import { createCallerFactory } from "@/server/trpc"
const createCaller = createCallerFactory(appRouter)
export function serverClient() {
return createCaller(createContext())
return createCaller(createContext(), {
onError: ({ ctx, error, input, path, type }) => {
console.error(`Server Client error for ${type}: ${path}`)
if (input) {
console.error(`Received input:`)
console.error(input)
}
console.error(error)
if (error instanceof TRPCError) {
if (error.code === "UNAUTHORIZED") {
const lang = ctx?.lang || Lang.en
const pathname = ctx?.pathname || "/"
redirect(`/${lang}/login?redirectTo=${encodeURIComponent(pathname)}`)
}
}
throw internalServerError()
},
})
}