Introduction
FrontQL is a lightweight front-end querying language designed to streamline client-server communication for web applications. It simplifies data retrieval by allowing clients to request exactly what they need from the server. One of the key features of FrontQL is its GET API, which enables clients to fetch data from the server using HTTP GET requests.
The GET API of FrontQL is based on a structured query language that is both powerful and easy to understand. When a client makes a GET request to a FrontQL-enabled server, it includes a query parameter that specifies the fields and objects to be returned. The server then processes this query and returns the requested data in a structured format, typically JSON, which can be easily consumed by the client application.
By using the GET API of FrontQL, developers can reduce the amount of data transferred over the network, minimize the need for multiple API endpoints, and ensure that applications are more performant and scalable.
Example
Here’s a simple example of a FrontQL GET API request:
async function getUsers () {
const response = await Api . get ( " /users " );
async function getUsers () {
const url = ` ${ BASE_URL } /users ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
Output
This example fetches all users from the server and returns them in a JSON format.
"created_at" : " 2022-01-01 00:00:00.000 " ,
"updated_at" : " 2022-01-01 00:00:00.000 "
"created_at" : " 2022-01-01 00:00:00.000 " ,
"updated_at" : " 2022-01-01 00:00:00.000 "
Parameters
page
: string
sort
: string
joins
: string
hidden
: string
fields
: string
filter
: string
search
: string
page
It is required for pagination.
There are two parameters in the page
parameter: page
and limit
.
page
is the page number and limit
is the number of items per page.
page
and limit
are separated by a comma.
Example:
async function getUsers () {
const response = await Api . get ( " /users " , {
page: " 1,15 " , // page = 1, limit = 15
async function getUsers () {
const url = ` ${ BASE_URL } /users?page=1,15 ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
sort
It is required for sorting.
Example:
async function getUsers () {
const response = await Api . get ( " /users " , {
async function getUsers () {
const url = ` ${ BASE_URL } /users?sort=-created_at ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
joins
It is required for joining two or more collections.
Example:
async function getUsersPosts () {
const response = await Api . get ( " /posts " , {
async function getUsers () {
const url = ` ${ BASE_URL } /users ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
collections: " user:users " , // join becomes collections in fetch
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
hidden
It is required for hiding fields which should not be returned.
Example:
async function getUsers () {
const response = await Api . get ( " /users " , {
async function getUsers () {
const url = ` ${ BASE_URL } /users ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
fields
It is required for specifying the fields to be returned.
Example:
async function getUsers () {
const response = await Api . get ( " /users " , {
async function getUsers () {
const url = ` ${ BASE_URL } /users ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
filter
It is required for filtering.
If the filter changes, the token has to be regenerated.
It is passed in the headers of the request.
Example:
async function getUsers () {
const response = await Api . get ( " /users " , {
async function getUsers () {
const url = ` ${ BASE_URL } /users ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
search
It is required for searching.
If search changes, no need to regenerate the token.
It is passed to the params of the request.
Example:
async function getUsers () {
const response = await Api . get ( " /users " , {
async function getUsers () {
const url = ` ${ BASE_URL } /users?search=name:John ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();
Single Data Fetch
It is possible to fetch a single object from the server.
Simply add the ID of the object after the DB_NAME
in the URL with a /
.
Example:
async function getUser () {
const response = await Api . get ( " /users/1 " );
async function getUsers () {
const url = ` ${ BASE_URL } /users/1 ` ; // Replace BASE_URL with the base URL of API server
const response = await fetch ( url , {
" Content-Type " : " application/json " ,
app: DATABASE , // Replace DATABASE with the name of your database
token: TOKEN , // Replace TOKEN with the token
// Authorization: `Bearer ${AUTH_TOKEN}`, // Replace AUTH_TOKEN with the authorization token
const data = await response . json ();