fix: handle graphql invalid response (#2582)

This commit is contained in:
HugoCasa
2023-11-07 12:52:56 +01:00
committed by GitHub
parent e84e38d3bd
commit 12e731b5c0
2 changed files with 38 additions and 7 deletions
@@ -205,9 +205,16 @@ GROUP BY table_name".replace('{dataset.id}', dataset.id)
publicOnly: !!schema.public || !!schema.PUBLIC
}
} else {
$dbSchemas[resourcePath] = {
lang: 'graphql',
schema: testResult.result
if (typeof testResult.result !== 'object' || !('__schema' in testResult.result)) {
console.error('Invalid GraphQL schema')
if (drawer?.isOpen()) {
sendUserToast('Invalid GraphQL schema', true)
}
} else {
$dbSchemas[resourcePath] = {
lang: 'graphql',
schema: testResult.result
}
}
}
}
@@ -1,5 +1,5 @@
<script lang="ts">
import { JobService, Preview } from '$lib/gen'
import { CompletedJob, JobService, Preview } from '$lib/gen'
import { Database, Loader2 } from 'lucide-svelte'
import Button from './common/button/Button.svelte'
@@ -15,6 +15,7 @@
code: string
lang: string
argName: string
additionalCheck?: (testResult: CompletedJob) => CompletedJob
}
} = {
postgresql: {
@@ -40,7 +41,25 @@
graphql: {
code: '{ __typename }',
lang: 'graphql',
argName: 'api'
argName: 'api',
additionalCheck: (testResult: CompletedJob) => {
if (
testResult.success &&
(typeof testResult.result !== 'object' || !('__typename' in testResult.result))
) {
return {
...testResult,
result: {
error: {
message: 'Invalid GraphQL API response'
}
},
success: false
}
} else {
return testResult
}
}
}
}
@@ -64,13 +83,18 @@
tryEvery({
tryCode: async () => {
const testResult = await JobService.getCompletedJob({
let testResult = await JobService.getCompletedJob({
workspace: $workspaceStore!,
id: job
})
if (resourceScript.additionalCheck) {
testResult = resourceScript.additionalCheck(testResult)
}
loading = false
sendUserToast(
testResult.success ? 'Connection successful' : testResult.result?.['error']?.['message'],
testResult.success
? 'Connection successful'
: 'Connection error: ' + testResult.result?.['error']?.['message'],
!testResult.success
)
},