Merged in feat/gql-logging (pull request #668)
feat: add improved logging for graphql requests Approved-by: Arvid Norlin
This commit is contained in:
@@ -53,13 +53,28 @@ export async function request<T>(
|
||||
}
|
||||
}
|
||||
|
||||
const print = (await import("graphql/language/printer")).print
|
||||
const nr = Math.random()
|
||||
console.log(`START REQUEST ${nr}`)
|
||||
console.time(`OUTGOING REQUEST ${nr}`)
|
||||
console.log(`Sending reqeust to ${env.CMS_URL}`)
|
||||
console.log(`Query:`, print(query as DocumentNode))
|
||||
console.log(`Variables:`, variables)
|
||||
try {
|
||||
// @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] 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>({
|
||||
document: query,
|
||||
@@ -70,8 +85,30 @@ export async function request<T>(
|
||||
variables,
|
||||
})
|
||||
|
||||
console.timeEnd(`OUTGOING REQUEST ${nr}`)
|
||||
console.log({ response })
|
||||
try {
|
||||
// @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 }
|
||||
} catch (error) {
|
||||
@@ -98,8 +135,10 @@ export async function request<T>(
|
||||
}
|
||||
}
|
||||
|
||||
// console.error("Error in graphql request")
|
||||
// console.error(error)
|
||||
console.error(
|
||||
`[gql] Error sending graphql request to ${env.CMS_URL}`,
|
||||
error
|
||||
)
|
||||
throw new Error("Something went wrong")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user