@@ -9,9 +9,11 @@ import {
99import { squashWhitespace } from "@code-chronicles/util/squashWhitespace" ;
1010import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow" ;
1111
12- import { fetchGraphQLData } from "./fetchGraphQLData.ts" ;
1312import { normalizeGraphQLDescription } from "./normalizeGraphQLDescription.ts" ;
1413import { sortByName } from "./sortByName.ts" ;
14+ import { getGraphQLClient } from "./getGraphQLClient.ts" ;
15+ import { rawRequest } from "graphql-request" ;
16+ import { GraphQLError } from "graphql" ;
1517
1618function getTypeFields ( depth : number ) : string {
1719 const base = "name kind" ;
@@ -78,9 +80,10 @@ function graphqlDecode(s: string): string {
7880 ) . toString ( "utf8" ) ;
7981}
8082
81- function getQueryAndVariables (
82- typeNames : Iterable < string > ,
83- ) : [ string , Record < string , string > ] {
83+ function getQueryAndVariables ( typeNames : Iterable < string > ) : {
84+ query : string ;
85+ variables : Record < string , string > ;
86+ } {
8487 const variables = Object . fromEntries (
8588 // TODO: .map the iterator once that's more widespread!
8689 [ ...typeNames ] . map ( ( typeName , index ) => [ `typeName${ index } ` , typeName ] ) ,
@@ -94,12 +97,12 @@ function getQueryAndVariables(
9497 `${ graphqlEncode ( typeName ) } : __type(name: $${ variable } ) { ...TypeFields }` ,
9598 ) ;
9699
97- return [
98- squashWhitespace (
100+ return {
101+ query : squashWhitespace (
99102 `query (${ queryArgs . join ( "," ) } ) {${ queryFields . join ( "," ) } }\n${ FRAGMENT } ` ,
100103 ) ,
101104 variables,
102- ] ;
105+ } ;
103106}
104107
105108const innerTypeZodTypeBase = z . strictObject ( {
@@ -203,13 +206,18 @@ export type LeetCodeGraphQLType = NonNullable<
203206
204207export async function fetchGraphQLTypeInformation (
205208 typeNames : string [ ] ,
209+ onErrors ?: ( errors : GraphQLError [ ] ) => void ,
206210) : Promise < Record < string , LeetCodeGraphQLType | null > > {
207211 const distinctTypeNames = new Set ( typeNames ) ;
208212 if ( distinctTypeNames . size === 0 ) {
209213 return { } ;
210214 }
211215
212- const [ query , variables ] = getQueryAndVariables ( distinctTypeNames ) ;
213- const { data } = await fetchGraphQLData ( query , variables ) ;
216+ const { data, errors } = await getGraphQLClient ( "all" ) . rawRequest (
217+ getQueryAndVariables ( distinctTypeNames ) ,
218+ ) ;
219+
220+ errors && errors . length > 0 && onErrors ?.( errors ) ;
221+
214222 return graphqlTypeZodType . parse ( data ) ;
215223}
0 commit comments