e2e/data-connect/dataconnect-generated/js/default-connector/README.md
This README will guide you through the process of using the generated JavaScript SDK package for the connector default. It will also provide examples on how to use your generated SDK to call your Data Connect queries and mutations.
NOTE: This README is generated alongside the generated SDK. If you make changes to this file, they will be overwritten when the SDK is regenerated.
A connector is a collection of Queries and Mutations. One SDK is generated for each connector - this SDK is generated for the connector default. You can find more information about connectors in the Data Connect documentation.
You can use this generated SDK by importing from the package @firebasegen/default-connector as shown below. Both CommonJS and ESM imports are supported.
You can also follow the instructions from the Data Connect documentation.
import { getDataConnect } from 'firebase/data-connect';
import { connectorConfig } from '@firebasegen/default-connector';
const dataConnect = getDataConnect(connectorConfig);
By default, the connector will connect to the production service.
To connect to the emulator, you can use the following code. You can also follow the emulator instructions from the Data Connect documentation.
import { connectDataConnectEmulator, getDataConnect } from 'firebase/data-connect';
import { connectorConfig } from '@firebasegen/default-connector';
const dataConnect = getDataConnect(connectorConfig);
connectDataConnectEmulator(dataConnect, 'localhost', 9399);
After it's initialized, you can call your Data Connect queries and mutations from your generated SDK.
There are two ways to execute a Data Connect Query using the generated Web SDK:
QueryRef
QueryRef can be used as an argument to executeQuery(), which will execute the Query and return a QueryPromiseQueryPromise
QueryPromiseThe following is true for both the action shortcut function and the QueryRef function:
QueryPromise returned will resolve to the result of the Query once it has finished executingQueryRef function accept a single argument: an object that contains all the required variables (and the optional variables) for the QueryDataConnect instance as an argument. If no DataConnect argument is passed in, then the generated SDK will call getDataConnect(connectorConfig) behind the scenes for you.Below are examples of how to use the default connector's generated functions to execute each query. You can also follow the examples from the Data Connect documentation.
You can execute the ListMovies query using the following action shortcut function, or by calling executeQuery() after calling the following QueryRef function, both of which are defined in default-connector/index.d.ts:
listMovies(): QueryPromise<ListMoviesData, undefined>;
interface ListMoviesRef {
...
/* Allow users to create refs without passing in DataConnect */
(): QueryRef<ListMoviesData, undefined>;
}
export const listMoviesRef: ListMoviesRef;
You can also pass in a DataConnect instance to the action shortcut function or QueryRef function.
listMovies(dc: DataConnect): QueryPromise<ListMoviesData, undefined>;
interface ListMoviesRef {
...
(dc: DataConnect): QueryRef<ListMoviesData, undefined>;
}
export const listMoviesRef: ListMoviesRef;
If you need the name of the operation without creating a ref, you can retrieve the operation name by calling the operationName property on the listMoviesRef:
const name = listMoviesRef.operationName;
console.log(name);
The ListMovies query has no variables.
Recall that executing the ListMovies query returns a QueryPromise that resolves to an object with a data property.
The data property is an object of type ListMoviesData, which is defined in default-connector/index.d.ts. It has the following fields:
export interface ListMoviesData {
movies: ({
id: UUIDString;
title: string;
imageUrl: string;
genre?: string | null;
} & Movie_Key)[];
}
ListMovies's action shortcut functionimport { getDataConnect } from 'firebase/data-connect';
import { connectorConfig, listMovies } from '@firebasegen/default-connector';
// Call the `listMovies()` function to execute the query.
// You can use the `await` keyword to wait for the promise to resolve.
const { data } = await listMovies();
// You can also pass in a `DataConnect` instance to the action shortcut function.
const dataConnect = getDataConnect(connectorConfig);
const { data } = await listMovies(dataConnect);
console.log(data.movies);
// Or, you can use the `Promise` API.
listMovies().then((response) => {
const data = response.data;
console.log(data.movies);
});
ListMovies's QueryRef functionimport { getDataConnect, executeQuery } from 'firebase/data-connect';
import { connectorConfig, listMoviesRef } from '@firebasegen/default-connector';
// Call the `listMoviesRef()` function to get a reference to the query.
const ref = listMoviesRef();
// You can also pass in a `DataConnect` instance to the `QueryRef` function.
const dataConnect = getDataConnect(connectorConfig);
const ref = listMoviesRef(dataConnect);
// Call `executeQuery()` on the reference to execute the query.
// You can use the `await` keyword to wait for the promise to resolve.
const { data } = await executeQuery(ref);
console.log(data.movies);
// Or, you can use the `Promise` API.
executeQuery(ref).then((response) => {
const data = response.data;
console.log(data.movies);
});
There are two ways to execute a Data Connect Mutation using the generated Web SDK:
MutationRef
MutationRef can be used as an argument to executeMutation(), which will execute the Mutation and return a MutationPromiseMutationPromise
MutationPromiseThe following is true for both the action shortcut function and the MutationRef function:
MutationPromise returned will resolve to the result of the Mutation once it has finished executingMutationRef function accept a single argument: an object that contains all the required variables (and the optional variables) for the MutationDataConnect instance as an argument. If no DataConnect argument is passed in, then the generated SDK will call getDataConnect(connectorConfig) behind the scenes for you.Below are examples of how to use the default connector's generated functions to execute each mutation. You can also follow the examples from the Data Connect documentation.
You can execute the CreateMovie mutation using the following action shortcut function, or by calling executeMutation() after calling the following MutationRef function, both of which are defined in default-connector/index.d.ts:
createMovie(vars: CreateMovieVariables): MutationPromise<CreateMovieData, CreateMovieVariables>;
interface CreateMovieRef {
...
/* Allow users to create refs without passing in DataConnect */
(vars: CreateMovieVariables): MutationRef<CreateMovieData, CreateMovieVariables>;
}
export const createMovieRef: CreateMovieRef;
You can also pass in a DataConnect instance to the action shortcut function or MutationRef function.
createMovie(dc: DataConnect, vars: CreateMovieVariables): MutationPromise<CreateMovieData, CreateMovieVariables>;
interface CreateMovieRef {
...
(dc: DataConnect, vars: CreateMovieVariables): MutationRef<CreateMovieData, CreateMovieVariables>;
}
export const createMovieRef: CreateMovieRef;
If you need the name of the operation without creating a ref, you can retrieve the operation name by calling the operationName property on the createMovieRef:
const name = createMovieRef.operationName;
console.log(name);
The CreateMovie mutation requires an argument of type CreateMovieVariables, which is defined in default-connector/index.d.ts. It has the following fields:
export interface CreateMovieVariables {
title: string;
genre: string;
imageUrl: string;
}
Recall that executing the CreateMovie mutation returns a MutationPromise that resolves to an object with a data property.
The data property is an object of type CreateMovieData, which is defined in default-connector/index.d.ts. It has the following fields:
export interface CreateMovieData {
movie_insert: Movie_Key;
}
CreateMovie's action shortcut functionimport { getDataConnect } from 'firebase/data-connect';
import { connectorConfig, createMovie, CreateMovieVariables } from '@firebasegen/default-connector';
// The `CreateMovie` mutation requires an argument of type `CreateMovieVariables`:
const createMovieVars: CreateMovieVariables = {
title: ...,
genre: ...,
imageUrl: ...,
};
// Call the `createMovie()` function to execute the mutation.
// You can use the `await` keyword to wait for the promise to resolve.
const { data } = await createMovie(createMovieVars);
// Variables can be defined inline as well.
const { data } = await createMovie({ title: ..., genre: ..., imageUrl: ..., });
// You can also pass in a `DataConnect` instance to the action shortcut function.
const dataConnect = getDataConnect(connectorConfig);
const { data } = await createMovie(dataConnect, createMovieVars);
console.log(data.movie_insert);
// Or, you can use the `Promise` API.
createMovie(createMovieVars).then((response) => {
const data = response.data;
console.log(data.movie_insert);
});
CreateMovie's MutationRef functionimport { getDataConnect, executeMutation } from 'firebase/data-connect';
import { connectorConfig, createMovieRef, CreateMovieVariables } from '@firebasegen/default-connector';
// The `CreateMovie` mutation requires an argument of type `CreateMovieVariables`:
const createMovieVars: CreateMovieVariables = {
title: ...,
genre: ...,
imageUrl: ...,
};
// Call the `createMovieRef()` function to get a reference to the mutation.
const ref = createMovieRef(createMovieVars);
// Variables can be defined inline as well.
const ref = createMovieRef({ title: ..., genre: ..., imageUrl: ..., });
// You can also pass in a `DataConnect` instance to the `MutationRef` function.
const dataConnect = getDataConnect(connectorConfig);
const ref = createMovieRef(dataConnect, createMovieVars);
// Call `executeMutation()` on the reference to execute the mutation.
// You can use the `await` keyword to wait for the promise to resolve.
const { data } = await executeMutation(ref);
console.log(data.movie_insert);
// Or, you can use the `Promise` API.
executeMutation(ref).then((response) => {
const data = response.data;
console.log(data.movie_insert);
});