Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 66 additions & 44 deletions dataconnect-sdk/js/default-connector/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
# Generated TypeScript README
# Table of Contents
- [**Overview**](#generated-typescript-readme)
- [**Accessing the connector**](#accessing-the-connector)
- [*Connecting to the local Emulator*](#connecting-to-the-local-emulator)
- [**Queries**](#queries)
- [*ListMovies*](#listmovies)
- [*GetMovieById*](#getmoviebyid)
- [**Mutations**](#mutations)
- [*CreateMovie*](#createmovie)
- [*UpsertMovie*](#upsertmovie)
- [*DeleteMovie*](#deletemovie)

# Generated TypeScript README
This README will guide you through the process of using the generated TypeScript 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.*

You can use this generated SDK by importing from the package `@dataconnect/default-connector` as shown below. Both CommonJS and ESM imports are supported.

You can also follow the instructions from the [Data Connect documentation](https://firebase.google.com/docs/data-connect/web-sdk#set-client).

# Accessing the connector
A connector is a collection of queries and mutations. One SDK is generated for each connector - this SDK is generated for the connector `default`.
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](https://firebase.google.com/docs/data-connect#how-does).

In order to call Data Connect queries and mutations, you need to create an instance of the connector in your application code.

```javascript
import { getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig } from '@dataconnect/default-connector';

const connector: DataConnect = getDataConnect(connectorConfig);
const dataConnect = getDataConnect(connectorConfig);
```

## Connecting to the local Emulator
Expand All @@ -27,17 +38,17 @@ To connect to the emulator, you can use the following code.
You can also follow the emulator instructions from the [Data Connect documentation](https://firebase.google.com/docs/data-connect/web-sdk#instrument-clients).

```javascript
// add connectDataConnectEmulator to your imports
import { connectDataConnectEmulator, getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig } from '@dataconnect/default-connector';

const connector: DataConnect = getDataConnect(connectorConfig);
connectDataConnectEmulator(connector, 'localhost', 9399);
const dataConnect = getDataConnect(connectorConfig);
connectDataConnectEmulator(dataConnect, 'localhost', 9399);
```

After it's initialized, you can call your Data Connect [queries](#queries) and [mutations](#mutations) from your generated SDK.
After it's initialized, you can call your Data Connect [queries](#queries) and [mutations](#mutations) from your generated SDK.

# Queries

There are two ways to execute a Data Connect Query using the generated Web SDK:
- Using a Query Reference function, which returns a `QueryRef`
- The `QueryRef` can be used as an argument to `executeQuery()`, which will execute the Query and return a `QueryPromise`
Expand All @@ -47,7 +58,7 @@ There are two ways to execute a Data Connect Query using the generated Web SDK:
The following is true for both the action shortcut function and the `QueryRef` function:
- The `QueryPromise` returned will resolve to the result of the Query once it has finished executing
- If the Query accepts arguments, both the action shortcut function and the `QueryRef` function accept a single argument: an object that contains all the required variables (and the optional variables) for the Query
- Both functions can be called with or without passing in a `DataConnect` instance as an argument
- Both functions can be called with or without passing in a `DataConnect` 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](https://firebase.google.com/docs/data-connect/web-sdk#using-queries).

Expand All @@ -68,7 +79,7 @@ listMoviesRef(dc: DataConnect): QueryRef<ListMoviesData, undefined>;
### Variables
The `ListMovies` query has no variables.
### Return Type
Recall that executing the `ListMovies` query returns a `QueryPromise` that resolves to an object with a `data` property.
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](./index.d.ts). It has the following fields:
```javascript
Expand All @@ -87,13 +98,14 @@ export interface ListMoviesData {
import { getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig, listMovies } from '@dataconnect/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 connector: DataConnect = getDataConnect(connectorConfig);
const { data } = await listMovies(connector);
const dataConnect = getDataConnect(connectorConfig);
const { data } = await listMovies(dataConnect);

console.log(data.movies);

Expand All @@ -110,12 +122,13 @@ listMovies().then((response) => {
import { getDataConnect, DataConnect, executeQuery } from 'firebase/data-connect';
import { connectorConfig, listMoviesRef } from '@dataconnect/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 connector: DataConnect = getDataConnect(connectorConfig);
const ref = listMoviesRef(connector);
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.
Expand Down Expand Up @@ -153,7 +166,7 @@ export interface GetMovieByIdVariables {
}
```
### Return Type
Recall that executing the `GetMovieById` query returns a `QueryPromise` that resolves to an object with a `data` property.
Recall that executing the `GetMovieById` query returns a `QueryPromise` that resolves to an object with a `data` property.

The `data` property is an object of type `GetMovieByIdData`, which is defined in [default-connector/index.d.ts](./index.d.ts). It has the following fields:
```javascript
Expand All @@ -171,10 +184,11 @@ export interface GetMovieByIdData {
```javascript
import { getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig, getMovieById, GetMovieByIdVariables } from '@dataconnect/default-connector';

// The `GetMovieById` query requires an argument of type `GetMovieByIdVariables`:
const getMovieByIdVars: GetMovieByIdVariables = {
id: ...,
}
};

// Call the `getMovieById()` function to execute the query.
// You can use the `await` keyword to wait for the promise to resolve.
Expand All @@ -183,8 +197,8 @@ const { data } = await getMovieById(getMovieByIdVars);
const { data } = await getMovieById({ id: ..., });

// You can also pass in a `DataConnect` instance to the action shortcut function.
const connector: DataConnect = getDataConnect(connectorConfig);
const { data } = await getMovieById(connector, getMovieByIdVars);
const dataConnect = getDataConnect(connectorConfig);
const { data } = await getMovieById(dataConnect, getMovieByIdVars);

console.log(data.movie);

Expand All @@ -200,19 +214,20 @@ getMovieById(getMovieByIdVars).then((response) => {
```javascript
import { getDataConnect, DataConnect, executeQuery } from 'firebase/data-connect';
import { connectorConfig, getMovieByIdRef, GetMovieByIdVariables } from '@dataconnect/default-connector';

// The `GetMovieById` query requires an argument of type `GetMovieByIdVariables`:
const getMovieByIdVars: GetMovieByIdVariables = {
id: ...,
}
};

// Call the `getMovieByIdRef()` function to get a reference to the query.
const ref = getMovieByIdRef(getMovieByIdVars);
// Variables can be defined inline as well.
const ref = getMovieByIdRef({ id: ..., });

// You can also pass in a `DataConnect` instance to the `QueryRef` function.
const connector: DataConnect = getDataConnect(connectorConfig);
const ref = getMovieByIdRef(connector, getMovieByIdVars);
const dataConnect = getDataConnect(connectorConfig);
const ref = getMovieByIdRef(dataConnect, getMovieByIdVars);

// Call `executeQuery()` on the reference to execute the query.
// You can use the `await` keyword to wait for the promise to resolve.
Expand All @@ -228,6 +243,7 @@ executeQuery(ref).then((response) => {
```

# Mutations

There are two ways to execute a Data Connect Mutation using the generated Web SDK:
- Using a Mutation Reference function, which returns a `MutationRef`
- The `MutationRef` can be used as an argument to `executeMutation()`, which will execute the Mutation and return a `MutationPromise`
Expand All @@ -237,7 +253,7 @@ There are two ways to execute a Data Connect Mutation using the generated Web SD
The following is true for both the action shortcut function and the `MutationRef` function:
- The `MutationPromise` returned will resolve to the result of the Mutation once it has finished executing
- If the Mutation accepts arguments, both the action shortcut function and the `MutationRef` function accept a single argument: an object that contains all the required variables (and the optional variables) for the Mutation
- Both functions can be called with or without passing in a `DataConnect` instance as an argument
- Both functions can be called with or without passing in a `DataConnect` 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](https://firebase.google.com/docs/data-connect/web-sdk#using-mutations).

Expand Down Expand Up @@ -266,7 +282,7 @@ export interface CreateMovieVariables {
}
```
### Return Type
Recall that executing the `CreateMovie` mutation returns a `MutationPromise` that resolves to an object with a `data` property.
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](./index.d.ts). It has the following fields:
```javascript
Expand All @@ -279,12 +295,13 @@ export interface CreateMovieData {
```javascript
import { getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig, createMovie, CreateMovieVariables } from '@dataconnect/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.
Expand All @@ -293,8 +310,8 @@ const { data } = await createMovie(createMovieVars);
const { data } = await createMovie({ title: ..., genre: ..., imageUrl: ..., });

// You can also pass in a `DataConnect` instance to the action shortcut function.
const connector: DataConnect = getDataConnect(connectorConfig);
const { data } = await createMovie(connector, createMovieVars);
const dataConnect = getDataConnect(connectorConfig);
const { data } = await createMovie(dataConnect, createMovieVars);

console.log(data.movie_insert);

Expand All @@ -310,21 +327,22 @@ createMovie(createMovieVars).then((response) => {
```javascript
import { getDataConnect, DataConnect, executeMutation } from 'firebase/data-connect';
import { connectorConfig, createMovieRef, CreateMovieVariables } from '@dataconnect/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 connector: DataConnect = getDataConnect(connectorConfig);
const ref = createMovieRef(connector, createMovieVars);
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.
Expand Down Expand Up @@ -364,7 +382,7 @@ export interface UpsertMovieVariables {
}
```
### Return Type
Recall that executing the `UpsertMovie` mutation returns a `MutationPromise` that resolves to an object with a `data` property.
Recall that executing the `UpsertMovie` mutation returns a `MutationPromise` that resolves to an object with a `data` property.

The `data` property is an object of type `UpsertMovieData`, which is defined in [default-connector/index.d.ts](./index.d.ts). It has the following fields:
```javascript
Expand All @@ -377,12 +395,13 @@ export interface UpsertMovieData {
```javascript
import { getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig, upsertMovie, UpsertMovieVariables } from '@dataconnect/default-connector';

// The `UpsertMovie` mutation requires an argument of type `UpsertMovieVariables`:
const upsertMovieVars: UpsertMovieVariables = {
id: ...,
title: ...,
imageUrl: ...,
}
};

// Call the `upsertMovie()` function to execute the mutation.
// You can use the `await` keyword to wait for the promise to resolve.
Expand All @@ -391,8 +410,8 @@ const { data } = await upsertMovie(upsertMovieVars);
const { data } = await upsertMovie({ id: ..., title: ..., imageUrl: ..., });

// You can also pass in a `DataConnect` instance to the action shortcut function.
const connector: DataConnect = getDataConnect(connectorConfig);
const { data } = await upsertMovie(connector, upsertMovieVars);
const dataConnect = getDataConnect(connectorConfig);
const { data } = await upsertMovie(dataConnect, upsertMovieVars);

console.log(data.movie_upsert);

Expand All @@ -408,21 +427,22 @@ upsertMovie(upsertMovieVars).then((response) => {
```javascript
import { getDataConnect, DataConnect, executeMutation } from 'firebase/data-connect';
import { connectorConfig, upsertMovieRef, UpsertMovieVariables } from '@dataconnect/default-connector';

// The `UpsertMovie` mutation requires an argument of type `UpsertMovieVariables`:
const upsertMovieVars: UpsertMovieVariables = {
id: ...,
title: ...,
imageUrl: ...,
}
};

// Call the `upsertMovieRef()` function to get a reference to the mutation.
const ref = upsertMovieRef(upsertMovieVars);
// Variables can be defined inline as well.
const ref = upsertMovieRef({ id: ..., title: ..., imageUrl: ..., });

// You can also pass in a `DataConnect` instance to the `MutationRef` function.
const connector: DataConnect = getDataConnect(connectorConfig);
const ref = upsertMovieRef(connector, upsertMovieVars);
const dataConnect = getDataConnect(connectorConfig);
const ref = upsertMovieRef(dataConnect, upsertMovieVars);

// Call `executeMutation()` on the reference to execute the mutation.
// You can use the `await` keyword to wait for the promise to resolve.
Expand Down Expand Up @@ -460,7 +480,7 @@ export interface DeleteMovieVariables {
}
```
### Return Type
Recall that executing the `DeleteMovie` mutation returns a `MutationPromise` that resolves to an object with a `data` property.
Recall that executing the `DeleteMovie` mutation returns a `MutationPromise` that resolves to an object with a `data` property.

The `data` property is an object of type `DeleteMovieData`, which is defined in [default-connector/index.d.ts](./index.d.ts). It has the following fields:
```javascript
Expand All @@ -473,10 +493,11 @@ export interface DeleteMovieData {
```javascript
import { getDataConnect, DataConnect } from 'firebase/data-connect';
import { connectorConfig, deleteMovie, DeleteMovieVariables } from '@dataconnect/default-connector';

// The `DeleteMovie` mutation requires an argument of type `DeleteMovieVariables`:
const deleteMovieVars: DeleteMovieVariables = {
id: ...,
}
};

// Call the `deleteMovie()` function to execute the mutation.
// You can use the `await` keyword to wait for the promise to resolve.
Expand All @@ -485,8 +506,8 @@ const { data } = await deleteMovie(deleteMovieVars);
const { data } = await deleteMovie({ id: ..., });

// You can also pass in a `DataConnect` instance to the action shortcut function.
const connector: DataConnect = getDataConnect(connectorConfig);
const { data } = await deleteMovie(connector, deleteMovieVars);
const dataConnect = getDataConnect(connectorConfig);
const { data } = await deleteMovie(dataConnect, deleteMovieVars);

console.log(data.movie_delete);

Expand All @@ -502,19 +523,20 @@ deleteMovie(deleteMovieVars).then((response) => {
```javascript
import { getDataConnect, DataConnect, executeMutation } from 'firebase/data-connect';
import { connectorConfig, deleteMovieRef, DeleteMovieVariables } from '@dataconnect/default-connector';

// The `DeleteMovie` mutation requires an argument of type `DeleteMovieVariables`:
const deleteMovieVars: DeleteMovieVariables = {
id: ...,
}
};

// Call the `deleteMovieRef()` function to get a reference to the mutation.
const ref = deleteMovieRef(deleteMovieVars);
// Variables can be defined inline as well.
const ref = deleteMovieRef({ id: ..., });

// You can also pass in a `DataConnect` instance to the `MutationRef` function.
const connector: DataConnect = getDataConnect(connectorConfig);
const ref = deleteMovieRef(connector, deleteMovieVars);
const dataConnect = getDataConnect(connectorConfig);
const ref = deleteMovieRef(dataConnect, deleteMovieVars);

// Call `executeMutation()` on the reference to execute the mutation.
// You can use the `await` keyword to wait for the promise to resolve.
Expand Down
4 changes: 4 additions & 0 deletions dataconnect-sdk/js/default-connector/index.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.createMovieRef = function createMovieRef(dcOrVars, vars) {
exports.createMovie = function createMovie(dcOrVars, vars) {
return executeMutation(createMovieRef(dcOrVars, vars));
};

exports.upsertMovieRef = function upsertMovieRef(dcOrVars, vars) {
const { dc: dcInstance, vars: inputVars} = validateArgs(connectorConfig, dcOrVars, vars, true);
dcInstance._useGeneratedSdk();
Expand All @@ -25,6 +26,7 @@ exports.upsertMovieRef = function upsertMovieRef(dcOrVars, vars) {
exports.upsertMovie = function upsertMovie(dcOrVars, vars) {
return executeMutation(upsertMovieRef(dcOrVars, vars));
};

exports.deleteMovieRef = function deleteMovieRef(dcOrVars, vars) {
const { dc: dcInstance, vars: inputVars} = validateArgs(connectorConfig, dcOrVars, vars, true);
dcInstance._useGeneratedSdk();
Expand All @@ -34,6 +36,7 @@ exports.deleteMovieRef = function deleteMovieRef(dcOrVars, vars) {
exports.deleteMovie = function deleteMovie(dcOrVars, vars) {
return executeMutation(deleteMovieRef(dcOrVars, vars));
};

exports.listMoviesRef = function listMoviesRef(dc) {
const { dc: dcInstance} = validateArgs(connectorConfig, dc, undefined);
dcInstance._useGeneratedSdk();
Expand All @@ -43,6 +46,7 @@ exports.listMoviesRef = function listMoviesRef(dc) {
exports.listMovies = function listMovies(dc) {
return executeQuery(listMoviesRef(dc));
};

exports.getMovieByIdRef = function getMovieByIdRef(dcOrVars, vars) {
const { dc: dcInstance, vars: inputVars} = validateArgs(connectorConfig, dcOrVars, vars, true);
dcInstance._useGeneratedSdk();
Expand Down
Loading