解析来自不同数据源的 Graphql 查询

发布于 2025-01-16 06:03:31 字数 791 浏览 6 评论 0原文

我有一个使用 ApolloServer 和 Type-Graphql 创建的 graphql 服务器。

我在 graphql 服务器上定义了两种类型:

User{
   id:string;
   name:string
   email:string;
   ...
}

UserPrefrences{
   userId:string;
   theme: string;
   color:string;
   ...
}

User 类型的数据保存在数据库中,我通过转发从客户端收到的请求,通过不同的 graphql 服务器访问该数据库。

UserPrefrences 类型的数据保存在另一个数据库中,我直接从 graphql 服务器访问该数据库。

我不希望我的客户端需要了解这两种不同的类型并且需要运行两个单独的查询。 我正在寻找一种方法让我的客户端在我的 graphql 服务器上运行以下查询:

query UserData($userId: String!) {
   id
   name
   email
   theme
   color
}

但是如果我将此请求转发到我正在查询的 graphql 服务器,我将得到一个响应,其中包含字段“主题”和“颜色” ” 他不知道。

我正在尝试找到一种方法,仅将相关字段转发到 graphql 服务器,然后在我的 graphql 服务器中解析其余字段。但我收到的查询是一个字符串,这使得尝试使用正则表达式仅转发我感兴趣的字段变得很痛苦。

对于如何解决此问题的任何想法,我将非常高兴。

I have a graphql server I've created using ApolloServer and Type-Graphql.

I have two types defined on my graphql server:

User{
   id:string;
   name:string
   email:string;
   ...
}

UserPrefrences{
   userId:string;
   theme: string;
   color:string;
   ...
}

The data for the User type is saved in a database which I access through a different graphql server by forwarding the request I get from client.

The data for the UserPrefrences type is saved on a different database which I access directly from my graphql server.

I don't want my client side to need to know these two separate types and to need to run two separate queries.
I'm looking for a way to let my client run the following query on my graphql server:

query UserData($userId: String!) {
   id
   name
   email
   theme
   color
}

But if I forward this request to the graphql server that I'm querying, I will get a response saying the fields 'theme' and 'color' are unknown to him.

I'm trying to find a way to forward only the relevant fields to the graphql server, and then resolving the rest within my graphql server. But I receive the query as a string which makes it a pain trying to use regex to only forward the fields I'm interested in.

I'd be more than happy for any ideas on how to solve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻了热茶 2025-01-23 06:03:31

我发现的唯一方法是在 Node.js 中使用 Graphql 客户端。

我正在使用名为 graphql-request 的 npm 库
https://www.npmjs.com/package/graphql-request

import { GraphQLClient, gql } from 'graphql-request';

const client = new GraphQLClient('http://localhost:4000/graphql');

const query = `
  {
    yourQuery {
      id
      name
    }
  }
`;

const response = client.request(query);

the only way I found is using a Graphql client in Node.js.

I'm using the npm library called graphql-request
https://www.npmjs.com/package/graphql-request

import { GraphQLClient, gql } from 'graphql-request';

const client = new GraphQLClient('http://localhost:4000/graphql');

const query = `
  {
    yourQuery {
      id
      name
    }
  }
`;

const response = client.request(query);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文