假设您有一个将数据发布到API服务器的表单.API服务器验证输入并返回JSON对象.如果输入无效,则返回如下所示的错误对象.
{errors: {field1: "is required"}}
Run Code Online (Sandbox Code Playgroud)
使用GraphQL时,我们如何处理和提供这些错误?应该如何以及在何处实施数据验证(应该是GraphQL的一部分还是应该在每个解析函数中)?
最近我开始研究GraphQL,我能够在没有任何问题的情况下在平面模式中插入数据但是当涉及数据数组时,我收到的错误就像
{ "errors": [ { "message": "Must be input type" } ]}
Run Code Online (Sandbox Code Playgroud)
我正在使用邮递员测试我的查询,我的变异查询是
mutation M {
AddEvent
(
title: "Birthday event"
description:"Welcome to all"
media:[{url:"www.google.com", mediaType:"image" }]
location:[{address:{state:"***", city:"****"}}]
)
{title,description,media,location,created,_id}}
Run Code Online (Sandbox Code Playgroud)
这是我的事件架构:
EventType = new GraphQLObjectType({
name: 'Event',
description: 'A Event',
fields: () => ({
_id: {
type: GraphQLString,
description: 'The id of the event.',
},
id: {
type: GraphQLString,
description: 'The id of the event.',
},
title: {
type: GraphQLString,
description: 'The title of the event.',
},
description: { …Run Code Online (Sandbox Code Playgroud) GraphQL对分析解决方案非常感兴趣(想想一个显示图形的webapp).但我找不到任何使用聚合函数的GraphQL示例.这是我的前端完成的大多数查询的主要方面.
对于我的解决方案,我们有3个典型的后端调用.
假设我们在GraphQL中指定了这种类型
type Person {
name: String
age: Int
create_time: Date
}
Run Code Online (Sandbox Code Playgroud)
这似乎很好地由GraphQL处理.这里没问题.
恩.搜索名为Bob的人的年龄{Person(姓名:"Bob"){age}}
这是我想在饼图中显示信息的典型情况.所以,我想说我想按年龄计算人数.
这将是PostgreSQL查询:
SELECT age, count(*) from Ticket group by age;
Run Code Online (Sandbox Code Playgroud)
GraphQL中的等价物是什么?
恩.假设我想计算每小时创建的用户数.
这将是PostgreSQL查询:
SELECT date_trunc('hour', create_time) as create_time_bin, count(*) from Person group by create_time_bin order by create_time_bin ASC;
Run Code Online (Sandbox Code Playgroud)
什么是GraphQL等价查询?
我有一个突变
mutation deleteRecord($id: ID) {
deleteRecord(id: $id) {
id
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个位置我有一个元素列表.
我可以从服务器返回更好的东西,我该如何更新列表?
更一般地说,在apollo/graphql中处理删除的最佳做法是什么?
我有一些我想用作输入和输出的对象类型 - 例如货币类型或预订类型.
如何定义我的模式以使其具有支持输入和输出的类型 - 如果我不需要,我不想重复代码.我也不想创建重复的输入类型的东西,如货币和状态枚举.
export const ReservationInputType = new InputObjectType({
name: 'Reservation',
fields: {
hotelId: { type: IntType },
rooms: { type: new List(RoomType) },
totalCost: { type: new NonNull(CurrencyType) },
status: { type: new NonNull(ReservationStatusType) },
},
});
export const ReservationType = new ObjectType({
name: 'Reservation',
fields: {
hotelId: { type: IntType },
rooms: { type: new List(RoomType) },
totalCost: { type: new NonNull(CurrencyType) },
status: { type: new NonNull(ReservationStatusType) },
},
});
Run Code Online (Sandbox Code Playgroud) 我正在开发使用 GraphQL 的应用程序。我正在使用urqlGraphQL 客户端并且到目前为止真的很喜欢它。什么是使用的优点阿波罗/继电器过urlql?
这些天我正在尝试使用React + Relay + Graphql.不幸的是,我找不到任何简单方便的方法来测试Relay Container包装的React组件.
基本上,我想沿着TDD实现这些目标,
与React + Flux相比,React + Relay更像是黑盒子,或者说是声明式的.
我可以看到人们模拟Relay.createContainer来绕过Relay并仅仅测试React Component.它使继电器部分不被覆盖,并且无法通过测试来驱动该部件. https://github.com/facebook/relay/issues/161
另外,我阅读了Relay的测试用例,并且渲染一个模拟容器真的很乏味. https://github.com/facebook/relay/blob/master/src/tools/ mocks /RelayTestUtils.js
如果您能与我分享解决方案,我将非常感激.
谢谢!
请原谅这个天真的问题,但我已经全神贯注地寻找答案了,我发现的一切都是模糊的,或者对我没有任何意义.以GraphQL规范为例:
query getZuckProfile($devicePicSize: Int) {
user(id: 4) {
id
name
profilePic(size: $devicePicSize)
}
}
Run Code Online (Sandbox Code Playgroud)
命名此查询有什么意义?我见过一些关于包含多个操作的GraphQL文档.命名查询是否会以某种方式影响返回的数据?我自己测试了这个,但是我没有可以轻松玩的服务器和数据集来进行实验.但是,如果某个文档中的某些内容可以澄清这一点会很好 - 到目前为止所有示例都是超级简单的单个查询,或者是被命名但却无法解释其原因的查询(除了"这里很酷)你可以做的事情.")当我每次请求发送一个匿名查询时,我从命名查询中得到什么好处?
另外,关于突变,我在规范中看到:
mutation setName {
setName(name: "Zuck") {
newName
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您指定getZuckProfile两次.为什么?我得到其中一个是变异的字段名称,需要将其与后端模式匹配,但为什么不:
mutation {
setName(name: "Zuck") {
...
Run Code Online (Sandbox Code Playgroud)
我可以两次指定相同的名称有什么好处?我知道第一个可能是随意的,但为什么不是噪音呢?我不得不遗漏一些明显的东西,但到目前为止我找不到任何东西已经为我清除了.
我有几个Graphql查询和突变,现在我正在尝试实现删除变异而不返回任何数据:
type Mutation{
addElement(element:ElementData): ID
removeElement(id:ID): ¿?
}
Run Code Online (Sandbox Code Playgroud)
但是,似乎需要删除操作的返回值.有没有办法在graphql中执行"空"响应?我想避免像可能的情况那样返回布尔值或状态标志.
我不确定Graphql删除操作的最佳实践是什么
我正在使用 React、Apollo 和 Next.js 构建一个项目。我正在尝试将 react-apollo 更新到 3.1.3,现在在查看站点时出现以下错误。
不变违规:无法在上下文中找到“客户端”或作为选项传入。将根组件包装在 中,或通过选项传递 ApolloClient 实例。
如果我将 react-apollo 包降级到 2.5.8,它可以正常工作,所以我认为 2.5 和 3.x 之间发生了一些变化,但在 react-apollo 或 next-with-apollo 文档中找不到任何内容来表明那可能是什么。任何帮助将不胜感激。
withData.js
import withApollo from 'next-with-apollo';
import ApolloClient from 'apollo-boost';
import { endpoint } from '../config';
function createClient({ headers }) {
return new ApolloClient({
uri: endpoint,
request: operation => {
operation.setContext({
fetchOptions: {
credentials: 'include'
},
headers
});
},
// local data
clientState: {
resolvers: {
Mutation: {}
},
defaults: {}
}
});
}
export default withApollo(createClient); …Run Code Online (Sandbox Code Playgroud) graphql ×10
graphql-js ×2
reactjs ×2
apollo ×1
javascript ×1
next.js ×1
node.js ×1
react-apollo ×1
relayjs ×1
tdd ×1
validation ×1