feat: add improved logging for graphql requests
This commit is contained in:
@@ -53,13 +53,28 @@ export async function request<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const print = (await import("graphql/language/printer")).print
|
try {
|
||||||
const nr = Math.random()
|
// @ts-expect-error: query can be undefined (?)
|
||||||
console.log(`START REQUEST ${nr}`)
|
const operationName = (query as DocumentNode).definitions.find(
|
||||||
console.time(`OUTGOING REQUEST ${nr}`)
|
(d) => d.kind === "OperationDefinition" && d.operation === "query"
|
||||||
console.log(`Sending reqeust to ${env.CMS_URL}`)
|
// @ts-expect-error: name does not exist (?)
|
||||||
console.log(`Query:`, print(query as DocumentNode))
|
).name.value
|
||||||
console.log(`Variables:`, variables)
|
|
||||||
|
console.log(`[gql] Sending graphql request to ${env.CMS_URL}`, {
|
||||||
|
operationName,
|
||||||
|
variables,
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[gql] Unable to extract operation name from query`, {
|
||||||
|
query,
|
||||||
|
error: e,
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(`[gql] Sending graphql request to ${env.CMS_URL}`, {
|
||||||
|
operationName: "<Unable to extract>",
|
||||||
|
variables,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const response = await client.request<T>({
|
const response = await client.request<T>({
|
||||||
document: query,
|
document: query,
|
||||||
@@ -70,8 +85,30 @@ export async function request<T>(
|
|||||||
variables,
|
variables,
|
||||||
})
|
})
|
||||||
|
|
||||||
console.timeEnd(`OUTGOING REQUEST ${nr}`)
|
try {
|
||||||
console.log({ response })
|
// @ts-expect-error: query can be undefined (?)
|
||||||
|
const operationName = (query as DocumentNode).definitions.find(
|
||||||
|
(d) => d.kind === "OperationDefinition" && d.operation === "query"
|
||||||
|
// @ts-expect-error: name does not exist (?)
|
||||||
|
).name.value
|
||||||
|
|
||||||
|
console.log(`[gql] Response for ${env.CMS_URL}`, {
|
||||||
|
response,
|
||||||
|
operationName,
|
||||||
|
variables,
|
||||||
|
})
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`[gql] Unable to extract operation name from query`, {
|
||||||
|
query,
|
||||||
|
error: e,
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(`[gql] Response for ${env.CMS_URL}`, {
|
||||||
|
response,
|
||||||
|
operationName: "<Unable to extract>",
|
||||||
|
variables,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return { data: response }
|
return { data: response }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -98,8 +135,10 @@ export async function request<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// console.error("Error in graphql request")
|
console.error(
|
||||||
// console.error(error)
|
`[gql] Error sending graphql request to ${env.CMS_URL}`,
|
||||||
|
error
|
||||||
|
)
|
||||||
throw new Error("Something went wrong")
|
throw new Error("Something went wrong")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user