GraphQL
1. A Simple GraphQL Adapter
import type { DataAdapter, QueryParams } from '@tablecraft/table';
const USERS_QUERY = `
query GetUsers($first: Int, $offset: Int, $orderBy: String) {
users(first: $first, offset: $offset, orderBy: $orderBy) {
totalCount
nodes {
id
name
email
role
}
}
}
`;
export function createGraphQLAdapter<T>(endpoint: string): DataAdapter<T> {
return {
async query(params: QueryParams) {
// 1. Map Pagination and Sorting to GraphQL variables
const variables = {
first: params.pageSize,
offset: (params.page - 1) * params.pageSize, // Using 1-based index conversion
orderBy: params.sort ? `${params.sort}_${params.sortOrder.toUpperCase()}` : null,
};
// 2. Execute the Query
const response = await fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
query: USERS_QUERY,
variables,
}),
});
const { data, errors } = await response.json();
if (errors) {
throw new Error(errors[0].message);
}
// 3. Return TableCraft QueryResult shape
const { totalCount, nodes } = data.users;
return {
data: nodes,
meta: {
total: totalCount,
page: params.page,
pageSize: params.pageSize,
totalPages: Math.ceil(totalCount / params.pageSize)
}
};
}
};
}2. Dynamic Column Selection
Last updated
Was this helpful?
