问题标签 [graphql]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
6755 浏览

javascript - GraphQL/Relay Schema“无法查询字段...”

我在查询 graphql/relay 中的某些字段时遇到问题。我正在尝试在下面的架构中查询 Courses 上的“courseName”字段。

如果我查询用户,例如:

{用户(id:1){名字,姓氏}}

它工作并返回正确的响应。

这是我的架构:

这是我的database.js:

当我尝试查询:

{ 用户 (id:1){courses{courseName}} }

它给了我错误:

“无法在‘CourseConnection’中查询字段‘courseName’”

好像是连接有问题。我也在用 cURL 测试这个查询。因为我想在移动到反应/中继之前测试架构。

我错过了什么?谢谢你。

0 投票
2 回答
14039 浏览

graphql - What is the point of naming queries and mutations in GraphQL?

Pardon the naive question, but I've looked all over for the answer and all I've found is either vague or makes no sense to me. Take this example from the GraphQL spec:

What is the point of naming this query getZuckProfile? I've seen something about GraphQL documents containing multiple operations. Does naming queries affect the returned data somehow? I'd test this out myself, but I don't have a server and dataset I can easily play with to experiment. But it would be good if something in some document somewhere could clarify this--thus far all of the examples are super simple single queries, or are queries that are named but that don't explain why they are (other than "here's a cool thing you can do.") What benefits do I get from naming queries that I don't have when I send a single, anonymous query per request?

Also, regarding mutations, I see in the spec:

In this case, you're specifying setName twice. Why? I get that one of these is the field name of the mutation and is needed to match it to the back-end schema, but why not:

What benefit do I get specifying the same name twice? I get that the first is likely arbitrary, but why isn't it noise? I have to be missing something obvious, but nothing I've found thus far has cleared it up for me.

0 投票
1 回答
16747 浏览

graphql - 没有子部分的 GraphQL 突变

我想发送不带子部分的 graphql 变异请求

我得到

添加 { id } 以请求工作正常,但我不想要

还有架构代码

0 投票
3 回答
944 浏览

javascript - 使用 Relay 更新 React 状态

使用 Relay,你可以像往常一样创建一个 React 组件:

然后组件被包裹在一个 Relay 容器中:

Relay 容器将使用 GraphQL 获取数据,然后更新状态。这是一个高阶组件,然后这个状态作为道具传递给它的孩子。

这与 Redux 之类的通量实现不兼容(或看起来不兼容)。Redux 有一个单一的全局状态对象,它也有更高阶的组件,可以将 props 传递给展示组件。所以我看不到 Redux 存储和 Relay 容器目前是如何共存的。

那么我们应该如何更新不是来自数据库的状态呢?这个状态应该如何用 Relay 来管理?

0 投票
1 回答
1275 浏览

node.js - 在 Node.js 中包装来自多个数据源的 API

我现在正在为基于 Node.js 的新项目开发中间件层。它需要从“纯后端 API(RESTful API)”和一些数据库(MongoDB、MySQL 等)中检索数据,然后将这些数据与一些条件过滤器混合,并作为新的 API 公开给所有客户端。

此时,我只是将这些数据获取器包装为 Promises 并过滤我需要的数据。但我想知道是否有任何优雅的解决方案可以满足这些要求。

以下是一些候选方法:

  • LoopBack:LoopBack 框架可以定义多个数据源之外的模型。但是,LoopBack 似乎只支持“模型级别”的多数据源,不支持“字段级别”的数据源,这意味着我们无法定义一个特定的模型,其中一些字段来自数据库,另一些来自 RESTful API。

  • GraphQL:我是 GraphQL 的新手,但似乎 GraphQL 可以为 RESTful API 和数据库提供一些连接器。

任何其他包或库表示赞赏!谢谢大家!

0 投票
1 回答
380 浏览

reactjs - 如何累积单个请求的解析调用?

当 a访问我的服务器时,它会根据查询graphql request跳转到几个不同的位置。resolve functions但我不想有时用几十个小请求(基本上select每个resolve函数中的一个)来访问我的数据库。这不会很好地扩展。

有没有办法为单个 graphql 请求累积所有这些解析调用 - 以便最后我可以做一些魔术并只select为我的数据库构建一个然后解析所有承诺?

通过累积我在解析函数中返回的所有承诺,我可以看到一些自己构建东西的方法,但我不知道何时调用了单个请求的所有解析函数。

这个问题有解决方案吗?Facebook 如何应对这种情况?

谢谢

0 投票
1 回答
6295 浏览

javascript - 出现错误:“Query”类型的“user”字段上的未知参数“id”[GraphQL,Relay,React]

我们正在中继中形成一个查询。我们的用户数据库设置如下:



我们的 schema.js 文件如下:


我们创建了我们的中继容器:


但是我们无法通过 userId 获取用户,并且在运行 npm run build 命令时出现以下错误:

错误:“查询”类型的字段“用户”上的未知参数“id”。文件:App.js 来源:>

查询应用{用户(id:$userID)}^^^

----------------------enter code here

有人可以在这个问题上帮助我们吗?

0 投票
2 回答
917 浏览

relayjs - 共享中继节点接口

我想与多个 GraphQL 类型共享一个 nodeInterface。目前收到此错误:

我已经声明了这样的接口:

我错过了什么?我需要能够将多个 GraphQL 类型的声明分解为相应的文件并实现 nodeInterface ...

0 投票
7 回答
181390 浏览

php - 如何在不编写长查询的情况下查询所有 GraphQL 类型字段?

假设您有一个 GraphQL 类型并且它包含许多字段。如何在不写下包含所有字段名称的长查询的情况下查询所有字段?

例如,如果我有这些字段:

要查询所有字段,通常查询是这样的:

但我想要一种无需编写所有字段即可获得相同结果的方法,如下所示:

有没有办法在 GraphQL 中做到这一点?

我正在使用Folkloreatelier/laravel-graphql库。

0 投票
3 回答
8556 浏览

graphql - 处理混合类型的 GraphQL Schema

我最近开始研究使用 GraphQL 请求动态数据配置的可能性。让我印象深刻的第一件事是 GraphQL 的强类型概念。

GraphQL 模式有没有办法处理混合类型对象的数组?我将不胜感激解释或可能是我可以阅读的参考。

我目前正在使用带有 Node.js 的 GraphQL,但以后的实现将不在 Java 容器中。所有数据将从 MongoDB 中提取为 JSON。