Skip to content

Commit a7ec134

Browse files
authored
Update datasource.ts
1 parent 880ecfc commit a7ec134

File tree

1 file changed

+95
-55
lines changed

1 file changed

+95
-55
lines changed

src/datasource.ts

Lines changed: 95 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,107 @@
1-
import { RESTDataSource, RequestOptions } from '@apollo/datasource-rest';
2-
import sdk from '@stackblitz/sdk'; // Ensure you have this package installed
3-
4-
export class StackBlitzAPI extends RESTDataSource {
1+
import { RESTDataSource } from '@apollo/datasource-rest';
52

3+
class StackBlitzAPI extends RESTDataSource {
64
constructor() {
75
super();
8-
9-
// If there's a base URL for StackBlitz API, set it here.
10-
this.baseURL = 'https://api.stackblitz.com'; // Hypothetical; adjust as necessary.
11-
}
6+
// Set the base URL for the StackBlitz API
7+
this.baseURL = 'https://api.stackblitz.com'; // Adjust as necessary
8+
}
9+
10+
// Fetch all projects with optional pagination
11+
async getProjects(first?: number, after?: string) {
12+
const response = await this.get('/projects', {
13+
first,
14+
after,
15+
});
16+
return this.transformProjects(response);
17+
}
18+
19+
// Fetch a single project by ID
20+
async getProjectById(id: string) {
21+
const response = await this.get(`/projects/${id}`);
22+
return this.transformProject(response);
23+
}
1224

13-
async getProjects(first?: number, after?: string) {
14-
// Logic to fetch projects with pagination support.
15-
const response = await sdk.getProjects({ first, after });
16-
return this.transformProjects(response);
17-
}
25+
// Fetch all users with optional pagination
26+
async getUsers(first?: number, after?: string) {
27+
const response = await this.get('/users', {
28+
first,
29+
after,
30+
});
31+
return this.transformUsers(response);
32+
}
1833

19-
async getProjectById(id: string) {
20-
// Logic to fetch a single project by ID.
21-
const response = await sdk.getProjectById(id);
22-
return this.transformProject(response);
23-
}
34+
// Fetch a single user by ID
35+
async getUserById(id: string) {
36+
const response = await this.get(`/users/${id}`);
37+
return this.transformUser(response);
38+
}
2439

25-
async getUsers(first?: number, after?: string) {
26-
// Logic to fetch users with pagination support.
27-
const response = await sdk.getUsers({ first, after });
28-
return this.transformUsers(response);
29-
}
40+
// Create a new project
41+
async createProject(input: { title: string; description?: string }) {
42+
const response = await this.post('/projects', input);
43+
return { project: this.transformProject(response) };
44+
}
3045

31-
async getUserById(id: string) {
32-
// Logic to fetch a single user by ID.
33-
const response = await sdk.getUserById(id);
34-
return this.transformUser(response);
35-
}
46+
// Update an existing project
47+
async updateProject(id: string, input: { title?: string; description?: string }) {
48+
const response = await this.put(`/projects/${id}`, input);
49+
return { project: this.transformProject(response) };
50+
}
3651

37-
async createProject(input) {
38-
const response = await sdk.createProject(input);
39-
return { project: this.transformProject(response) };
40-
}
52+
// Delete a project by ID
53+
async deleteProject(id: string) {
54+
await this.delete(`/projects/${id}`);
55+
return { success: true };
56+
}
4157

42-
async updateProject(id: string, input) {
43-
const response = await sdk.updateProject(id, input);
44-
return { project: this.transformProject(response) };
45-
}
58+
// Transformations to match GraphQL schema types
4659

47-
async deleteProject(id: string) {
48-
await sdk.deleteProject(id);
49-
return { success: true };
50-
}
60+
private transformProjects(response: any) {
61+
return {
62+
edges: response.items.map((project: any) => ({
63+
cursor: project.id,
64+
node: this.transformProject(project),
65+
})),
66+
pageInfo: {
67+
hasNextPage: response.hasNextPage,
68+
endCursor: response.endCursor,
69+
},
70+
};
71+
}
5172

52-
private transformProjects(response) {
53-
// Transform the response into ProjectConnection format as needed.
54-
}
55-
56-
private transformUsers(response) {
57-
// Transform the response into UserConnection format as needed.
58-
}
59-
60-
private transformUser(response) {
61-
// Transform the response into User format as needed.
62-
}
63-
64-
private transformProject(response) {
65-
// Transform the response into Project format as needed.
66-
}
73+
private transformProject(project: any) {
74+
return {
75+
id: project.id,
76+
title: project.title,
77+
description: project.description,
78+
createdAt: project.createdAt,
79+
updatedAt: project.updatedAt,
80+
owner: project.owner, // Assuming owner is already a User object
81+
};
82+
}
83+
84+
private transformUsers(response: any) {
85+
return {
86+
edges: response.items.map((user: any) => ({
87+
cursor: user.id,
88+
node: this.transformUser(user),
89+
})),
90+
pageInfo: {
91+
hasNextPage: response.hasNextPage,
92+
endCursor: response.endCursor,
93+
},
94+
};
95+
}
96+
97+
private transformUser(user: any) {
98+
return {
99+
id: user.id,
100+
username: user.username,
101+
email: user.email,
102+
projects: user.projects, // Assuming projects is already structured correctly
103+
};
104+
}
67105
}
106+
107+
export default StackBlitzAPI;

0 commit comments

Comments
 (0)