我想知道如何在同一个 GraphQL 查询中使用输入类型及其字段之一作为参数。我认为有一些有效的解决方案,但我想知道哪些(如果有)是最佳实践。
考虑以下假设查询。我们通过位置和状态获取玩家,以及处于同一位置的团队成员,但该member字段只有一个location参数:
input PlayerInput {
location: String!
status: Int!
}
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(location: ???) { // <-- How to access playerInput.location?
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以想出几种方法来解决这个问题:
1. 更改查询以采用单独的参数
input PlayerInput {
location: String!
status: Int!
}
query getPlayers($playerInput: PlayerInput) {
players(playerInput: $playerInput) {
name
team {
name
members(location: ???) { // <-- How to access playerInput.location?
name
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
2. 更新架构以members …
我尝试用 TDD 视图编写代码,我一直遇到同样的问题,我应该测试什么,不应该测试什么,目标是使用 TDD 制作一个基于 React-bootstrap 的导航栏,我的组件将只是一个具有较少属性的包装器,以使其变得简单,我从react-bootstrap中Navbar的品牌子组件开始。最终目标是测试反映用户应该测试的内容,例如导航中存在具有良好 src 和 alt 属性的品牌徽标 img。
这些测试有趣还是应该避免?
我的测试
import React from 'react';
import { render } from '@testing-library/react';
import BrandNav from './BrandNav';
test('testing brand navigation', () => {
const altProp = 'message to show if image unavailable',
srcProp = 'relativeFilePath',
host = window.location;
const { getByAltText } = render(<BrandNav alt={altProp} src={srcProp} />);
const brandLogo = getByAltText(altProp);
expect(brandLogo.src).toEqual(`${host}${srcProp}`);
});
Run Code Online (Sandbox Code Playgroud)
我的组件
import React from 'react';
import { Navbar } from 'react-bootstrap';
const BrandNav = ({ alt, …Run Code Online (Sandbox Code Playgroud) 在使用 React 测试库测试组件时,我发现自己从 开始getBy*,偶尔需要将其替换为queryBy*(例如,如果我需要检查某个元素是否不存在)。我的测试最终混合了getBy和queryBy,而我最近刚刚使用queryBy了所有东西。
这让我思考......有没有理由使用getBy?
像这样的断言按预期失败,无需抛出错误:
expect(queryByText('Click me')).toBeInTheDocument();
expect(queryByLabel('Name').value).toBe('George')
Run Code Online (Sandbox Code Playgroud)
如果未找到元素,则抛出错误有什么好处,是否有理由不queryBy用于所有(同步)查询?
编辑:看起来queryBy现在只推荐用于断言文档中没有内容:
该文章还建议使用screen.queryBy/screen.getBy而不是解构 from render,这简化了从一个到另一个的更改,因为您不再需要更新解构函数。