Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

api.ts

44 lines800 Bytes

1import { gql, request } from 'graphql-request'
2import type { Post } from './types'
3​
4const endpoint = 'https://graphqlzero.almansi.me/api'
5​
6export const postsAPI = {
7 getPosts: async () => {
8 const {
9 posts: { data },
10 } = await request<{
11 posts: { data: Array<Pick<Post, 'id' | 'title'>> }
12 }>(
13 endpoint,
14 gql`
15 query {
16 posts {
17 data {
18 id
19 title
20 }
21 }
22 }
23 `,
24 )
25 return data
26 },
27 getPost: async (id: string) => {
28 const { post } = await request<{ post: Post }>(
29 endpoint,
30 gql`
31 query Post($id: ID!) {
32 post(id: $id) {
33 id
34 title
35 body
36 }
37 }
38 `,
39 { id },
40 )
41​
42 return post
43 },