-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
beb1293
commit 8173cad
Showing
18 changed files
with
788 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
BLOG_API_URL=http://localhost:8000/api/posts | ||
ROOT_URL=http://localhost:3000 | ||
ROOT_URL=http://localhost:3000 | ||
BLOG_GRAPHQL_URL=http://localhost:8000/graphql |
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,8 @@ | ||
import { ApolloClient, InMemoryCache } from '@apollo/client'; | ||
|
||
const apolloClient = new ApolloClient({ | ||
uri: process.env.BLOG_GRAPHQL_URL, | ||
cache: new InMemoryCache(), | ||
}); | ||
|
||
export default apolloClient; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { ApolloClient, NormalizedCacheObject, gql } from '@apollo/client'; | ||
|
||
import { POST_PAGE_SIZE } from '../app/constants'; | ||
import { Post, PostsAPIResponse } from '../app/lib/definitions'; | ||
import { BaseDataProvider } from './base'; | ||
import { PaginatedPosts, PostSearchOptions } from './interface'; | ||
import { calculateTotalPages } from './utils'; | ||
|
||
const POST_FRAGMENT = gql` | ||
fragment PostFragment on PostType { | ||
title | ||
date | ||
content | ||
slug | ||
tags { | ||
name | ||
} | ||
} | ||
`; | ||
|
||
export const GET_POST = gql` | ||
query GetPosts($slug: String!) { | ||
post(slug: $slug) { | ||
...PostFragment | ||
} | ||
} | ||
${POST_FRAGMENT} | ||
`; | ||
|
||
export const GET_POSTS = gql` | ||
query GetPost($search: String, $limit: Int, $offset: Int) { | ||
posts(search: $search, limit: $limit, offset: $offset) { | ||
...PostFragment | ||
} | ||
totalPosts(search: $search) | ||
} | ||
${POST_FRAGMENT} | ||
`; | ||
|
||
export const GET_POST_METADATA = gql` | ||
query GetPostMetadata($slug: String!) { | ||
post(slug: $slug) { | ||
title | ||
metaDescription | ||
} | ||
} | ||
`; | ||
|
||
type PostsOffsetBased = { | ||
posts: Post[]; | ||
totalPosts: number; | ||
}; | ||
|
||
export class GraphqlDataProvider extends BaseDataProvider { | ||
private client: ApolloClient<NormalizedCacheObject>; | ||
|
||
constructor(client: ApolloClient<NormalizedCacheObject>) { | ||
super(); | ||
this.client = client; | ||
} | ||
|
||
async getPosts(options: PostSearchOptions): Promise<PaginatedPosts> { | ||
const currentPage = options.currentPage || 1; | ||
const limit = options.pageSize || POST_PAGE_SIZE; | ||
|
||
const offset = (currentPage - 1) * limit; | ||
|
||
const variables = options.query | ||
? { search: options.query, limit, offset } | ||
: { limit, offset }; | ||
|
||
const { data } = await this.client.query<PostsOffsetBased>({ | ||
query: GET_POSTS, | ||
variables, | ||
}); | ||
return { | ||
posts: data.posts, | ||
totalPages: calculateTotalPages(data.totalPosts, POST_PAGE_SIZE), | ||
}; | ||
} | ||
|
||
async getPost(slug: string): Promise<Post | null> { | ||
try { | ||
const { data } = await this.client.query<{ post: Post }>({ | ||
query: GET_POST, | ||
variables: { slug }, | ||
}); | ||
|
||
return data.post || null; | ||
} catch (error) { | ||
console.error('Error fetching post:', error); | ||
return null; | ||
} | ||
} | ||
|
||
async getPostMetadata(slug: string): Promise<Post | null> { | ||
try { | ||
const { data } = await this.client.query<{ post: Post }>({ | ||
query: GET_POST_METADATA, | ||
variables: { slug }, | ||
}); | ||
|
||
return data.post || null; | ||
} catch (error) { | ||
console.error('Error fetching post metadata:', error); | ||
return null; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.