Scalars client provides auto-generated and type-safe query client for your application build in scalars
The first step when using Scalars Client is installing its npm package as follows:
yarn add @scalars/clinpm install @scalars/cliOnce you installed the package it's needed to invoke the sync
command which reads your scalars endpoint and client ID from your
environment variables, don't forget to create an .env.dev
file as follows:
# YOUR SCALARS ENDPOINT GOES HERE
SCALARS_API=https://app.scalars.co/qoboe5t4aH/api
# or
SCALARS_ENDPOINT=https://app.scalars.co/qoboe5t4aH/api
# YOUR SCALARS CLIENT ID GOES HERE
SCALARS_CLIENT_ID=c921b093-4334e-1be1-c0cb-5aa907d3d912Once Scalars Client has been generated you can import in your code
import { ScalarsClient } from '@scalars/cli'
const client = new ScalarsClient()Then you can start using operations like creating, getting, updating and deleting on your entities:
const car = await client.query.car( {
where: { id: '22bc79036f26481e' }
} )💡
carwill be an object like{ id: '22bc79036f26481e' }
Note that you need to provide the
selectfield on every operation to indicate which attributes of the entity you need to retrieve, otherwise it will only return a simple object:
const car = await client.query.car( {
select: { id: true, model: true, brand: true },
where: { id: '22bc79036f26481e' }
} )const newCar = await client.mutation.createCar( {
data: { brand: 'Toyota', model: 'T90' }
} )const updatedCar = await client.mutation.updateCar( {
data: { brand: 'BMW' },
where: { id: '22bc79036f26481e' }
} )const deletedCar = await client.mutation.deleteCar( {
where: { id: '22bc79036f26481e' }
} )Scalars Client offers type definitions, these ensure that all your operations are type safe and validated, also enables features like IntelliSense (autocompletion) in your editor.
Examples above would look as follows with type definitions:
const carSelect: CarSelect = { id: true, model: true, brand: true }
const carWhere: CarWhereUniqueInput = { id: '22bc79036f26481e' }
const car: ICar = await client.query.car( {
select: carSelect,
where: carWhere
} )const carData: CarCreateInput = { brand: 'Toyota', model: 'T90' }
const newCar: ICar = await client.mutation.createCar( {
data: carData
} )const carData: CarUpdateInput = { brand: 'BMW' }
const carWhere: CarWhereUniqueInput = { id: '22bc79036f26481e' }
const updatedCar = await client.mutation.updateCar( {
data: carData,
where: carWhere
} )const carWhere: CarWhereUniqueInput = { id: '22bc79036f26481e' }
const deletedCar = await client.mutation.deleteCar( {
where: carWhere
} )