我有一个操作getFoo要求用户通过身份验证才能访问资源。
用户使用突变进行身份验证authenticate,例如
mutation {
authenticate (email: "foo", password: "bar") {
id
}
}
Run Code Online (Sandbox Code Playgroud)
当用户通过身份验证时,会发生两件事:
但是,我想将身份验证和getFoo方法调用结合到一个请求中,例如
mutation {
authenticate (email: "foo", password: "bar") {
id
}
}
query {
getFoo {
id
}
}
Run Code Online (Sandbox Code Playgroud)
后者产生语法错误。
有没有办法将突变与查询结合起来?
我有一个基于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) 我试图根据官方文档动态设置 Apollo 客户端的标头,但出现错误:
TypeError: (0 , _apollo.default) is not a function
Run Code Online (Sandbox Code Playgroud)
这是我的apollo.js
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { AsyncStorage } from 'react-native';
const httpLink = createHttpLink({
uri: 'http://192.168.2.4:8000/api/',
});
const authLink = setContext((_, { headers }) => {
const token = AsyncStorage.getItem('token');
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
}); …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) 使用 进行干净的 Gatsby 5.2 安装后Gatsby CLI,我需要帮助理解我的终端输出,其中显示了大量NPM WARN标志。
我有三个问题;
我在这里问这些问题是因为我想先发制人地学习如何正确理解和处理它们。
我在这里阅读了一些类似的问题,其中一些建议使用该--legacy-peer-deps标志。
我理解为什么有人可能--legacy-peer-deps一般使用该标志,但我很难理解为什么新版本,即(Gatsby 5.0),需要使用--legacy-peer-deps.
NPM 错误仍然是我的痛点,所以我正在寻找易于理解的阅读材料。或者如果可能的话,提供全面的解释。
运行后:gatsby info --clipboard
% gatsby info --clipboard
System:
OS: macOS 13.0.1
CPU: (16) x64 Intel(R) Xeon(R) W-2140B CPU @ 3.20GHz
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
Browsers:
Chrome: 108.0.5359.98
Safari: 16.1
npmPackages:
gatsby: ^5.2.0 => 5.2.0
gatsby-plugin-image: ^3.2.0 => 3.2.0
gatsby-plugin-manifest: …Run Code Online (Sandbox Code Playgroud) 我想知道如何正确创建用于创建此 Django 模型的突变:
class Company(models.Model):
class Meta:
db_table = 'companies'
app_label = 'core'
default_permissions = ()
name = models.CharField(unique=True, max_length=50, null=False)
email = models.EmailField(unique=True, null=False)
phone_number = models.CharField(max_length=13, null=True)
address = models.TextField(max_length=100, null=False)
crn = models.CharField(max_length=20, null=False)
tax = models.CharField(max_length=20, null=False)
parent = models.ForeignKey('self', null=True, on_delete=models.CASCADE)
currency = models.ForeignKey(Currency, null=False, on_delete=models.CASCADE)
country = models.ForeignKey(Country, null=False, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
Run Code Online (Sandbox Code Playgroud)
如您所见,共有三个外键。对于模型Currency、Country和Parent(self)。公司 DjangoObjectType看起来非常简单,如下所示:
class CompanyType(DjangoObjectType):
class Meta:
model = Company
Run Code Online (Sandbox Code Playgroud)
最后我的变异类 …
graphql ×10
javascript ×4
apollo ×2
react-native ×2
angular ×1
aws-amplify ×1
django ×1
enums ×1
node.js ×1
npm ×1
react-apollo ×1
rest ×1