Overview
Cortado.js also offers a GraphQL API that provides flexible querying capabilities for image optimization and resizing. With the GraphQL API, you can specify exactly what operations you need to perform on your images and retrieve the results in a single request.
Usage
To interact with Cortado.js via GraphQL API, you can send GraphQL queries to the designated endpoint. Here's how you can use the GraphQL API to optimize and resize images:
Optimize Image
mutation {
optimizeImage(imageUrl: "URL_OF_YOUR_IMAGE") {
optimizedImageUrl
}
}
Send this mutation query to optimize the image specified by its URL. Cortado.js will optimize the image and return the URL of the optimized image in the response.
Resize Image
mutation {
resizeImage(imageUrl: "URL_OF_YOUR_IMAGE", width: 300, height: 200) {
resizedImageUrl
}
}
Send this mutation query to resize the image specified by its URL to the desired width and height. Cortado.js will resize the image and return the URL of the resized image in the response.
Example
Using Apollo Client:
import { gql } from '@apollo/client';
const OPTIMIZE_IMAGE_MUTATION = gql`
mutation OptimizeImage($imageUrl: String!) {
optimizeImage(imageUrl: $imageUrl) {
optimizedImageUrl
}
}
`;
const RESIZE_IMAGE_MUTATION = gql`
mutation ResizeImage($imageUrl: String!, $width: Int!, $height: Int!) {
resizeImage(imageUrl: $imageUrl, width: $width, height: $height) {
resizedImageUrl
}
}
`;
client.mutate({
mutation: OPTIMIZE_IMAGE_MUTATION,
variables: { imageUrl: 'URL_OF_YOUR_IMAGE' }
}).then(result => {
const optimizedImageUrl = result.data.optimizeImage.optimizedImageUrl;
}).catch(error => {
console.error('Error optimizing image:', error);
});
client.mutate({
mutation: RESIZE_IMAGE_MUTATION,
variables: {
imageUrl: 'URL_OF_YOUR_IMAGE',
width: 300,
height: 200
}
}).then(result => {
const resizedImageUrl = result.data.resizeImage.resizedImageUrl;
}).catch(error => {
console.error('Error resizing image:', error);
});
This example demonstrates how to send GraphQL mutation queries to the Cortado.js GraphQL API endpoint to optimize and resize images.