Skip to content

Commit

Permalink
refactor(api): replace graphql-request with fetch for GraphQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert27 committed Dec 5, 2024
1 parent 48229da commit 967cd79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"expo-system-ui": "~4.0.5",
"fuse.js": "^7.0.0",
"graphql": "^16.9.0",
"graphql-request": "^7.1.2",
"i18next": "^23.16.8",
"metro": "^0.81.0",
"moment": "^2.30.1",
Expand Down
36 changes: 28 additions & 8 deletions src/api/neuland-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import {
type UniversitySportsQuery,
} from '@/__generated__/gql/graphql'
import { type SpoWeights } from '@/types/asset-api'
// Import the generated type
import { type RequestDocument, type Variables, request } from 'graphql-request'
import { type DocumentNode, print } from 'graphql'

import packageInfo from '../../package.json'
import {
Expand Down Expand Up @@ -44,12 +43,33 @@ class NeulandAPIClient {
}
}

async performGraphQLQuery(
query: RequestDocument,
variables?: Variables
): Promise<any> {
const data = await request(GRAPHQL_ENDPOINT, query, variables)
return data
async performGraphQLQuery<T>(
query: DocumentNode,
variables?: Record<string, any>
): Promise<T> {
const resp = await fetch(GRAPHQL_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'User-Agent': USER_AGENT,
},
body: JSON.stringify({
query: print(query),
variables,
}),
})

const json = await resp.json()

if (resp.ok && json.errors == null) {
return json.data as T
} else {
const errorMessage =
json.errors != null
? JSON.stringify(json.errors)
: resp.statusText
throw new Error('GraphQL error: ' + errorMessage)
}
}

/**
Expand Down

0 comments on commit 967cd79

Please sign in to comment.