|
| 1 | +# Create Your First Application |
| 2 | +Now that you've set up Harper, let's build a simple Book API. Start by cloning the template: |
| 3 | + |
| 4 | +```bash |
| 5 | +git clone https://github.com/HarperDB/application-template my-book-app |
| 6 | +cd my-book-app |
| 7 | +``` |
| 8 | + |
| 9 | +The template includes: |
| 10 | +- **schema.graphql** – Your GraphQL schema definition |
| 11 | +- **resources.js** – For custom application logic |
| 12 | +- **config.yaml** – Application-level settings |
| 13 | + |
| 14 | +## Define a GraphQL Schema |
| 15 | +Edit your `schema.graphql` file to create a Book table: |
| 16 | +```graphql |
| 17 | +type Book @table @export { |
| 18 | + id: ID @primaryKey |
| 19 | + title: String! |
| 20 | + author: String! |
| 21 | + publishedYear: Int |
| 22 | + genre: String |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +The schema above does several important things: |
| 27 | +- Creates a `Book` table with the `@table` directive |
| 28 | +- Makes the table accessible via REST and GraphQL using the `@export` directive |
| 29 | +- Defines an `id` field as the primary key with the `@primaryKey` directive |
| 30 | +- Requires `title` and `author` fields (marked with `!`) |
| 31 | +- Adds optional `publishedYear` and `genre` fields |
| 32 | + |
| 33 | +When you start your application, Harper will automatically create this table structure. |
| 34 | + |
| 35 | +## Extend the Resource Class |
| 36 | +Now let's create the business logic for our Book API by implementing a custom resource class. Update your `resources.js` file: |
| 37 | + |
| 38 | +```js |
| 39 | +export class Books extends Resource { |
| 40 | + // Get a book by ID or list all books |
| 41 | + async get() { |
| 42 | + console.log("GET request received"); |
| 43 | + const id = this.getId(); |
| 44 | + |
| 45 | + if (id) { |
| 46 | + // Return a single book |
| 47 | + return await this.table.get(id); |
| 48 | + } else { |
| 49 | + // Return all books with optional genre filter |
| 50 | + const genre = this.getQueryParam("genre"); |
| 51 | + const searchOptions = genre ? { genre } : {}; |
| 52 | + return await this.table.search(searchOptions); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Create a new book |
| 57 | + async post(data) { |
| 58 | + console.log("POST request received"); |
| 59 | + |
| 60 | + // Parse string data if needed |
| 61 | + if (typeof data === "string") { |
| 62 | + try { |
| 63 | + data = JSON.parse(data); |
| 64 | + } catch (err) { |
| 65 | + return { error: "Invalid JSON" }; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Validate required fields |
| 70 | + if (!data.title || !data.author) { |
| 71 | + return { error: "Title and author are required" }; |
| 72 | + } |
| 73 | + |
| 74 | + // Prevent primary key overriding |
| 75 | + if (data.id) delete data.id; |
| 76 | + |
| 77 | + try { |
| 78 | + const result = await this.table.post(data); |
| 79 | + return result; |
| 80 | + } catch (error) { |
| 81 | + return { error: "Error creating book", details: error.message }; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | +Let's break down what this JavaScript file is doing: |
| 87 | + |
| 88 | +### Resource Class Extension |
| 89 | +```js |
| 90 | +export class Books extends Resource { |
| 91 | +``` |
| 92 | +
|
| 93 | +This line creates a custom `Books` class that extends Harper's built-in `Resource` class. By doing this, we inherit all the standard functionality while allowing us to customize behavior. |
| 94 | +
|
| 95 | +### GET Method |
| 96 | +```js |
| 97 | +async get() { |
| 98 | + console.log("GET request received"); |
| 99 | + const id = this.getId(); |
| 100 | + |
| 101 | + if (id) { |
| 102 | + // Return a single book |
| 103 | + return await this.table.get(id); |
| 104 | + } else { |
| 105 | + // Return all books with optional genre filter |
| 106 | + const genre = this.getQueryParam("genre"); |
| 107 | + const searchOptions = genre ? { genre } : {}; |
| 108 | + return await this.table.search(searchOptions); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | +
|
| 113 | +The `get()` method handles HTTP GET requests to our Book endpoint. It: |
| 114 | +- Logs that a request was received (helpful for debugging) |
| 115 | +- Checks if an ID was provided in the URL path |
| 116 | +- If an ID exists, returns a single book by that ID |
| 117 | +- If no ID exists, checks for a `genre` query parameter |
| 118 | +- Returns all books, filtered by genre if specified |
| 119 | +
|
| 120 | +### POST Method |
| 121 | +```js |
| 122 | +async post(data) { |
| 123 | + console.log("POST request received"); |
| 124 | + |
| 125 | + // Parse string data if needed |
| 126 | + if (typeof data === "string") { |
| 127 | + try { |
| 128 | + data = JSON.parse(data); |
| 129 | + } catch (err) { |
| 130 | + return { error: "Invalid JSON" }; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + // Validate required fields |
| 135 | + if (!data.title || !data.author) { |
| 136 | + return { error: "Title and author are required" }; |
| 137 | + } |
| 138 | + |
| 139 | + // Prevent primary key overriding |
| 140 | + if (data.id) delete data.id; |
| 141 | + |
| 142 | + try { |
| 143 | + const result = await this.table.post(data); |
| 144 | + return result; |
| 145 | + } catch (error) { |
| 146 | + return { error: "Error creating book", details: error.message }; |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | +
|
| 151 | +The `post()` method handles HTTP POST requests to create new books. It: |
| 152 | +- Handles both string and object data formats |
| 153 | +- Validates that required fields (`title` and `author`) are present |
| 154 | +- Removes any provided `id` field to prevent primary key conflicts |
| 155 | +- Creates the book record in the database |
| 156 | +- Returns the result or an error message if something goes wrong |
| 157 | +
|
| 158 | +## Run and Test |
| 159 | +1. Start your application: |
| 160 | + ```bash |
| 161 | + harperdb dev . |
| 162 | + ``` |
| 163 | +2. Create a book: |
| 164 | + ```bash\ |
| 165 | + curl -X POST -H "Content-Type: application/json" \ -d '{"title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "publishedYear": 1925, "genre": "Fiction"}' \ http://localhost:9926/Books |
| 166 | + ``` |
| 167 | +3. Retrieve a book by ID: |
| 168 | + ```bash |
| 169 | + curl http://localhost:9926/Book/BOOK_ID_HERE |
| 170 | + ``` |
| 171 | +4. Get all books in a genre: |
| 172 | + ```bash |
| 173 | + curl http://localhost:9926/Book?genre=Fiction |
| 174 | + ``` |
| 175 | +
|
| 176 | +This simple Book API demonstrates Harper's key features: |
| 177 | +- Schema-based table definitions with GraphQL |
| 178 | +- Custom resource extensions for business logic |
| 179 | +- Automatic REST endpoint generation |
| 180 | +- Built-in data validation and error handling |
| 181 | +
|
| 182 | +You can build on this foundation by adding more fields, implementing additional methods, or creating relationships to other tables. |
0 commit comments