我尝试在 GraphQL 的 Apollo 客户端中更改 addTypeName: false
apollo.create({
link: httpLinkWithErrorHandling,
cache: new InMemoryCache({ addTypename: false }),
defaultOptions: {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'all'
}
}
Run Code Online (Sandbox Code Playgroud)
但它可以工作,并且会在控制台中抛出以下消息
fragmentMatcher.js:26 You're using fragments in your queries, but either don't have the addTypename:true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.Please turn on the addTypename option and include __typename when writing fragments so that Apollo Clientcan accurately match fragments.
Run Code Online (Sandbox Code Playgroud)
,
Could not …Run Code Online (Sandbox Code Playgroud) 来自Facebook的官方消息是,Relay" 故意不了解身份验证机制".在中继存储库中的所有示例中,身份验证和访问控制是一个单独的问题.实际上,我还没有找到一种简单的方法来实现这种分离.
Relay存储库中提供的示例都具有根模式,其中的viewer字段假定有一个用户.该用户可以访问所有内容.
然而,实际上,应用程序具有许多用户,并且每个用户对每个节点具有不同的访问程度.
假设我在JavaScript中有这个架构:
export const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
user: {
type: new GraphQLObjectType({
name: 'User',
args: {
// The `id` of the user being queried for
id: { type: new GraphQLNonNull(GraphQLID) },
// Identity the user who is querying
session: { type: new GraphQLInputObjectType({ ... }) },
},
resolve: (_, { id, session }) => {
// Given `session, get user with `id` …Run Code Online (Sandbox Code Playgroud) 我很难理解何时使用GraphQLInterfaceType和GraphQLUnionType.
我有RTFM:
任何人都可以提供一个真实世界的例子,当这些通过我的厚头有用时会有用吗?
Facebook没有提到他们的GraphQL库的身份验证.
假设我有一个可以从GraphQL获取的用户表,我不希望向除了登录用户之外的任何需要它的人透露用户信息,我应该在什么级别添加认证层?
在模式级别通过改变"登录"状态?
或者可能通过将额外的参数传递给graphql当前只需要的函数query和schema?
我有一个基于getAllItems查询的过滤项目列表,它将过滤器和一个order by选项作为参数.
创建新项目后,我想删除此查询的缓存,无论传递什么变量.我不知道该怎么做.
我不认为更新缓存是一种选择.Apollo Client文档中提到的方法(在变异后更新缓存,refetchQueries和更新)似乎都需要一组给定的变量,但由于过滤器是一个复杂的对象(带有一些文本信息),我需要更新缓存对于先前提交的每组给定变量.我不知道该怎么做.另外,只有服务器确实知道这个新项目如何影响分页和排序.
我不认为fetch-policy(例如将其设置为cache-and-network)是我正在寻找的,因为如果在创建新项目后访问网络是我想要的,那么当我只是过滤列表时(输入要搜索的字符串),我想保持默认行为(cache-only).
client.resetStore会为所有类型的查询(不仅是getAllItems查询)重置商店,所以我不认为这也是我正在寻找的.
我很确定我在这里遗漏了一些东西.
我正在尝试创建一个架构,但是会变得太长和混乱,拆分不同的查询、突变和输入的最佳实践是什么,这样我就可以只需要它们并组织它们以使其易于阅读。
我试图在网上查找信息,但没有任何明确的信息,我尽量不使用 Apollo。
const { buildSchema } = require('graphql');
module.exports = buildSchema(`
type Region {
_id: ID!
name: String!
countries: [Country!]
}
type Country {
_id: ID!
name: String!
region: [Region!]!
}
type City {
_id: ID!
name: String!
country: [Country!]!
}
type Attraction {
_id: ID!
name: String!
price: Float!
description: String!
city: [City!]!
}
type Eatery {
_id: ID!
name: String!
cuisine: String!
priceRange: String!
location: [Location!]!
typeOfEatery: String!
city: [City!]!
}
type Location {
_id: ID!
latitude: …Run Code Online (Sandbox Code Playgroud) 我有属于主要类别的主要类别和次要类别。两者都是 ENUM 类型。我希望客户选择匹配的次要类别 ENUM 与其主要类别一起提交。我不想将所有不同的次要类别 ENUM 作为字段包含在内。
我第一次尝试做
union MinorCategories = Minor1 | Minor2
但是这失败了,因为union只适用于ObjectTypes
没有必要根据主要类别强制执行次要类别。我只想通过 ENUM 接收一个可由客户端选择的字段。有什么解决办法吗?
我有一个 Apollo GraphQL 服务器,我有一个删除记录的突变。此更改接收资源的 UUID,调用 REST(Ruby on Rails)API,当删除成功时,该 API 仅返回成功的 HTTP 代码和空正文(204 无内容)以及带有错误的 HTTP 错误代码删除不起作用时的消息(404 或 500,典型的 REST 删除端点)。
在定义 GraphQL 突变时,我必须定义突变返回类型。突变返回类型应该是什么?
input QueueInput {
"The queue uuid"
uuid: String!
}
deleteQueue(input: QueueInput!): ????????
Run Code Online (Sandbox Code Playgroud)
我可以使它与几种不同类型的返回(布尔值、字符串等)一起工作,但我想知道什么是最佳实践,因为我尝试过的返回类型都没有感觉是正确的。我认为重要的是,在调用突变后,在客户端我有一些关于如果事情进展顺利(API 返回 204 不是内容)或如果发生一些错误(API 返回 404 或 500)发生了什么的信息,并且最好有一些关于错误。
所以我们正在使用 Apollo 和 GraphQL 创建一个 React-Native 应用程序。我正在使用基于 JWT 的身份验证(当用户同时登录activeToken和refreshToken 时),并希望实现一个流程,当服务器注意到它已过期时,令牌会自动刷新。
Apollo-Link-Error 的 Apollo Docs 提供了一个很好的起点来捕获来自 ApolloClient 的错误:
onError(({ graphQLErrors, networkError, operation, forward }) => {
if (graphQLErrors) {
for (let err of graphQLErrors) {
switch (err.extensions.code) {
case 'UNAUTHENTICATED':
// error code is set to UNAUTHENTICATED
// when AuthenticationError thrown in resolver
// modify the operation context with a new token
const oldHeaders = operation.getContext().headers;
operation.setContext({
headers: {
...oldHeaders,
authorization: getNewToken(),
},
});
// …Run Code Online (Sandbox Code Playgroud) 我在一个角度项目中使用放大。当我运行命令 ng serve 时出现此错误。
Error: node_modules/@aws-amplify/api-graphql/lib-esm/types/index.d.ts:1:30 - error TS7016: Could not find a declaration file for module 'graphql/error/GraphQLError'. 'C:/Users/Ruwani Indrachapa/Documents/profileApp/profileApp1/node_modules/graphql/error/GraphQLError.js' implicitly has an 'any' type.
Try `npm install @types/graphql` if it exists or add a new declaration (.d.ts) file containing `declare module 'graphql/error/GraphQLError';`
1 import { GraphQLError } from 'graphql/error/GraphQLError';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/@aws-amplify/api-graphql/lib-esm/types/index.d.ts:2:30 - error TS7016: Could not find a declaration file for module 'graphql/language/ast'. 'C:/Users/Ruwani Indrachapa/Documents/profileApp/profileApp1/node_modules/graphql/language/ast.js' implicitly has an 'any' type.
Try `npm install @types/graphql` if it exists or add …Run Code Online (Sandbox Code Playgroud) graphql ×10
javascript ×5
apollo ×3
graphql-js ×2
relayjs ×2
angular ×1
aws-amplify ×1
enums ×1
node.js ×1
react-apollo ×1
react-native ×1
rest ×1