--- title: Use WunderGraph with Neon subtitle: Leverage the power of Neon and WunderGraph to build fully serverless apps in minutes enableTableOfContents: true isDraft: false updatedOn: '2024-10-22T15:41:04.377Z' --- _This guide was contributed by the team at WunderGraph_ WunderGraph is an open-source Backend for Frontend (BFF) framework designed to optimize developer workflows through API composition. Developers can use this framework to compose multiple APIs into a single unified interface and generate typesafe API clients that include authentication and file uploads. This guide shows how you can pair WunderGraph with your Neon database to accelerate application development. With WunderGraph, you can easily introspect your data sources and combine them within your virtual graph. WunderGraph treats APIs as dependencies. You can easily turn your Neon database into a GraphQL API or expose it via JSON-RPC or REST. With an easy-to-deploy Postgres database like Neon, you can now have a 100% serverless stack and build your own stateful serverless apps on the edge. This guide demonstrates setting up a full-stack app with Neon and WunderGraph, securely exposing Neon to your Next.js frontend in under 15 minutes. While WunderGraph and Neon are compatible with a variety of frontend clients, this demo focuses on using Next.js. This guide is also available in video format: [Neon with WunderGraph video guide](#neon-with-wundergraph-video-guide). ## Prerequisites - A [WunderGraph Cloud](https://cloud.wundergraph.com/) account - A Neon project. See [Create a Neon project](/docs/manage/projects#create-a-project). ## Installation Sign into [WunderGraph Cloud](https://cloud.wundergraph.com/) and follow these steps: 1. Click **New Project**. 2. Choose the `NEXT.js` template and give your repository a name. 3. Select the region closest to you. 4. Click **Deploy**. The deployment will take a few moments. ### Add sample data to Neon While the project is deploying, add some sample data to your Neon database. 1. Navigate to the [Neon Console](https://console.neon.tech/) and select **SQL Editor** from the sidebar. 2. Run the following SQL statements to add the sample data. ```sql create table if not exists Users ( id serial primary key not null, email text not null, name text not null, unique (email) ); create table if not exists Messages ( id serial primary key not null, user_id int not null references Users(id), message text not null ); insert into Users (email, name) VALUES ('Jens@wundergraph.com','Jens@WunderGraph'); insert into Messages (user_id, message) VALUES ((select id from Users where email = 'Jens@wundergraph.com'),'Hey, welcome to the WunderGraph!'); insert into Messages (user_id, message) VALUES ((select id from Users where email = 'Jens@wundergraph.com'),'This is WunderGraph!'); insert into Messages (user_id, message) VALUES ((select id from Users where email = 'Jens@wundergraph.com'),'WunderGraph!'); alter table Users add column updatedAt timestamptz not null default now(); alter table Users add column lastLogin timestamptz not null default now(); ``` ### Connect Neon and Wundergraph 1. Now that your database has some data, navigate back to WunderGraph Cloud. 2. Select the project you just created and navigate to the **Settings** page. 3. Select the **Integrations** tab and click **Connect Neon**. ![WunderGraph Settings](/docs/guides/wundergraph_settings.png) 4. You are directed to Neon to authorize WunderGraph. Review the permissions and click **Authorize** to continue. You are directed back to WunderGraph Cloud. If you are a part of multiple organizations, you are asked to select the organization to connect with Neon. 5. Select the Neon project and WunderGraph project that you want to connect, and click **Connect Projects**. ![WunderGraph connect projects](/docs/guides/wundergraph_connect_projects.png) Your Neon and Wundergraph projects are now connected. WunderGraph creates a role named `wundergraph-$project_id` in the Neon project that you selected during the integration process. Please do not delete or change the password for this role. WunderGraph configures a environment variable called `NEON_DATABASE_URL`. Please use this variable wherever you need to provide a database URL. ## Set up the WunderGraph project locally The following steps describe how to set up your Wundergraph project locally and configure access to Neon. 1. In WunderGraph Cloud, select your project and click **View Git repository** to view your WunderGraph project repository. 2. Clone the repository and open it in your IDE. For example: ```bash git clone https://github.com//wundergraph.git cd wundergraph code . ``` 3. After the project is cloned, run the following commands in your project directory: ```bash npm install && npm run dev ``` These commands install the required dependencies and start your project locally. 4. Inside the `.wundergraph` directory, open the `wundergraph.config.ts` file and add Neon as a datasource, as shown below, or simply replace the existing code with this code: ```typescript import { configureWunderGraphApplication, introspect, authProviders, EnvironmentVariable, } from '@wundergraph/sdk'; import operations from './wundergraph.operations'; import server from './wundergraph.server'; const spaceX = introspect.graphql({ apiNamespace: 'spacex', url: 'https://spacex-api.fly.dev/graphql/', }); // Add your neon datasource const neon = introspect.postgresql({ apiNamespace: 'neon', //Your database URL can be found in the Neon Console databaseURL: new EnvironmentVariable('NEON_DATABASE_URL'), }); configureWunderGraphApplication({ // Add neon inside your APIs array apis: [spaceX, neon], server, operations, codeGenerators: [ { templates: [...templates.typescript.all], }, ], }); ``` 5. Write an operation that turns your Neon database into an API that exposes data that you can pass through the frontend. To do so, navigate to the `operations` folder inside your `.wundergraph` directory and create a new file called `Users.graphql`. With WunderGraph you can write operations in either GraphQL or TypeScript. Inside your `Users.graphql` file, add the following code: ```graphql { neon_findFirstusers { id name email } } ``` This operation queries your Neon database using GraphQL and exposes the data via JSON-RPC. In the next section, you will add the operation to the frontend. ## Configure the frontend This section describes how to configure the frontend application. 1. In your local project, navigate to the `pages` directory and open the `index.tsx` file. 2. In the `index.tsx` file, make the following three changes or replace the existing code with the code shown below: - Retrieve the data from the `Users` endpoint using the `UseQuery` hook. - On line 62, update the copy to read: "This is the result of your **Users** Query". - On line 66, pass the `users` variable through to the frontend. ```typescript import { NextPage } from 'next'; import { useQuery, withWunderGraph } from '../components/generated/nextjs'; const Home: NextPage = () => { const dragons = useQuery({ operationName: 'Dragons', }); // We want to write this hook to get the data from our Users operation const users = useQuery({ operationName: 'Users', }); const refresh = () => { dragons.mutate(); }; return (

WunderGraph & Next.js

Use{' '} WunderGraph {' '} to make your data-source accessible through JSON-RPC to your Next.js app.

This is the result of your{' '} Users{' '} operation.

//update dragons to users {JSON.stringify(users, null, 2)}

Visit{' '} GitHub {' '} to learn more about WunderGraph.

); }; export default withWunderGraph(Home); ``` ## Run the application 1. Run `npm run dev`. 2. Navigate to http://localhost:3000 when the application is finished building. If your application runs successfully, you should see the result of your User's operation. 3. To take the setup one step further, commit the changes to your GitHub repository and merge them into your `main` branch. 4. After you merge the changes, navigate to `WunderGraph Cloud` and view out the **Deployments** tab. You should see that a deployment was triggered. Give the deployment a few seconds to finish. 5. When deployment is ready, navigate to the **Operations** tab. You should see the new endpoint that you created and added to your application. Click it to see your data in real time. ## Key takeaways This guide provided a brief demonstration showcasing the capabilities of Neon and WunderGraph, which enable you to turn your Neon database into an API exposed via JSON-RPC and rapidly deploy fully serverless apps on the edge in a matter of minutes. The power of Neon with WunderGraph lies in simplifying the development process, allowing you to focus on creating valuable and efficient applications. In under 15 minutes, you were able to: 1. Create a WunderGraph Cloud account 2. Create a Next.js project hosted in a region near you 3. Set up a Neon database with sample data 4. Connect your WunderGraph application with your Neon database 5. Add Neon to your WunderGraph project using a code first approach 6. Write a GraphQL operation to query your Neon database 7. Update the frontend to display the results of your GraphQL operation securely using JSON-RPC 8. Commit your changes and trigger a deployment without a CI/CD pipeline or Devops team 9. View your new operations in real time with real-time metrics If you had trouble with any of the steps outlined above, refer to the video guide below. ## Neon with WunderGraph video guide