forked from blockscout/frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial graph client impl * Fetching logic * Generalize types * Validate graph env * Trim name Signed-off-by: Wolmin <[email protected]>
- Loading branch information
Showing
9 changed files
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { GraphResponse } from 'types/api/universalProfile'; | ||
|
||
import { getEnvValue } from 'configs/app/utils'; | ||
|
||
import { constructQuery } from './graphQueries'; | ||
import type { QueryOperation, SearchProfileQueryResponse } from './graphTypes'; | ||
|
||
const graphUrl = getEnvValue('NEXT_PUBLIC_GRAPH_URL') || ''; | ||
|
||
type FetchFunc<T> = (queryParams?: string) => Promise<GraphResponse<T> | null>; | ||
|
||
type GraphClient = { | ||
getProfiles: FetchFunc<SearchProfileQueryResponse>; | ||
}; | ||
|
||
const queryParamsToGraphQuery = (operationName: QueryOperation, queryParams?: string): string => { | ||
const query = constructQuery(operationName, queryParams); | ||
|
||
return JSON.stringify({ | ||
operationName: operationName, | ||
query: query, | ||
}); | ||
}; | ||
|
||
const fetchQuery = <T>(operationName: QueryOperation): FetchFunc<T> => { | ||
return async(queryParams?: string): Promise<GraphResponse<T> | null> => { | ||
const query = queryParamsToGraphQuery(operationName, queryParams); | ||
|
||
try { | ||
const resp = await fetch(graphUrl, { | ||
method: 'POST', | ||
headers: {}, | ||
body: query, | ||
}); | ||
const json = await resp.json(); | ||
return json as GraphResponse<T>; | ||
} catch (err) { | ||
return null; | ||
} | ||
}; | ||
}; | ||
|
||
const createGraphClient = (): GraphClient => ({ | ||
getProfiles: fetchQuery<SearchProfileQueryResponse>('search_profiles'), | ||
}); | ||
|
||
export const graphClient: GraphClient = createGraphClient(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { QueryOperation } from './graphTypes'; | ||
|
||
type QueryConstructor = (queryParams?: string) => string; | ||
|
||
export const constructQuery = (operationType: QueryOperation, queryParams?: string) => { | ||
const queryConstruct = queryConstructors[operationType]; | ||
|
||
return queryConstruct(queryParams); | ||
}; | ||
|
||
export const searchProfileQuery = (queryParams?: string): string => `query search_profiles { | ||
search_profiles(args: { search: "${ queryParams }" }) { | ||
profileImages(order_by: { width: asc }) { | ||
src, | ||
width, | ||
}, | ||
id, | ||
name, | ||
fullName, | ||
} | ||
}`; | ||
|
||
const queryConstructors: Record<QueryOperation, QueryConstructor> = { | ||
search_profiles: searchProfileQuery, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export type ProfileImage = { | ||
src: string; | ||
width: number; | ||
}; | ||
|
||
export type SearchProfileQueryResponse = { | ||
profileImages: Array<ProfileImage>; | ||
id: string; | ||
name: string; | ||
fullName: string; | ||
}; | ||
|
||
export type GraphQuery = { | ||
operationName: string; | ||
query: string; | ||
}; | ||
|
||
export type QueryOperation = 'search_profiles'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters