From f111685c5886f3e77edfd3621c98d8ac1b9bcce4 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 24 Nov 2023 20:00:08 +0100 Subject: refactor(services, types): reorganize GraphQL fetchers and data types The Typescript mapped types was useful for autocompletion in fetchers but their are harder to maintain. I think it's better to keep each query close to its fetcher to have a better understanding of the fetched data. So I: * colocate queries with their own fetcher * colocate mutations with their own mutator * remove Typescript mapped types for queries and mutations * move data convertors inside graphql services * rename most of data types and fetchers --- tests/utils/graphql/connections.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'tests/utils/graphql') diff --git a/tests/utils/graphql/connections.ts b/tests/utils/graphql/connections.ts index f38fa59..dc4bfc6 100644 --- a/tests/utils/graphql/connections.ts +++ b/tests/utils/graphql/connections.ts @@ -1,4 +1,9 @@ -import type { EdgesResponse, GraphQLEdges, Maybe } from '../../../src/types'; +import type { + GraphQLConnection, + GraphQLEdge, + Maybe, + Nullable, +} from '../../../src/types'; import { CONFIG } from '../../../src/utils/config'; /** @@ -8,7 +13,7 @@ import { CONFIG } from '../../../src/utils/config'; * @param {number} offset - The offset. * @returns {Array>} The edges. */ -export const getEdges = (data: T[], offset: number): GraphQLEdges[] => +export const getEdges = (data: T[], offset: number): GraphQLEdge[] => data.map((singleData, index) => { const currentItemNumber = index + 1; @@ -21,7 +26,7 @@ export const getEdges = (data: T[], offset: number): GraphQLEdges[] => type GetConnectionProps = { data: Maybe; first: Maybe; - after: Maybe; + after: Maybe>; }; /** @@ -37,7 +42,7 @@ export const getConnection = ({ after, data = [], first = CONFIG.postsPerPage, -}: GetConnectionProps): EdgesResponse => { +}: GetConnectionProps): GraphQLConnection => { const afterInt = after ? Number(after.replace('cursor', '')) : 0; const edges = getEdges(data.slice(afterInt, afterInt + first), afterInt); const endCursor = @@ -47,7 +52,9 @@ export const getConnection = ({ edges, pageInfo: { endCursor, + hasPreviousPage: typeof after !== 'undefined', hasNextPage: data.length - afterInt > first, + startCursor: after ?? 'cursor1', total: data.length, }, }; -- cgit v1.2.3