You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define these relations in your config file to enable joins to and from your views.
260
266
267
+
### Derive Definitions
268
+
269
+
Our derives setup offers a powerful way to join data efficiently between multiple Postgres databases. You can use several approaches to enable joins across two or more Postgres databases.
270
+
271
+
The Grafbase Gateway prevents GraphQL N+1 problems in all cross-database joins by minimizing the number of queries needed to load data.
272
+
273
+
Let's explore some examples using these SQL schemas:
274
+
275
+
Database A:
276
+
277
+
```sql
278
+
CREATETABLE "posts" (
279
+
id INTPRIMARY KEY,
280
+
title VARCHAR(255) NOT NULL,
281
+
author_id INTNOT NULL
282
+
);
283
+
```
284
+
285
+
Database B:
286
+
287
+
```sql
288
+
CREATETABLE "users" (
289
+
id INTPRIMARY KEY,
290
+
name VARCHAR(255) NOT NULL
291
+
)
292
+
```
293
+
294
+
We want to create a federated GraphQL schema that joins posts and users:
Creatinganextraviewtojoindatabetweendatabasesoftenaddsmaintenanceoverheadandcanslowdownperformance. Instead, usethecompositespec `@is` and `@derive` directives. DefinederivesforsubgraphA:
346
+
347
+
```toml
348
+
[schemas.public.tables.posts.derives.author]
349
+
referenced_type = "User"
350
+
fields = { id = "authorId" }
351
+
```
352
+
353
+
This tells the system: for the `posts` table, create a derived field `author` that loads a single `User` entity. The `id` field of the User type maps to the `authorId` field of the `Post` type.
0 commit comments