Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo federation: reference entities #8725

Closed
mac2000 opened this issue Jul 28, 2022 · 2 comments
Closed

apollo federation: reference entities #8725

mac2000 opened this issue Jul 28, 2022 · 2 comments
Labels
k/bug Something isn't working

Comments

@mac2000
Copy link

mac2000 commented Jul 28, 2022

Hurray #3064 is here, so giving it a try

Trying to build the following graph:

Authors Service

type Author key(fields: "id") {
  id: ID!
  name: String!
}

Books Service

type Book key(fields: "id") {
  id: ID!
  name: String!
  # author_id: ID!
  author: Author!
}

extend type Author key(fields: "id") {
  id: ID! @external
  books: [Book!]!
}

to do so I have prepared following docker compose:

docker-compose.yml
version: "3.6"
services:

  pgbooks:
    hostname: pgbooks
    container_name: pgbooks
    image: postgres:12
    environment:
      POSTGRES_PASSWORD: postgres

  pgauthors:
    hostname: pgauthors
    container_name: pgauthors
    image: postgres:12
    environment:
      POSTGRES_PASSWORD: postgres
      
  books:
    hostname: books
    container_name: books
    image: hasura/graphql-engine:v2.10.0-beta.1
    restart: always
    ports:
    - 8081:8080
    depends_on:
    - pgbooks
    environment:
      HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgres@pgbooks:5432/postgres
      PG_DATABASE_URL: postgres://postgres:postgres@pgbooks:5432/postgres
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
      HASURA_GRAPHQL_DEV_MODE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      # HASURA_GRAPHQL_ADMIN_SECRET: postgres
      HASURA_GRAPHQL_EXPERIMENTAL_FEATURES: apollo_federation
  
  authors:
    hostname: authors
    container_name: authors
    image: hasura/graphql-engine:v2.10.0-beta.1
    restart: always
    ports:
    - 8082:8080
    depends_on:
    - pgauthors
    environment:
      HASURA_GRAPHQL_METADATA_DATABASE_URL: postgres://postgres:postgres@pgauthors:5432/postgres
      PG_DATABASE_URL: postgres://postgres:postgres@pgauthors:5432/postgres
      HASURA_GRAPHQL_ENABLE_CONSOLE: "true"
      HASURA_GRAPHQL_DEV_MODE: "true"
      HASURA_GRAPHQL_ENABLED_LOG_TYPES: startup, http-log, webhook-log, websocket-log, query-log
      # HASURA_GRAPHQL_ADMIN_SECRET: postgres
      HASURA_GRAPHQL_EXPERIMENTAL_FEATURES: apollo_federation

After starting it up, connected both databases to corresponding consoles and created basic tables, then untrack them and via API add them back so they are federated, e.g.:

curl -s -i -X POST http://localhost:8081/v1/metadata -H "Content-Type: application/json" -H "X-Hasura-Admin-Secret: postgres" -d '{
  "type": "pg_track_table",
  "args": {
    "source": "postgres",
    "table": "book",
    "apollo_federation_config": {
      "enable": "v1"
    }
  }
}'

Id do see tables in { _service { sdl } } query response and can compose graph from both services with help of apollo federation:

gateway.js
// npm init -y -f
// npm i -S @apollo/gateway apollo-server graphql
// node gateway.js
const { ApolloServer } = require("apollo-server");
const { ApolloGateway, IntrospectAndCompose } = require("@apollo/gateway");

const server = new ApolloServer({
    gateway: new ApolloGateway({
        supergraphSdl: new IntrospectAndCompose({
            subgraphs: [
                { name: "books", url: "http://localhost:8081/v1/graphql" },
                { name: "authors", url: "http://localhost:8082/v1/graphql" }
            ],
            introspectionHeaders: {
                'X-Hasura-Admin-Secret': 'postgres'
            }
        })
    })
})

server.listen({port: 4000}).then(({ url }) => console.log({ url }));

But there is no description of how should we tell hasura that there is some other type which we are extending

My very first attempt was to put it into actions (e.g. it wont resolve anything in either case)

But console does not allowing me to do so, complaining that given type is wrong:

image

The second attempt was with remote schema (which probably is not what we want here) but still it does not work, complaining that there are same _entity and _service fields 🤷‍♂️

image

Maybe it is possible by adding computed field to a books table and with help of postgres function do something around this, but I'm not sure what is a right way to create function itself e.g. one from docs expecting to have a table, but in our case there wont be any table and we will just resolve given ids always, e..g something like (pseudo code):

create function author(author_row author) {
  return select author.author_id as id
}

create function author_books(author_row author) {
  return select * from books where authori_id = author.author_id
}
@mac2000 mac2000 added the k/bug Something isn't working label Jul 28, 2022
@paritosh-08
Copy link
Contributor

Hey @mac2000, thanks a lot for trying out the feature and raising the issue. Currently, the Apollo Federation support in Hasura does not allow you to extend Hasura types from other subgraphs (we recommend remote relationships for this). You can use Hasura types to extend types in other subgraphs though. You can checkout the RFC here, especially point 2:

  1. Other subgraphs can use Hasura. At a minimum, this means that table types (generated by Hasura) should be available for other subgraphs.

We will add this to the documentation as well. Thanks again for pointing this out.

@mac2000
Copy link
Author

mac2000 commented Jul 28, 2022

@paritosh-08 oh I see, sorry, somehow I was so excited about a new feature that did not see this docs 🤷‍♂️🤦‍♂️

But still - the awesome job is done 💪🎉

🤞 to have managed Hasura services in our graph in future

@mac2000 mac2000 closed this as completed Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
k/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants