Get started with Firebase Data Connect

In this quickstart, you will learn how to build Firebase Data Connect in your application with a production SQL instance. You will:

  • Add Firebase Data Connect to your Firebase project.
  • Provision a Cloud SQL instance for your app.
  • Set up a development environment including a Visual Studio Code extension to work with a production instance.
  • Then we will show you how to:
    • Create a schema for a movie app
    • Define queries and mutations that will be used in your app
    • Test your queries and mutations with sample data
    • Generate strongly typed SDKs and use them in your app
    • Deploy your final schema, queries and data to the cloud.

Create a Firebase project and Cloud SQL database

  1. If you haven't already, create a Firebase project.
    1. In the Firebase console, click Add project, then follow the on-screen instructions.
  2. Navigate to the Data Connect section of the Firebase console and follow the product setup workflow.
  3. Upgrade your project to the Blaze plan. This lets you create a Cloud SQL for PostgreSQL instance.

  4. Select a location for your CloudSQL for PostgreSQL database.

  5. Note the project, service and database names and IDs for confirmation later.

  6. Follow the remaining setup flow then click Done.

Choose a development flow

Data Connect offers you two ways to install development tools.

Set up the development environment

  1. Create a new directory for your local project.
  2. Run the following command in the new directory you created.

      curl -sL https://firebase.tools/dataconnect | bash

    This script tries to set up the development environment for you and launch a browser-based IDE. This IDE provides toolings, including a pre-bundled VS Code extension, to help you manage your schema and define queries and mutations to be used in your application, and generate strongly-typed SDKs.

alias dataconnect='curl -sL https://firebase.tools/dataconnect | bash'

Set up your project directory

To set up your local project, initialize your project directory. In the IDE window, in the left-hand panel, click the Firebase icon to open the Data Connect VS Code extension UI:

  1. Click the Sign in with Google button.
  2. Click the Connect a Firebase project button and select the project you created earlier in the console.
  3. Click the Run firebase init button and complete the flow.
  4. Click the Start emulators button.

Create a schema

In your Firebase project directory, in the /dataconnect/schema/schema.gql file, start defining a GraphQL schema that includes movies.

Movie

In Data Connect, GraphQL fields are mapped to columns. The Movie type has id, title, imageUrl and genre. Data Connect recognizes the primitive data types String and UUID.

Copy the following snippet or uncomment the corresponding lines in the file.

# File `/dataconnect/schema/schema.gql`

# By default, a UUID id key will be created by default as primary key.
type Movie @table {
  id: UUID! @default(expr: "uuidV4()")
  title: String!
  imageUrl: String!
  genre: String
}

MovieMetadata

Now that you have movies, you can model movie metadata.

Copy the following snippet or uncomment the corresponding lines in the file.

# Movie - MovieMetadata is a one-to-one relationship
type MovieMetadata @table {
  # This time, we omit adding a primary key because
  # you can rely on Data Connect to manage it.

  # @unique indicates a 1-1 relationship
  movie: Movie! @unique
  # movieId: UUID <- this is created by the above reference
  rating: Float
  releaseYear: Int
  description: String
}

Notice that the movie field is mapped to a type of Movie. Data Connect understands that this is a relationship between Movie and MovieMetadata and will manage this relationship for you.

Learn more about Data Connect schemas in the documentation

Deploy your schema to production

You need to deploy your schema before continuing.

In the extension UI, under the Firebase Data Connect panel, click Deploy to production.

After deploying your schema to your production database, you should be able to view the schema in the Firebase console.

Add data to your tables

In the IDE editor panel, you can see CodeLens buttons appear over the GraphQL types in /dataconnect/schema/schema.gql. Since you've deployed your schema to production, you can use the Add data and Run (Production) buttons add data to your database on the backend.

To add records to the Movie table:

  1. In schema.gql, click the Add data button above the Movie type declaration.
    Code Lens Add data button for Firebase Data Connect
  2. In the Movie_insert.gql file that is generated, hard code data for the four fields.
  3. Click the Run (Production) button.
    Code Lens Run button for Firebase Data Connect
  4. Repeat the previous steps to add a record to the MovieMetadata table, supplying the id of your Movie in the movieId field, as prompted in the generated MovieMetadata_insert mutation.

To quickly verify data was added:

  1. Back in schema.gql, click the Read data button above the Movie type declaration.
  2. In the resulting Movie_read.gql file, click the Run (Production) button to execute the query.

Learn more about Data Connect mutations in the documentation

Define your query

Now the fun part, queries. As a developer, you're accustomed to writing SQL queries rather than GraphQL queries, so this can feel a bit different at first. However, GraphQL is far more terse and type-safe than raw SQL. And our VS Code extension eases the development experience.

Start editing the /dataconnect/connector/queries.gql file. If you want to get all movies, use a query like this.

# File `/dataconnect/connector/queries.gql`

# @auth() directives control who can call each operation.
# Anyone should be able to list all movies, so the auth level
# is set to PUBLIC
query ListMovies @auth(level: PUBLIC) {
  movies {
    id
    title
    imageUrl
    genre
  }
}

Execute the query using the nearby CodeLens button.

Learn more about Data Connect queries in the documentation

Generate SDKs and use them in your app

  1. Click the Add SDK to app button.
  2. In the dialog that appears, select a directory containing code for your app. Data Connect SDK code will be generated and saved there.

  3. Select your app platform, then note that SDK code is immediately generated in your selected directory.

Learn how to use the generated SDK to call queries and mutations from client apps (web, Android, iOS, Flutter).

Deploy your schema and query to production

You have worked through a development iteration. Now you can deploy your schema, data, and queries to the server with the Firebase extension UI or the Firebase CLI, just as you did with your schema.

In the IDE window, in the VS Code Extension UI, click the Deploy to production button.

Once deployed, go to the Firebase console to verify the schema, operations and data has been uploaded to the cloud. You should be able to view the schema, and run your operations on the console as well. The Cloud SQL for PostgreSQL instance will be updated with its final deployed generated schema and data.

Learn more about using the Data Connect emulator in the documentation

Next steps

Review your deployed project and discover more tools:

  • Add data to your database, inspect and modify your schemas, and monitor your Data Connect service in the Firebase console. Access more information in the documentation. For example, since you've completed the quickstart:

  • Learn more about schema, query and mutation development

  • Learn about generating client SDKs and calling queries and mutations from client code for web, Android, iOS, and Flutter.