import fetch from 'node-fetch';
const GRAPHQL_URL = process.env.GRAPHQL_URL || 'http://localhost:4000/graphql';
export async function graphqlRequest(query, variables = {}) {
const response = await fetch(GRAPHQL_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables,
}),
});
const result = await response.json();
if (result.errors) {
throw new Error(result.errors.map(e => e.message).join(', '));
}
return result.data;
}
export async function getAllADLEntries() {
const query = `
query {
adlEntries {
id
created
lastEdited
author
title
decision
factSheets
status
}
}
`;
const data = await graphqlRequest(query);
return data.adlEntries;
}
export async function getADLEntry(id) {
const query = `
query GetADLEntry($id: ID!) {
adlEntry(id: $id) {
id
created
lastEdited
author
title
decision
factSheets
status
}
}
`;
const data = await graphqlRequest(query, { id });
return data.adlEntry;
}
export async function createADLEntry(input) {
const mutation = `
mutation CreateADLEntry($input: CreateADLEntryInput!) {
createADLEntry(input: $input) {
id
created
lastEdited
author
title
decision
factSheets
status
}
}
`;
const data = await graphqlRequest(mutation, { input });
return data.createADLEntry;
}
export async function updateADLEntry(id, input) {
const mutation = `
mutation UpdateADLEntry($id: ID!, $input: UpdateADLEntryInput!) {
updateADLEntry(id: $id, input: $input) {
id
created
lastEdited
author
title
decision
factSheets
status
}
}
`;
const data = await graphqlRequest(mutation, { id, input });
return data.updateADLEntry;
}
export async function deleteADLEntry(id) {
const mutation = `
mutation DeleteADLEntry($id: ID!) {
deleteADLEntry(id: $id)
}
`;
const data = await graphqlRequest(mutation, { id });
return data.deleteADLEntry;
}
export async function searchADLEntries(search) {
const query = `
query SearchADLEntries($search: SearchADLInput!) {
searchADLEntries(search: $search) {
id
created
lastEdited
author
title
decision
factSheets
status
}
}
`;
const data = await graphqlRequest(query, { search });
return data.searchADLEntries;
}