1- import { ApolloClient , HttpLink , InMemoryCache } from '@apollo/client/core/index.js ' ;
1+ import { ApolloClient , HttpLink , InMemoryCache } from '@apollo/client/core' ;
22import type {
33 CreateEolReportInput ,
44 EolReport ,
55 EolReportMutationResponse ,
66 EolReportQueryResponse ,
77 GetEolReportInput ,
88} from '@herodevs/eol-shared' ;
9+ import type { GraphQLFormattedError } from 'graphql' ;
910import { config } from '../config/constants.ts' ;
1011import { debugLogger } from '../service/log.svc.ts' ;
1112import { stripTypename } from '../utils/strip-typename.ts' ;
1213import { createReportMutation , getEolReportQuery } from './gql-operations.ts' ;
1314
15+ type GraphQLExecutionResult = {
16+ errors ?: ReadonlyArray < GraphQLFormattedError > ;
17+ } ;
18+
1419export const createApollo = ( uri : string ) =>
1520 new ApolloClient ( {
1621 cache : new InMemoryCache ( ) ,
@@ -28,13 +33,17 @@ export const createApollo = (uri: string) =>
2833
2934export const SbomScanner = ( client : ReturnType < typeof createApollo > ) => {
3035 return async ( input : CreateEolReportInput ) : Promise < EolReport > => {
31- const res = await client . mutate < EolReportMutationResponse , { input : CreateEolReportInput } > ( {
36+ let res : Awaited < ReturnType < typeof client . mutate < EolReportMutationResponse , { input : CreateEolReportInput } > > > ;
37+ res = await client . mutate < EolReportMutationResponse , { input : CreateEolReportInput } > ( {
3238 mutation : createReportMutation ,
3339 variables : { input } ,
3440 } ) ;
3541
36- if ( res ?. errors ?. length ) {
37- debugLogger ( 'GraphQL errors in createReport: %o' , res . errors ) ;
42+ if ( res ?. error || ( res as GraphQLExecutionResult ) ?. errors ) {
43+ debugLogger (
44+ 'Error returned from createReport mutation: %o' ,
45+ res . error || ( res as GraphQLExecutionResult | undefined ) ?. errors ,
46+ ) ;
3847 throw new Error ( 'Failed to create EOL report' ) ;
3948 }
4049
@@ -64,11 +73,16 @@ export const SbomScanner = (client: ReturnType<typeof createApollo>) => {
6473
6574 for ( let i = 0 ; i < pages . length ; i += config . concurrentPageRequests ) {
6675 const batch = pages . slice ( i , i + config . concurrentPageRequests ) ;
67- const batchResponses = await Promise . all ( batch ) ;
76+ let batchResponses : Awaited <
77+ ReturnType < typeof client . query < EolReportQueryResponse , { input : GetEolReportInput } > >
78+ > [ ] ;
79+
80+ batchResponses = await Promise . all ( batch ) ;
6881
6982 for ( const response of batchResponses ) {
70- if ( response ?. errors ?. length ) {
71- debugLogger ( 'GraphQL errors in getReport query: %o' , response . errors ) ;
83+ const queryErrors = ( response as GraphQLExecutionResult | undefined ) ?. errors ;
84+ if ( response ?. error || queryErrors ?. length || ! response . data ?. eol ) {
85+ debugLogger ( 'Error in getReport query response: %o' , response ?. error ?? queryErrors ?? response ) ;
7286 throw new Error ( 'Failed to fetch EOL report' ) ;
7387 }
7488
0 commit comments