-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
project frontend to backend connect api with test
- Loading branch information
1 parent
40b4158
commit b89e3b7
Showing
2 changed files
with
169 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { gql } from '@apollo/client'; | ||
import client from '../../../utils/apolloClient'; | ||
|
||
// Define the queries and mutations | ||
|
||
// Fetch user projects | ||
export const GET_USER_PROJECTS = gql` | ||
query GetUserProjects { | ||
getUserProjects { | ||
id | ||
projectName | ||
path | ||
projectPackages { | ||
id | ||
content | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const getUserProjects = async (): Promise<any> => { | ||
try { | ||
const response = await client.query({ | ||
query: GET_USER_PROJECTS, | ||
}); | ||
return response.data.getUserProjects; | ||
} catch (error) { | ||
console.error('Error fetching user projects:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
// Fetch project details | ||
export const GET_PROJECT_DETAILS = gql` | ||
query GetProjectDetails($projectId: String!) { | ||
getProjectDetails(projectId: $projectId) { | ||
id | ||
projectName | ||
path | ||
projectPackages { | ||
id | ||
content | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const getProjectDetails = async (projectId: string): Promise<any> => { | ||
try { | ||
const response = await client.query({ | ||
query: GET_PROJECT_DETAILS, | ||
variables: { projectId }, | ||
}); | ||
return response.data.getProjectDetails; | ||
} catch (error) { | ||
console.error('Error fetching project details:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
// Upsert project (Create or Update) | ||
export const UPSERT_PROJECT = gql` | ||
mutation UpsertProject($upsertProjectInput: UpsertProjectInput!) { | ||
upsertProject(upsertProjectInput: $upsertProjectInput) { | ||
id | ||
projectName | ||
path | ||
projectPackages { | ||
id | ||
content | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const upsertProject = async (upsertProjectInput: any): Promise<any> => { | ||
try { | ||
const response = await client.mutate({ | ||
mutation: UPSERT_PROJECT, | ||
variables: { | ||
upsertProjectInput, | ||
}, | ||
}); | ||
return response.data.upsertProject; | ||
} catch (error) { | ||
console.error('Error creating/updating project:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
// Delete project | ||
export const DELETE_PROJECT = gql` | ||
mutation DeleteProject($projectId: String!) { | ||
deleteProject(projectId: $projectId) | ||
} | ||
`; | ||
|
||
export const deleteProject = async (projectId: string): Promise<boolean> => { | ||
try { | ||
const response = await client.mutate({ | ||
mutation: DELETE_PROJECT, | ||
variables: { projectId }, | ||
}); | ||
return response.data.deleteProject; | ||
} catch (error) { | ||
console.error('Error deleting project:', error); | ||
throw error; | ||
} | ||
}; | ||
|
||
// Remove package from project | ||
export const REMOVE_PACKAGE_FROM_PROJECT = gql` | ||
mutation RemovePackageFromProject($projectId: String!, $packageId: String!) { | ||
removePackageFromProject(projectId: $projectId, packageId: $packageId) | ||
} | ||
`; | ||
|
||
export const removePackageFromProject = async (projectId: string, packageId: string): Promise<boolean> => { | ||
try { | ||
const response = await client.mutate({ | ||
mutation: REMOVE_PACKAGE_FROM_PROJECT, | ||
variables: { projectId, packageId }, | ||
}); | ||
return response.data.removePackageFromProject; | ||
} catch (error) { | ||
console.error('Error removing package from project:', error); | ||
throw error; | ||
} | ||
}; |
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,40 @@ | ||
import { getUserProjects, getProjectDetails, deleteProject, upsertProject, removePackageFromProject } from '../app/api/project/route'; | ||
|
||
describe('Project API', () => { | ||
let projectId: string; | ||
let packageId: string; | ||
|
||
it('should upsert a project', async () => { | ||
const upsertProjectInput = { | ||
projectName: 'Test Project', | ||
projectPackages: ['Package 1', 'Package 2'], | ||
}; | ||
const project = await upsertProject(upsertProjectInput); | ||
expect(project).toHaveProperty('id'); | ||
projectId = project.id; | ||
console.log('Project id is: ' + projectId) | ||
packageId = project.projectPackages[0].id; | ||
}); | ||
|
||
it('should get user projects', async () => { | ||
const projects = await getUserProjects(); | ||
expect(Array.isArray(projects)).toBe(true); | ||
expect(projects.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('should get project details', async () => { | ||
const projectDetails = await getProjectDetails(projectId); | ||
expect(projectDetails).toHaveProperty('id', projectId); | ||
}); | ||
|
||
it('should remove a package from project', async () => { | ||
const removed = await removePackageFromProject(projectId, packageId); | ||
expect(removed).toBe(true); | ||
}); | ||
|
||
it('should delete a project', async () => { | ||
const deleted = await deleteProject(projectId); | ||
expect(deleted).toBe(true); | ||
}); | ||
}); | ||
|