简体   繁体   English

如何在hasura中为特定模式执行graphql查询?

[英]How to execute graphql query for a specific schema in hasura?

As it can be seen in the following screenshot, the current project database (postgresql) named default has these 4 schema - public, appcompany1, appcompany2 and appcompany3.从下面的屏幕截图中可以看出,名为default的当前项目数据库 (postgresql) 具有这 4 个架构 - public、appcompany1、appcompany2 和 appcompany3。

哈苏拉控制台

They share some common tables.它们共享一些公共表。 Right now, when I want to fetch data for customers, I write a query like this:现在,当我想为客户获取数据时,我会写一个这样的查询:

query getCustomerList {
    customer {
        customer_id
        ...
        ...
    }
}

And it fetches the required data from public schema.它从公共模式中获取所需的数据。

But according to the requirements, depending on user interactions in front-end, that query will be executed for appcompanyN (N=1,2,3,..., any positive integer).但是根据要求,根据前端的用户交互,将为 appcompanyN(N=1,2,3,...,任何正整数)执行该查询。 How do I achieve this goal?我如何实现这个目标?

NOTE: Whenever the user creates a new company, a new schema is created for that company.注意:每当用户创建新公司时,都会为该公司创建一个新架构。 So the total number of schema is not limited to 4.所以模式的总数不限于 4。

I suspect that you see a problem where it does not exists actually.我怀疑你看到了一个实际上并不存在的问题。

Everything is much simpler than maybe it seems.一切都比看起来简单得多。

A. Where all those tables? A. 那些桌子在哪里?

There are a lot of schemas with identical (or almost identical) objects inside them.有很多模式在它们内部具有相同(或几乎相同)的对象。

All tables are registered in hasura.所有表都在 hasura 中注册。

Hasura can't register different tables with the same name, so by default names will be [schema_name]_[table_name] (except for public ) Hasura 无法注册同名的不同表,因此默认名称将为 [schema_name]_[table_name] ( public除外)

So table customer will be registered as:所以表customer将被注册为:

  • customer (from public ) customer (来自public
  • appcompany1_customer
  • appcompany2_customer
  • appcompany3_customer

It's possible to customize entity name in GraphQL-schema with "Custom GraphQL Root Fields".可以使用“自定义 GraphQL 根字段”在 GraphQL 模式中自定义实体名称。

B. The problem B、问题

But according to the requirements, depending on user interactions in front-end, that query will be executed for appcompanyN (N=1,2,3,..., any positive integer).但是根据要求,根据前端的用户交互,将为 appcompanyN(N=1,2,3,...,任何正整数)执行该查询。 How do I achieve this goal?我如何实现这个目标?

There are identical objects that differs only with prefixes with schema name.有相同的对象,仅在带有模式名称的前缀上有所不同。

So solutions are trivial所以解决方案是微不足道的

1. Dynamic GraphQL query 1.动态GraphQL查询

Application stores templates of GraphQL-queries and replaces prefix with real schema name before request.应用程序存储 GraphQL 查询的模板,并在请求之前用真实的模式名称替换前缀。

Eg例如

query getCustomerList{
   [schema]_customer{
   }
}

substitute [schema] with appcompany1 , appcompany2 , appcompanyZ and execute.appcompany1appcompany2appcompanyZ替换[schema]并执行。

2. SQL view for all data 2.所有数据的SQL视图

If tables are 100% identical then it's possible to create an sql view as:如果表 100% 相同,则可以创建一个 sql 视图,如下所示:

CREATE VIEW ALL_CUSTOMERS
AS
SELECT 'public' as schema,* FROM public.customer
UNION ALL 
SELECT 'appcompany1' as schema,* FROM appcompany1.customer
UNION ALL
SELECT 'appcompany2' as schema,* FROM appcompany2.customer
UNION ALL
....
SELECT `appcompanyZ',* FROM appcompanyZ.customer

This way: no need for dynamic query, no need to register all objects in all schemas.这样:不需要动态查询,不需要注册所有模式中的所有对象。

You need only to register view with combined data and use one query您只需要使用组合数据注册视图并使用一个查询

query{
query getCustomerList($schema: string) {
   all_customer(where: {schema: {_eq: $schema}}){
     customer_id
   }
}

About both solutions: it's hard to call them elegant.关于这两种解决方案:很难称它们为优雅。

I myself dislike them both ;)我自己都不喜欢他们两个;)

So decide yourself which is more suitable in your case.因此,请自行决定哪种更适合您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM