Coder Social home page Coder Social logo

graphql-joins-example's Introduction

Hasura GraphQL Joins Example

An example of joining two GraphQL schemas, a store service and a fulfillment service, with Hasura. Both services were made with GraphQL Code Generator and GraphQL Yoga.

View the example here: https://cloud.hasura.io/public/graphiql?endpoint=https://present-hyena-89.hasura.app/v1/graphql

Run Example With Hasura Cloud

  1. Deploy the fulfillment and store service to any online host.

  2. In your Hasura Cloud project, add the FULFILLMENT_SERVICE_URL and STORE_SERVICE_URL enviroment variables. Also add a database named default.

  3. Using the Hasura CLI, apply the metadata to your Cloud project:

    hasura metadata apply --project hasura --endpoint <Hasura Cloud endpoint> --admin-secret <Hasura Cloud admin secret>

Run Example With Docker Compose

docker compose up -d --build

Then navigate to http://localhost:8080/console

Store Service

The store service is an example of an ecommerce schema with items and orders containing items

type Item {
  id: Int!
  name: String!
  price: Float!
}

type LineItem {
  quantity: Int!
  itemId: Int!
  item: Item
}

type Order {
  id: Int!
  lineItems: [LineItem!]!
}

type Query {
  item(id: Int!): Item
  items: [Item!]!
  order(id: Int!): Order
  orders: [Order]!
}

Fulfillment Service

The fulfillment service is an example of a shipping company schema with each fulfillment having an order ID and a status

enum Status {
  PACKING
  SHIPPED
  DELIVERED
}

type Fulfillment {
  id: Int!
  orderId: Int!
  status: Status!
}

type Query {
  fulfillment(orderId: Int!): Fulfillment!
  fulfillments: [Fulfillment]!
}

Joining the Schemas

Using Hasura GraphQL Joins we can combine the schemas together! From the fulfillment schema orderId key we join order information from store schema using the order type ID field. Without any code we have joined two separate services!

Screen Shot 2022-04-26 at 19 32 30

{
  fulfillment(orderId: 1) {
    status
    order {
      lineItems {
        item {
          name
        }
      }
    }
  }
}
{
  "data": {
    "fulfillment": {
      "status": "PACKING",
      "order": {
        "lineItems": [
          {
            "item": {
              "name": "Sunglasses"
            }
          }
        ]
      }
    }
  }
}

graphql-joins-example's People

Contributors

arjunyel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

graphql-joins-example's Issues

We are looking for feedback on the GraphQL Joins feature!

Hey all!

We just launched GraphQL Joins with Hasura v2.6.0 allowing you to join data across GraphQL sources using the open-source GraphQL specification. Read the announcement blog post.

Here are the important takes aways:

  • Join data between any GraphQL schema (custom servers, third-party APIs such as Github)
  • Join data between GraphQL & non GraphQL services (GraphQL to DB and DB to GraphQL)
  • No need to modify/annotate the upstream GraphQL services
  • Role-based schema access
  • Uses the open-source GraphQL spec

This repo has a simple example of a GraphQL Join between two servers mimicking types of an e-commerce setup. (store and order fulfilment).

We would really like for you to try out the various join capabilities that Hasura exposes today.

Do you have any specific use cases that you want us to cover? Are you looking at any interesting Authorization usage in a federated API? Do let us know your feedback as comments on this thread as we keep adding more examples.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.