Skip to content

Commit 2e9a35a

Browse files
committed
refactor: update file imports and remove unused files for better organization
1 parent 317883f commit 2e9a35a

71 files changed

Lines changed: 417 additions & 463 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/.vscode/launch.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/README.md

Lines changed: 102 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,115 +6,171 @@ We will start from `01-graphql-backend`.
66

77
Summary steps:
88

9-
- Install `graphql-request`.
10-
- Add configuration.
9+
- Configure the GraphQL endpoint.
10+
- Implement requests using fetch.
1111
- Update queries.
1212

1313
# Steps to build it
1414

15-
- `npm install` to install previous sample packages:
15+
Run the following command to install dependencies from the previous sample:
1616

1717
```bash
1818
npm install
1919
```
2020

2121
# Libraries
2222

23-
- We are going to install a library to work with graphql in front side, [graphql-request](https://github.com/prisma-labs/graphql-request). Make sure you are over `root` folder in terminal
23+
Typically, we can use libraries like [graffle](https://github.com/graffle-js/graffle) or [Apollo Client](https://www.npmjs.com/package/@apollo/client) to interact with a GraphQL API. For example:
2424

2525
```bash
26-
npm install graphql-request graphql --save
26+
npm install graffle@next graphql --save
2727
```
2828

29-
> It has `graphql` lib as peerDependency.
30-
> Install this library on frontend package.json
29+
However, in this case, we will not use any external libraries and will handle GraphQL requests manually using the native fetch API.
30+
31+
Example request using fetch:
32+
33+
```javascript
34+
const query = `
35+
query {
36+
users {
37+
id
38+
name
39+
email
40+
}
41+
}
42+
`;
43+
44+
fetch('https://your-graphql-endpoint.com/graphql', {
45+
method: 'POST',
46+
headers: {
47+
'Content-Type': 'application/json',
48+
},
49+
body: JSON.stringify({ query }),
50+
})
51+
.then((response) => response.json())
52+
.then((data) => console.log(data))
53+
.catch((error) => console.error('Error:', error));
54+
```
55+
56+
This approach gives us full control over the requests without relying on third-party dependencies.
3157

3258
# Other libraries
3359

3460
- [relay](https://github.com/facebook/relay)
35-
- [apollo-client](https://github.com/apollographql/apollo-client)
3661
- [axios](https://github.com/axios/axios): We could create grapqhl queries using axios too.
3762

3863
# Config
3964

40-
- `graphql-request` has TypeScript support and we don't need an extra package:
41-
42-
- Add `webpack proxy` config to avoid configure `cors`:
65+
- Add `vite proxy` config to avoid configure `cors`:
4366

44-
### ./config/webpack/dev.js
67+
### ./vite.config.js
4568

4669
```diff
47-
...
48-
devServer: {
49-
inline: true,
50-
host: 'localhost',
51-
port: 8080,
52-
stats: 'minimal',
53-
hot: true,
70+
import { defineConfig } from 'vite';
71+
import react from '@vitejs/plugin-react';
72+
73+
export default defineConfig({
74+
plugins: [react()],
75+
server: {
5476
proxy: {
5577
'/api': 'http://localhost:3000',
78+
'/thumbnails': 'http://localhost:3000',
5679
+ '/graphql': 'http://localhost:3000',
5780
},
5881
},
82+
});
5983
```
6084

6185
- Create `GraphQL client` for use same url in all queries:
6286

6387
### ./core/api/graphql.client.ts
6488

6589
```javascript
66-
import { GraphQLClient } from 'graphql-request';
67-
6890
const url = '/graphql';
6991

70-
export const graphQLClient = new GraphQLClient(url);
71-
92+
interface GraphqlProps<Variables> {
93+
query: string;
94+
variables?: Variables;
95+
}
96+
97+
export const graphql = async <Response, Variables = unknown>(
98+
props: GraphqlProps<Variables>
99+
): Promise<Response> => {
100+
const { query, variables } = props;
101+
const response = await fetch(url, {
102+
method: 'POST',
103+
headers: {
104+
'Content-Type': 'application/json',
105+
},
106+
body: JSON.stringify({
107+
query,
108+
variables,
109+
}),
110+
});
111+
const { data, errors } = await response.json();
112+
113+
if (errors) {
114+
console.error(errors);
115+
}
116+
117+
return data;
118+
};
72119
```
73120

121+
The `graphql function` allows sending queries and retrieving data from the GraphQL server. It is responsible for:
122+
123+
- Sending the query to the server.
124+
- Passing the required parameters or variables.
125+
- Receiving the server response.
126+
- Handling possible errors.
127+
128+
Currently, errors are simply logged to the console. However, in the future, we could implement a more robust error-handling mechanism, such as throwing exceptions, displaying user-friendly error messages, or integrating with an error-tracking system.
129+
74130
- Add barrel file:
75131

76132
### ./core/api/index.ts
77133

78134
```javascript
79135
export * from './graphql.client';
80-
81136
```
82137

83138
- Update `hotel-collection` api:
84139

85140
### ./src/pods/hotel-collection/api/hotel-collection.api.ts
86141

87142
```diff
88-
import Axios from 'axios';
89-
+ import { gql } from 'graphql-request';
90-
+ import { graphQLClient } from 'core/api';
143+
import axios from 'axios';
144+
+ import { graphql } from '#core/api';
91145
import { HotelEntityApi } from './hotel-collection.api-model';
92146

93147
const url = '/api/hotels';
94148

95149
+ interface GetHotelCollectionResponse {
96-
+ hotels: HotelEntityApi[];
150+
+ hotels: HotelEntityApi[];
97151
+ }
98152

99153
export const getHotelCollection = async (): Promise<HotelEntityApi[]> => {
100-
+ const query = gql`
101-
+ query {
102-
+ hotels {
103-
+ id
104-
+ name
105-
+ shortDescription
106-
+ hotelRating
107-
+ address1
108-
+ thumbNailUrl
109-
+ }
110-
+ }
111-
+ `;
112-
- const { data } = await Axios.get<HotelEntityApi[]>(url);
113-
- return data;
114-
+ const { hotels } = await graphQLClient.request<GetHotelCollectionResponse>(
115-
+ query
116-
+ );
117-
+ return hotels;
154+
- const { data } = await axios.get<HotelEntityApi[]>(url);
155+
- return data;
156+
+ const query = `
157+
+ query {
158+
+ hotels {
159+
+ id
160+
+ name
161+
+ shortDescription
162+
+ hotelRating
163+
+ address1
164+
+ thumbNailUrl
165+
+ }
166+
+ }
167+
+ `;
168+
169+
+ const { hotels } = await graphql<GetHotelCollectionResponse>({
170+
+ query,
171+
+ });
172+
173+
+ return hotels;
118174
};
119175

120176
...

06-rest-api/03-graphql/02-crud/02-graphql-frontend/config/webpack/base.js

Lines changed: 0 additions & 33 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/config/webpack/dev.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/config/webpack/helpers.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/config/webpack/prod.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/dev.env

Lines changed: 0 additions & 2 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/02-graphql-frontend/src/index.html renamed to 06-rest-api/03-graphql/02-crud/02-graphql-frontend/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
</head>
88
<body>
99
<div id="root"></div>
10+
<script type="module" src="/src/index.tsx"></script>
1011
</body>
1112
</html>

0 commit comments

Comments
 (0)