Joining Data Sources

Graeme CouparGraeme Coupar
Joining Data Sources

Grafbase has long had support for adding different data sources to a single graph using connectors like OpenAPI, Postgres and GraphQL. But these have always been relatively separate - if two distinct data sources have related types they won't be linked to each other in any way.

Federation offers one way to solve this problem - you can treat each of your data sources as a subgraph and join them together that way. But we've now also introduced joins, which offer a lightweight way to connect your types together in a standalone graph.

For example: if you've imported Stripe & Orb as data sources using the OpenAPI connector you might want to join together their Customer types. We can do this by adding an orbCustomer field onto the StripeCustomer type, and instructing it to join onto the customersExternalCustomerId Query field provided by Orb:

g.extend("StripeCustomer", (extend) => {
  extend.addField(
    "orbCustomer",
    g.ref("OrbCustomer").optional().join(
       "customersExternalCustomerId(externalCustomerId: $id)"
    ))))
})

Once this is done, you can make a query to fetch this related data:

query FetchCustomers($email: String!) {
  stripe {
    customers(email: $email) {
      nodes {
        id
        name
        orbCustomer {
          balance
          timezone
          portalUrl
        }
      }
    }
}

Which would return your linked data:

{
  "data": {
    "stripe": {
      "customers": {
        "nodes": [
          {
            "id": "1",
            "name": "Jane",
            "orbCustomer": {
              "balance": "1000000",
              "timezone": "America/Los_Angeles",
              "portalUrl": "http://example.com/1234"
            }
          }
        ]
      }
    }
  }
}

More information on how this works, and the limitations of the current implementation can be found in the documentation

We'd love to hear your feedback and ideas, so join us on Discord.