async function safeGraphQLQuery(query, variables = {}) {
try {
const response = await fetch('https://api.buildbetter.app/graphql', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({ query, variables })
});
const result = await response.json();
// Check for GraphQL errors
if (result.errors) {
console.error('GraphQL Errors:', result.errors);
// Handle specific error types
result.errors.forEach(error => {
if (error.message.includes('permission denied')) {
throw new Error('Access denied. Check your API key permissions.');
}
if (error.message.includes('not found')) {
throw new Error('Resource not found.');
}
});
throw new Error('GraphQL query failed');
}
return result.data;
} catch (error) {
// Handle network errors
if (error.name === 'NetworkError') {
console.error('Network error:', error);
throw new Error('Unable to connect to API');
}
// Re-throw other errors
throw error;
}
}