Movie
This page documents the movie
model.
Data Shape
Gadget's database stores movie records by storing and retrieving each of the fields defined on the model in the Gadget Editor to a managed database. Gadget has generated a GraphQL type matching the configured fields for movie:
1export type Movie = {2 __typename: "Movie";34 /** The globally unique, unchanging identifier for this record. Assigned and managed by Gadget. */5 id: string;67 /** The time at which this record was first created. Set once upon record creation and never changed. Managed by Gadget. */8 createdAt: Date;910 /** The time at which this record was last changed. Set each time the record is successfully acted upon by an action. Managed by Gadget. */11 updatedAt: Date;1213 title: string;1415 embedding: number;1617 embeddingCosineSimilarityTo: number;1819 embeddingL2DistanceTo: number;2021 quote: string;2223 /** Get all the fields for this record. Useful for not having to list out all the fields you want to retrieve, but slower. */24 _all: Record<string, any>;25};
1type Movie {2 """3 The globally unique, unchanging identifier for this record. Assigned and managed by Gadget.4 """5 id: GadgetID!67 """8 The time at which this record was first created. Set once upon record creation and never changed. Managed by Gadget.9 """10 createdAt: DateTime!1112 """13 The time at which this record was last changed. Set each time the record is successfully acted upon by an action. Managed by Gadget.14 """15 updatedAt: DateTime!16 title: String!17 embedding: [Float!]18 embeddingCosineSimilarityTo(19 """20 Calculate the cosine similarity between this given vector and the vector in the embedding.21 """22 vector: [Float!]!23 ): Float24 embeddingL2DistanceTo(25 """26 Calculate the L2 distance between this given vector and the vector in the embedding.27 """28 vector: [Float!]!29 ): Float30 quote: String!3132 """33 Get all the fields for this record. Useful for not having to list out all the fields you want to retrieve, but slower.34 """35 _all: JSONObject!36}
You can preview what a real record's shape looks like by fetching it using the openai-screenwriter-tutorial-v2 API playground.
Any fetched movie record will have this same Movie
type, and expose the same data by default, regardless of if it's fetched by ID or as part of a findMany
. This means you can select any of the record's fields wherever you like in a GraphQL query according to the use case at hand.
Retrieving one movie record
Individual movie records can be retrieved using the "find by ID" API endpoint. You can also return only some fields, or
extra fields beyond what Gadget retrieves by default, using the select
option.
The findOne
function throws an error if no matching record is found, which you will need to catch and handle. Alternatively, you can use the maybeFindOne
function, which returns null
if no record is found, without throwing an error.
Similarly, the useFindOne
React hook returns (but does not throw) an error when no matching record is found, while the useMaybeFindOne
hook simply returns null
if no record is found, without also returning an error.
const movieRecord = await api.movie.findOne("123");console.log(movieRecord.id); //=> a stringconsole.log(movieRecord.createdAt); //=> a Date object
1const FindOneComponent = (props) => {2 const [{ data, error, fetching }, refresh] = useFindOne(api.movie, "123");34 console.log(data?.id); //=> a string5 console.log(data?.createdAt); //=> a Date object67 return <>{JSON.stringify(data)}</>;8};
1query GetOneMovie($id: GadgetID!) {2 movie(id: $id) {3 __typename4 id5 createdAt6 embedding7 quote8 title9 updatedAt10 }11}
{ "id": "123" }
Retrieving the first of many movie records
The first record from a list of records can be retrieved using the "find first" API endpoint. The source list of records can be filtered using the filter
option, sorted using the sort
option, searched using the search
option, though no pagination options are available on this endpoint. You can also return only some fields, or extra fields beyond what Gadget retrieves by default using the select
option.
The findFirst
function throws an error if no matching record is found, which you will need to catch and handle. Alternatively, you can use the maybeFindFirst
function, which returns null
if no record is found, without throwing an error.
Similarly, the useFindFirst
React hook returns (but does not throw) an error when no matching record is found, while the useMaybeFindFirst
hook simply returns null
if no record is found, without also returning an error.
const movieRecord = await api.movie.findFirst();console.log(movieRecord.id); //=> a stringconsole.log(movieRecord.createdAt); //=> a Date object
1const FindFirstComponent = (props) => {2 const [{ data, error, fetching }, refresh] = useFindFirst(api.movie);34 console.log(data?.id); //=> a string5 console.log(data?.createdAt); //=> a Date object67 return <>{JSON.stringify(data)}</>;8};
1query FindManyMovies(2 $first: Int3 $search: String4 $sort: [MovieSort!]5 $filter: [MovieFilter!]6) {7 movies(first: $first, search: $search, sort: $sort, filter: $filter) {8 edges {9 node {10 __typename11 id12 createdAt13 embedding14 quote15 title16 updatedAt17 }18 }19 }20}
{ "first": 1 }
Retrieving many movie records
Pages of movie records can be retrieved by using the "find many" API endpoint. The returned records can be filtered using the filter
option, sorted using the sort
option, searched using the search
option, and paginated using standard Relay-style pagination options. You can also return only some fields, or extra fields beyond what Gadget retrieves by default using the select
option.
This GraphQL endpoint returns records in the Relay Connection style (as a list of edge
s with node
s and cursor
s) so they can be paginated. The movies
GraphQL endpoint works with any Relay-compatible caching client, or you can use Gadget's JS client for pagination with the findMany
function.
Find a page of movies
You can fetch one page of records with the movie.findMany
JS method or the movies
GraphQL field. No options are required, and records can be sorted with the sort
option and filtered with the filter
optional. The records returned will be implicitly sorted by ID ascending, unless you pass a sort
option.
const movieRecords = await api.movie.findMany();console.log(movieRecords.length); //=> a numberconsole.log(movieRecords[0].id); //=> a string
1const FindManyComponent = (props) => {2 const [{ data, error, fetching }, refresh] = useFindMany(api.movie);3 if (!data) return null;45 console.log(data.length); //=> a number67 return <>{data.map((record) => JSON.stringify(record))}</>;8};
1query FindManyMovies {2 movies {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{}
Retrieving a single movie record by a uniquely identifiable field
After adding a unique validation to a field, you can retrieve a single record by using the finders generated below. If you would like to edit the fields returned or filtering, see the filtering section.
Retrieving a single movie record by id
Individual movie records can be retrieved using the "find many" API endpoint pre-filtered by the field's id. Throws if stored data is not unique.
const movieRecord = await api.movie.findById("some-value");console.log(movieRecord.id); //=> a string
1const FindByComponent = (props) => {2 const [{ data, error, fetching }, refresh] = useFindBy(3 api.movie.findById,4 "some-value"5 );67 console.log(data?.id); //=> a string89 return <>{JSON.stringify(data)}</>;10};
Sorting
Records can be sorted in the database to retrieve them in a certain order. Records are always implicitly sorted by ID ascending unless an explicit sort on the id
field is defined. The GraphQL type MovieSort
defines which fields can be sorted by.
Records can be sorted by multiple different fields and in multiple different directions by passing a list of MovieSort
instead of just one.
Pass the sort
option to the JS client, or the sort
variable to a GraphQL query to sort the records returned.
const movieRecords = await api.movie.findMany({ sort: { createdAt: "Descending" } });
const [result, refresh] = useFindMany(api.movie, {sort: { createdAt: "Descending" },});const { data, error, fetching } = result;
1query FindManyMovies($sort: [MovieSort!]) {2 movies(sort: $sort) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{ "sort": { "createdAt": "Descending" } }
Sort by multiple fields by passing an array of { [field]: "Ascending" | "Descending" }
objects.
const movieRecords = await api.movie.findMany({sort: [{ id: "Descending" }, { createdAt: "Ascending" }],});
const [result, refresh] = useFindMany(api.movie, {sort: [{ id: "Descending" }, { createdAt: "Ascending" }],});const { data, error, fetching } = result;
1query FindManyMovies($sort: [MovieSort!]) {2 movies(sort: $sort) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{ "sort": [{ "id": "Descending" }, { "createdAt": "Ascending" }] }
Available fields for sorting
You can sort movie
records by the following fields:
Field | Sort type |
---|---|
id | Ascending / Descending |
title | Ascending / Descending |
quote | Ascending / Descending |
embedding | Ascending / Descending |
createdAt | Ascending / Descending |
updatedAt | Ascending / Descending |
For example, you can sort by the id
field:
const movieRecords = await api.movie.findMany({sort: { id: "Descending" },});
const [result, refresh] = useFindMany(api.movie, {sort: { id: "Descending" },});const { data, error, fetching } = result;
1query FindManyMovies($sort: [MovieSort!]) {2 movies(sort: $sort) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{ "sort": { "id": "Descending" } }
Read more about sorting in the Sorting and Filtering reference.
Searching
movie records can be searched using Gadget's built in full text search functionality. Gadget search is appropriate
for powering autocompletes, searchable tables, or other experiences where humans are writing search queries. It's typo tolerant, synonym aware
and supports simple search operators like !
to exclude search terms.
Search Movies by passing the search
parameter with a search query string.
Search isn't field specific in Gadget -- all String or RichText field types are searched with the built in search functionality.
const movieRecords = await api.movie.findMany({search: "a specific phrase to search for",});
const [result, refresh] = useFindMany(api.movie, {search: "a specific phrase to search for",});const { data, error, fetching } = result;
1query FindManyMovies($search: String) {2 movies(search: $search) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{ "search": "a specific phrase to search for" }
Filtering
movie records can be filtered to return only the appropriate records. Records can be filtered on any field, including those managed by Gadget or fields added by developers. Filters can be combined with sorts, searches and paginated using cursor-based Relay pagination.
Filter Movies by passing the filter
parameter with a filter object. Filter objects are nestable boolean conditions expressed as JS objects capturing a key, an operator, and usually a value.
const yesterday = new Date(Date.now() - 864e5);const movieRecords = await api.movie.findMany({filter: { createdAt: { greaterThan: yesterday } },});
const yesterday = new Date(Date.now() - 864e5);const [result, refresh] = useFindMany(api.movie, {filter: { createdAt: { greaterThan: yesterday } },});const { data, error, fetching } = result;
1query FindManyMovies($filter: [MovieFilter!]) {2 movies(filter: $filter) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{ "filter": { "createdAt": { "greaterThan": "2024-09-07T02:02:40.843Z" } } }
Available fields for filtering
The GraphQL type MovieFilter
defines which fields can be filtered on.
Field | Filter type |
---|---|
id | IDFilter |
title | StringFilter |
quote | StringFilter |
embedding | VectorFilter |
createdAt | DateTimeFilter |
updatedAt | DateTimeFilter |
For example, we can filter movie
records on the id field:
const movieRecords = await api.movie.findMany({filter: {id: { isSet: true },},});
1const [result, refresh] = useFindMany(api.movie, {2 filter: {3 id: { isSet: true },4 },5});6const { data, error, fetching } = result;
1query FindManyMovies($filter: [MovieFilter!]) {2 movies(filter: $filter) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
{ "filter": { "id": { "isSet": true } } }
Read more about the available filters in the Sorting and Filtering reference.
Combining filters
Records can be filtered by multiple different fields simultaneously. If you want to combine filters using boolean logic, nest them under the AND
, OR
, or NOT
keys of a parent filter. Filters can be nested deeply by passing multiple levels boolean condition filters.
You can also pass a list of filters to the filter
parameter which will be implicitly AND
ed with one another such that they all need to match for a record to be returned.
1const yesterday = new Date(Date.now() - 86400000);2const oneWeekAgo = new Date(Date.now() - 604800000);3const movieRecords = await api.movie.findMany({4 filter: {5 OR: [6 {7 createdAt: { greaterThan: oneWeekAgo },8 },9 {10 updatedAt: { greaterThan: yesterday },11 },12 ],13 },14});
1const yesterday = new Date(Date.now() - 86400000);2const oneWeekAgo = new Date(Date.now() - 604800000);3const [result, refresh] = useFindMany(api.movie, {4 filter: {5 OR: [6 {7 createdAt: { greaterThan: oneWeekAgo },8 },9 {10 updatedAt: { greaterThan: yesterday },11 },12 ],13 },14});15const { data, error, fetching } = result;
1query FindManyMovies($filter: [MovieFilter!]) {2 movies(filter: $filter) {3 edges {4 node {5 __typename6 id78 # ...910 createdAt11 title12 updatedAt13 }14 }15 }16}
1{2 "filter": {3 "OR": [4 { "createdAt": { "greaterThan": "2024-09-01T02:02:40.844Z" } },5 { "updatedAt": { "greaterThan": "2024-09-07T02:02:40.844Z" } }6 ]7 }8}
Pagination
All Gadget record lists, including the top level movie finder as well as associations to movie, are structured as GraphQL connections. GraphQL connections are the de facto standard for querying lists and support cursor-based forward and backward pagination. When querying via GraphQL, you must select the edges
field and then the node
field to get the movie record. When querying using a Gadget API client, the GraphQL queries are generated for you and the records are unwrapped and returned as a GadgetRecordList
ready for use.
movie pagination supports the standard GraphQL connection pagination arguments: first
+ after
, or last
+ before
.
Pagination is done using cursors, which you can retrieve from the edge.cursor
field or the pageInfo.startCursor
properties.
const movieRecords = await api.movie.findMany({ first: 25 });console.log(movieRecords.length); //=> no greater than 25
1const FirstPage = (props) => {2 const [{ data, error, fetching }, refresh] = useFindMany(api.movie, { first: 25 });3 if (!data) return null;45 console.log(data.length); //=> no greater than 2567 return <>{data.map((record) => JSON.stringify(record))}</>;8};
1query FindManyMovies($first: Int, $after: String) {2 movies(first: $first, after: $after) {3 pageInfo {4 hasNextPage5 hasPreviousPage6 startCursor7 endCursor8 }9 edges {10 cursor11 node {12 __typename13 id1415 # ...1617 createdAt18 title19 updatedAt20 }21 }22 }23}
{ "first": 25 }
const movieRecords = await api.movie.findMany({after: "example-cursor",first: 25,});
1const SecondPage = (props) => {2 const [{ data, error, fetching }, refresh] = useFindMany(api.movie, {3 after: "example-cursor",4 first: 25,5 });6 if (!data) return null;78 console.log(data.length); //=> no greater than 25910 return <>{data.map((record) => JSON.stringify(record))}</>;11};
1query FindManyMovies($first: Int, $after: String) {2 movies(first: $first, after: $after) {3 pageInfo {4 hasNextPage5 hasPreviousPage6 startCursor7 endCursor8 }9 edges {10 cursor11 node {12 __typename13 id1415 # ...1617 createdAt18 title19 updatedAt20 }21 }22 }23}
{ "first": 25, "after": "abcdefg" }
Pagination Limits
Root-level record finders like movies
support a maximum page size of 250 records and a default page size of 50 records. The page size is controlled using the first
or last
GraphQL field arguments.
Related record finders that access lists of records through a has many or has many through field support a maximum page size of 100 records and a default page size of 50 records.
Get the next or previous page
When using the generated JavaScript API client, including the api
parameter in a Gadget code effect, the record lists returned from findMany calls can be paginated using the nextPage()
or previousPage()
option.
Both nextPage()
and previousPage()
will throw an error if the corresponding hasNextPage
or hasPreviousPage
is false
.
1const movieRecords = await api.movie.findMany();2if (movieRecords.hasNextPage) {3 const nextPage = await movieRecords.nextPage();4}5if (movieRecords.hasPreviousPage) {6 const prevPage = await movieRecords.previousPage();7}
When using React and paging through records, you can use cursors to get the previous or next pages of records. This is an example of a React component that pages forward and backward through 2 records at a time for movie.
1import { api } from "../api"; // your Gadget project's API Client2import { useFindMany } from "@gadgetinc/react";3import { useCallback, useState } from "react";45export default function TestComponent() {6 const NUM_ON_PAGE = 2; // the number of records per page78 const [cursor, setCursor] = useState({ first: NUM_ON_PAGE });9 // using Gadget React hooks to fetch records of movie10 const [{ data, fetching, error }] = useFindMany(api.movie, {11 ...cursor,12 });1314 const getNextPage = useCallback(() => {15 // use first + after to page forwards16 setCursor({ first: NUM_ON_PAGE, after: data.endCursor });17 }, [data]);1819 const getPreviousPage = useCallback(() => {20 // use last + before to page backwards21 setCursor({ last: NUM_ON_PAGE, before: data.startCursor });22 }, [data]);2324 return (25 <div>26 <button onClick={getPreviousPage} disabled={!data?.hasPreviousPage}>27 Previous page28 </button>29 <button onClick={getNextPage} disabled={!data?.hasNextPage}>30 Next page31 </button>32 {!fetching && data.map((d) => <div>{d.id}</div>)}33 </div>34 );35}
Get all records
If you need to get all available data for movie, you will need to paginate through all pages of data. If you have a large amount of data, this can take a long time. Make sure you need to collect all data at once before writing a pagination loop that reads all records! If you are querying records for display in a UI and cannot display all your records at once, we don't recommend fetching all the data beforehand - instead, use the cursor to read additional data when the user needs it.
If you need all data for analytics applications or to collect some statistics on your data, consider options like intermediate models and pre-defined data rollups.
If you have determined that you need all your data, you can fetch it using cursors and a loop. We also suggest using select
so that you only grab fields that are needed, in addition to applying a filter
, if possible. Using first
with the maximum allowable value will also allow you to grab the maximum number of records you can at once.
1const allRecords = []; // use allRecords to store all records2let records = await api.movie.findMany({3 first: 250,4 select: {5 id: true,6 },7 filter: {8 // add filter conditions, if possible9 },10});1112allRecords.push(...records);1314// loop through additional pages to get all protected orders15while (records.hasNextPage) {16 // paginate17 records = await records.nextPage();18 allRecords.push(...records);19}
Selecting fields, and fields of fields
When using the JavaScript client, all of findOne
, maybeFindOne
, findMany
, findFirst
, maybeFindFirst
, and various action functions, allow requesting specific fields of a movie and its relationships. The select
option controls which fields are selected in the generated GraphQL query sent to the Gadget API. Pass each field you want to select in an object, with true
as the value for scalar fields, and a nested object of the same shape for nested fields.
Gadget has a default selection that will retrieve all of the scalar fields for a movie. If you don't pass a select
option to a record finder, this default selection will be used.
1// fetch only the id and createdAt field2const movieRecords = await api.movie.findMany({3 select: { id: true, createdAt: true },4});5// fetch all the scalar fields for the model, but no relationship fields6const movieRecords = await api.movie.findMany();
1const SelectionComponent = (props) => {2 // fetch only the id and createdAt field3 const [{ data, error, fetching }, refresh] = useFindMany(api.movie, {4 select: { id: true, createdAt: true },5 });6 // ...7};
Realtime subscriptions with live
queries
Each read function in the movie runs once and returns a static set of results. There is also a second mode for subscribing to the results of the query as they change over time.
By passing live: true
to any read query or hook, you'll get a stream of results as data changes in the backend, instead of just one result. This is useful for automatically updating your UI when any record is created, updated, or deleted in your backend database.
Realtime queries are issued by passing live: true
to your API call or React hook. For example, you can query and stream changes to the record with id 123
:
// calling findOne with live: true will return a stream of results as an AsyncIterator, not just one resultfor await (const record of api.movie.findOne("123", { live: true })) {// record will be a GadgetRecord objectconsole.log("new record version", record);}
1export const Record123 = (props) => {2 const [{ data, error, fetching }] = useFindOne(3 api.movie,4 "123", // the id of the record to fetch5 {6 // make this a realtime live query7 live: true,8 }9 );10 if (!data) return null;11 return <div>Record 123 last updated at {data.updatedAt}</div>;12};
You can also subscribe to realtime results for pages of records. For example, we can fetch and render a list of the first ten movie records, which will automatically update when backend data changes:
// calling findMany with live: true will return a stream of results as an AsyncIterator, not just one resultfor await (const list of api.movie.findMany({ live: true, first: 10 })) {// list will be an array of GadgetRecord objectsconsole.log("new query result", list);}
1export const FirstPage = (props) => {2 const [{ data, error, fetching }] = useFindMany(api.movie, {3 // make this a realtime live query4 live: true,5 first: 10,6 });7 if (!data) return null;8 return (9 <div>10 {data.map((record) => (11 <div key={record.id}>{record.id}</div>12 ))}13 </div>14 );15};
For more information, see the Realtime Queries guide.
Type Safety
The select
option is fully type-safe if you're using TypeScript. The returned GadgetRecord
type will have a <Shape>
exactly matching the fields and nested fields you selected.
This behavior of selecting only some fields is built right into GraphQL. If you want to limit or expand what you retrieve from a GraphQL query, include or exclude those fields in your GraphQL query. For more information on executing GraphQL queries, see GraphQL.
1// fetch the id and createdAt field, and fetch some nested fields from an example relationship field named `someRelatedObject`2const movieRecords = await api.movie.findMany({3 select: {4 id: true,5 createdAt: true,6 someRelatedObject: { id: true, createdAt: true },7 },8});
1const SelectionComponent = (props) => {2 // fetch the id and createdAt field, and fetch some nested fields from an example relationship field named `someRelatedObject`3 const [{ data, error, fetching }, refresh] = useFindMany(api.movie, {4 select: {5 id: true,6 createdAt: true,7 someRelatedObject: { id: true, createdAt: true },8 },9 });10 // ...11};
Combining parameters
Sort, search, filtering, selection, and pagination parameters can be combined to access the exact set of records needed for your use case.
1const movieRecords = await api.movie.findMany({2 search: "<some search query>",3 sort: { createdAt: "Descending" },4 filter: { updatedAt: { greaterThan: new Date(Date.now() - 864e5) } },5 select: { id: true, createdAt: true },6 first: 25,7 after: "abcdefg",8});
1const [result, refresh] = useFindMany(api.movie, {2 search: "<some search query>",3 sort: { createdAt: "Descending" },4 filter: { updatedAt: { greaterThan: new Date(Date.now() - 864e5) } },5 select: { id: true, createdAt: true },6 first: 25,7 after: "abcdefg",8});9const { data, error, fetching } = result;
1query FindManyMovies(2 $after: String3 $before: String4 $first: Int5 $last: Int6 $search: String7 $sort: [MovieSort!]8 $filter: [MovieFilter!]9) {10 movies(11 after: $after12 before: $before13 first: $first14 last: $last15 search: $search16 sort: $sort17 filter: $filter18 ) {19 pageInfo {20 hasNextPage21 hasPreviousPage22 startCursor23 endCursor24 }25 edges {26 cursor27 node {28 __typename29 id30 createdAt31 embedding32 quote33 title34 updatedAt35 }36 }37 }38}
1{2 "search": "<some search query>",3 "sort": { "createdAt": "Descending" },4 "filter": { "updatedAt": { "greaterThan": "2024-09-07T02:02:40.964Z" } },5 "first": 25,6 "after": "abcdefg"7}
Invoking Actions
movie records are changed by invoking Actions. Actions are the things that "do" stuff -- update records, make API calls, call backend code, etc. Actions with a GraphQL API trigger each have one corresponding GraphQL mutation and a corresponding function available in the API client libraries. Nested Actions can also be invoked with the API client, by providing the actions as input to any relationship fields.
Action Result format
Each API action returns results in the same format that includes a success indicator, errors, and the actual result if the action succeeded. The result is the record that was acted on for a model action, or a list of records for a bulk action, or a JSON blob for Global Actions. Model actions that delete the record don't return the record.
The success
field returns a boolean indicating if the action executed as expected. Any execution errors are returned in the errors
object, which will always be null
if success
is true or contain ExecutionError
objects if success
is false.
ExecutionError
objects always have a message
describing what error prevented the action from succeeding, as well as a code
attribute that gives a stable, searchable, human-readable error class code for referencing this specific error. Details on each error code can be found in the Errors documentation. All ExecutionError
object types returned by the GraphQL object can be one of many types of error, where some types have extra data that is useful for remedying the error. All error types will always have message
and code
properties, but some, like InvalidRecordError
have extra fields for use by clients.
Errors when using the generated client
The generated JavaScript client automatically interprets errors from invoking actions and throws JavaScript Error
instances if the action didn't succeed. The Error
objects it throws are rich, and expose extra error properties beyond just message
and code
if they exist.
Errors thrown by the JavaScript client are easiest to catch by using a try/catch
statement around an await
, like so:
JavaScript1import {2 GadgetOperationError,3 InvalidRecordError,4} from "@gadgetinc/api-client-core";56// must be in an async function to use \`await\` syntax7export async function run({ api }) {8 try {9 return await api.exampleModel.create({ name: "example record name" });10 } catch (error) {11 if (error instanceof GadgetOperationError) {12 // a recognized general error has occurred, retry the operation or inspect \error.code\`13 console.error(error);14 } else if (error instanceof InvalidRecordError) {15 // the submitted input data for the action was invalid, inspect the invalid fields which \`InvalidRecordError\` exposes16 console.error(error.validationErrors);17 } else {18 // an unrecognized error occurred like an HTTP connection interrupted error or a syntax error. Re-throw it because it's not clear what to do to fix it19 throw error;20 }21 }22}
For more information on error codes, consult the Errors documentation.
movie create
Input
create
accepts the following input parameters:
1export type CreateMovieInput = {2 title?: (Scalars["String"] | null) | null;34 embedding?: (Scalars["Float"] | null)[];56 quote?: (Scalars["String"] | null) | null;7};89export type CreateMovieArguments = {10 movie?: CreateMovieInput | null;11};
1input CreateMovieInput {2 title: String3 embedding: [Float!]4 quote: String5}67input CreateMovieArguments {8 movie: CreateMovieInput9}
const movieRecord = await api.movie.create({embedding: [0.1, 0.2, 0.3],quote: "example value for quote",title: "example value for title",});
1const ExampleRunCreateComponent = (props) => {2 const [{ data, error, fetching }, create] = useAction(api.movie.create);34 return (5 <>6 <button7 onClick={async () => {8 await create({9 embedding: [0.1, 0.2, 0.3],10 quote: "example value for quote",11 title: "example value for title",12 });13 }}14 >15 Run Action16 </button>17 Result: {JSON.stringify(data)}18 </>19 );20};
1mutation CreateMovie($movie: CreateMovieInput) {2 createMovie(movie: $movie) {3 success4 errors {5 message6 ... on InvalidRecordError {7 validationErrors {8 apiIdentifier9 message10 }11 record12 model {13 apiIdentifier14 }15 }16 }17 movie {18 __typename19 id20 createdAt21 embedding22 quote23 title24 updatedAt25 }26 }27}
1{2 "movie": {3 "embedding": [0.1, 0.2, 0.3],4 "quote": "example value for quote",5 "title": "example value for title"6 }7}
Output
create
returns the movie. In the JS client, the fields returned can be controlled with the select
option. In GraphQL, the return format is the action result format, which includes the record if the action was successful. You can include or exclude the fields you need right in the mutation itself.
1type CreateMovieResult {2 success: Boolean!3 errors: [ExecutionError!]4 actionRun: String5 movie: Movie6}
movie update
Input
update
operates on one movie in particular, identified by the id
variable.update
accepts the following input parameters:
1export type UpdateMovieInput = {2 title?: (Scalars["String"] | null) | null;34 embedding?: (Scalars["Float"] | null)[];56 quote?: (Scalars["String"] | null) | null;7};89export type UpdateMovieArguments = {10 movie?: UpdateMovieInput | null;11};
1input UpdateMovieInput {2 title: String3 embedding: [Float!]4 quote: String5}67input UpdateMovieArguments {8 movie: UpdateMovieInput9}
const movieRecord = await api.movie.update("123", {embedding: [0.1, 0.2, 0.3],quote: "example value for quote",title: "example value for title",});
1const ExampleRunUpdateComponent = (props) => {2 const [{ data, error, fetching }, update] = useAction(api.movie.update);34 return (5 <>6 <button7 onClick={async () => {8 await update({9 embedding: [0.1, 0.2, 0.3],10 id: "123",11 quote: "example value for quote",12 title: "example value for title",13 });14 }}15 >16 Run Action17 </button>18 Result: {JSON.stringify(data)}19 </>20 );21};
1mutation UpdateMovie($movie: UpdateMovieInput, $id: GadgetID!) {2 updateMovie(movie: $movie, id: $id) {3 success4 errors {5 message6 ... on InvalidRecordError {7 validationErrors {8 apiIdentifier9 message10 }11 record12 model {13 apiIdentifier14 }15 }16 }17 movie {18 __typename19 id20 createdAt21 embedding22 quote23 title24 updatedAt25 }26 }27}
1{2 "movie": {3 "embedding": [0.1, 0.2, 0.3],4 "quote": "example value for quote",5 "title": "example value for title"6 },7 "id": "123"8}
Output
update
returns the movie. In the JS client, the fields returned can be controlled with the select
option. In GraphQL, the return format is the action result format, which includes the record if the action was successful. You can include or exclude the fields you need right in the mutation itself.
1type UpdateMovieResult {2 success: Boolean!3 errors: [ExecutionError!]4 actionRun: String5 movie: Movie6}
movie delete
The delete
action destroys the record.
Input
delete
operates on one movie in particular, identified by the id
variable.
await api.movie.delete("123");
1const ExampleRunDeleteComponent = (props) => {2 const [{ data, error, fetching }, _delete] = useAction(api.movie.delete);34 return (5 <>6 <button7 onClick={async () => {8 await _delete({9 id: "123",10 });11 }}12 >13 Run Action14 </button>15 Result: {JSON.stringify(data)}16 </>17 );18};
1mutation DeleteMovie($id: GadgetID!) {2 deleteMovie(id: $id) {3 success4 errors {5 message6 ... on InvalidRecordError {7 validationErrors {8 apiIdentifier9 message10 }11 record12 model {13 apiIdentifier14 }15 }16 }17 }18}
{ "id": "123" }
Output
delete
deletes the record, so it returns void
in the JS client. In GraphQL it returns only the success
and errors
from the action result format.
type DeleteMovieResult {success: Boolean!errors: [ExecutionError!]actionRun: String}
Bulk Actions
You can run the same action for an array of inputs all at once with bulk actions. Bulk Actions are executed as a single API call and offer better performance when running the same action on many records.
Creates, updates, deletes, and custom actions can all be run in bulk. Bulk Actions repeat the same action each time across a variety of different records. If you want to call different actions, you can't use Bulk Actions and must instead make multiple API calls.
If a bulk action group fails on some of the individual records, the remaining records will still be processed, and the list of errors will be returned in the result. Only the records which succeed in executing the action will be returned in the result.
Bulk movie create
Input
bulkCreateMovies
operates on a set of movies, identified by the ids
variable.
1const movieRecords = await api.movie.bulkCreate([2 {3 quote: "example value for quote",4 title: "example value for title",5 },6 {7 quote: "example value for quote",8 title: "example value for title",9 },10]);
1const RunActionComponent = (props) => {2 const [{ data, error, fetching }, bulkCreate] = useBulkAction(3 api.movie.bulkCreate4 );56 return (7 <>8 <button9 onClick={async () => {10 await bulkCreate([11 {12 quote: "example value for quote",13 title: "example value for title",14 },15 {16 quote: "example value for quote",17 title: "example value for title",18 },19 ]);20 console.log(data?.length); //=> a number21 console.log(data?.[0].id); //=> a string22 }}23 >24 Run Action25 </button>26 Result: {JSON.stringify(data)}27 </>28 );29};
1mutation BulkCreateMovies($inputs: [BulkCreateMoviesInput!]!) {2 bulkCreateMovies(inputs: $inputs) {3 success4 errors {5 message6 }7 movies {8 id9 createdAt10 embedding11 quote12 title13 updatedAt14 }15 }16}
1{2 "inputs": [3 {4 "movie": {5 "quote": "example value for quote",6 "title": "example value for title"7 }8 },9 {10 "movie": {11 "quote": "example value for quote",12 "title": "example value for title"13 }14 }15 ]16}
Output
bulkCreateMovies
returns the set of movies that successfully completed the action. In the JS client, the fields returned for the record can be controlled with the select
option. In GraphQL, the response is in the action result format, which includes a selection that completed the action successfully.The success
property will be false
if any of the records failed to perform their action and the returned set of movies
will only include the ones that completed successfully.
1"""2The output when running the create on the movie model in bulk.3"""4type BulkCreateMoviesResult {5 """6 Boolean describing if all the bulk actions succeeded or not7 """8 success: Boolean!910 """11 Aggregated list of errors that any bulk action encountered while processing12 """13 errors: [ExecutionError!]1415 """16 The list of all changed movie records by each sent bulk action. Returned in the same order as the input bulk action params.17 """18 movies: [Movie]19}
Bulk movie update
Input
bulkUpdateMovies
operates on a set of movies, identified by the ids
variable.
1const movieRecords = await api.movie.bulkUpdate([2 {3 embedding: [0.1, 0.2, 0.3],4 id: "123",5 title: "example value for title",6 },7 {8 embedding: [0.1, 0.2, 0.3],9 id: "456",10 title: "example value for title",11 },12]);
1const RunActionComponent = (props) => {2 const [{ data, error, fetching }, bulkUpdate] = useBulkAction(3 api.movie.bulkUpdate4 );56 return (7 <>8 <button9 onClick={async () => {10 await bulkUpdate([11 {12 embedding: [0.1, 0.2, 0.3],13 id: "123",14 title: "example value for title",15 },16 {17 embedding: [0.1, 0.2, 0.3],18 id: "456",19 title: "example value for title",20 },21 ]);22 console.log(data?.length); //=> a number23 console.log(data?.[0].id); //=> a string24 }}25 >26 Run Action27 </button>28 Result: {JSON.stringify(data)}29 </>30 );31};
1mutation BulkUpdateMovies($inputs: [BulkUpdateMoviesInput!]!) {2 bulkUpdateMovies(inputs: $inputs) {3 success4 errors {5 message6 }7 movies {8 id9 createdAt10 embedding11 quote12 title13 updatedAt14 }15 }16}
1{2 "inputs": [3 {4 "movie": { "embedding": [0.1, 0.2, 0.3], "title": "example value for title" },5 "id": "123"6 },7 {8 "movie": { "embedding": [0.1, 0.2, 0.3], "title": "example value for title" },9 "id": "456"10 }11 ]12}
Output
bulkUpdateMovies
returns the set of movies that successfully completed the action. In the JS client, the fields returned for the record can be controlled with the select
option. In GraphQL, the response is in the action result format, which includes a selection that completed the action successfully.The success
property will be false
if any of the records failed to perform their action and the returned set of movies
will only include the ones that completed successfully.
1"""2The output when running the update on the movie model in bulk.3"""4type BulkUpdateMoviesResult {5 """6 Boolean describing if all the bulk actions succeeded or not7 """8 success: Boolean!910 """11 Aggregated list of errors that any bulk action encountered while processing12 """13 errors: [ExecutionError!]1415 """16 The list of all changed movie records by each sent bulk action. Returned in the same order as the input bulk action params.17 """18 movies: [Movie]19}
Bulk movie delete
bulkDeleteMovies
action destroys the records.
Input
bulkDeleteMovies
operates on a set of movies, identified by the ids
variable.
await api.movie.bulkDelete(["123", "456"]);
1const RunActionComponent = (props) => {2 const [{ data, error, fetching }, bulkDelete] = useBulkAction(3 api.movie.bulkDelete4 );56 return (7 <>8 <button9 onClick={async () => {10 await bulkDelete({11 ids: ["123", "456"],12 });13 }}14 >15 Run Action16 </button>17 Result: {JSON.stringify(data)}18 </>19 );20};
1mutation BulkDeleteMovies($ids: [GadgetID!]!) {2 bulkDeleteMovies(ids: $ids) {3 success4 errors {5 message6 }7 }8}
{ "ids": ["123", "456"] }
Output
bulkDeleteMovies
deletes the record, so it returns void
in the JS client. In GraphQL it returns only the success
and errors
from the action result format.
1"""2The output when running the delete on the movie model in bulk.3"""4type BulkDeleteMoviesResult {5 """6 Boolean describing if all the bulk actions succeeded or not7 """8 success: Boolean!910 """11 Aggregated list of errors that any bulk action encountered while processing12 """13 errors: [ExecutionError!]14}