我们在应用程序中使用 Apollo/Graphql,但遇到了一个问题。
该应用程序是在 Angular 5 中开发的,NodeJS 作为后端,graphql 服务器将响应前端。为了以 Angular 拦截 Http 请求,我们实现了“HttpInterceptor”,以便我们可以以自定义的方式处理错误。但是,Apollo 请求不会通过这个拦截器。
为了正确处理错误,我们使用了“apollo-link-error”。
但是我们想知道是否有可能以类似于 HttpInterceptor 的方式拦截 apollo 请求。
我正在尝试创建一个乐观的响应,其中 ui 会通过拖放数据立即更新(最小延迟和更好的用户体验)。然而,我遇到的问题是它无论如何都会滞后。
所以发生的事情是我希望从我的查询中得到一个区域和未分配区域的列表,unassignedZone 是一个对象,其中包含城市,区域是一个区域列表,其中包含城市。在编写我的突变时,我会在拖放它们后返回重新排序的新区域列表。重新排序是由名为“DisplayOrder”的区域对象上的字段完成的。逻辑是正确设置数字。问题是,当我尝试用乐观的 ui 和更新来模仿它时,会有一点延迟,就像它仍在等待网络一样。
我试图实现的大部分内容都发生在 onDragEnd = () => { ... } 函数中。
import React, { Component } from "react";
import { graphql, compose, withApollo } from "react-apollo";
import gql from "graphql-tag";
import { withState } from "recompose";
import { withStyles } from "@material-ui/core/styles";
import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";
import Input from "@material-ui/core/Input";
import Grid from "@material-ui/core/Grid";
import InputLabel from "@material-ui/core/InputLabel";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
import AppBar from "@material-ui/core/AppBar";
import …Run Code Online (Sandbox Code Playgroud) const httpLink = createHttpLink({
uri: 'http://localhost:3090/'
})
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache()
})
client.query({
query: gql`
query users {
email
}
`,
})
.then(data => console.log(data))
.catch(error => console.error(error));
Run Code Online (Sandbox Code Playgroud)
从客户端代码获取时,此查询会出错,但是当我在浏览器中的http://localhost:3090/graphql上执行此查询时,它会正确获取数据
我正在尝试禁用Apollo 上的缓存,因此我正在关注文档apollo-client
,但我无法成功,我一直收到此警告 ApolloBoost was initialized with unsupported options: defaultOptions
有没有人有同样的警告?
import Vue from 'vue'
import ApolloClient from 'apollo-boost'
const defaultOptions = {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'ignore'
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all'
}
}
const client = new ApolloClient({
defaultOptions: defaultOptions,
)};
Run Code Online (Sandbox Code Playgroud) 我的应用程序分为客户端和服务器。Client 是托管在 Now.sh 上的前端 Nextjs 应用程序,Server 是其后端使用 Express 创建并托管在 Heroku 上,因此域是 client-app.now.sh 和 server-app.herokuapp.com 。
验证
身份验证系统基于 cookie,我使用 express-session 来实现它。这是我的快速会话配置
app.use(
session({
store:
process.env.NODE_ENV === "production"
? new RedisStore({
url: process.env.REDIS_URL
})
: new RedisStore({
host: "localhost",
port: 6379,
client
}),
name: "sessionID",
resave: false,
saveUninitialized: false,
secret: keys.SESSION,
unset: "destroy",
cookie: {
domain:
process.env.NODE_ENV === "production"
? ".client-app.now.sh"
: "localhost",
secure: process.env.NODE_ENV === "production",
httpOnly: true,
maxAge: 7 * 24 * 60 * 60 * 1000
}
})
); …Run Code Online (Sandbox Code Playgroud) 如果是,应覆盖以下哪一种方法以及如何覆盖?我无法找到有关如何覆盖每个示例的示例。
visitSchema(schema: GraphQLSchema)visitScalar(scalar: GraphQLScalarType)visitObject(object: GraphQLObjectType)visitFieldDefinition(field: GraphQLField<any, any>)visitArgumentDefinition(argument: GraphQLArgument)visitInterface(iface: GraphQLInterfaceType)visitUnion(union: GraphQLUnionType)visitEnum(type: GraphQLEnumType)visitEnumValue(value: GraphQLEnumValue)visitInputObject(object: GraphQLInputObjectType)visitInputFieldDefinition(field: GraphQLInputField)我的直觉会说,visitObject(object: GraphQLObjectType)因为type Query是一个GraphQLObjectType.
大家好,
之前已经讨论过这一点,但这是其中之一,有如此多的分散讨论导致各种提议的“黑客”,我很难确定我应该做什么。
我想使用查询的结果作为另一个嵌套查询的参数。
query {
allStudents {
nodes {
courseAssessmentInfoByCourse(courseId: "2b0df865-d7c6-4c96-9f10-992cd409dedb") {
weightedMarkAverage
// getting result for specific course is easy enough
}
coursesByStudentCourseStudentIdAndCourseId {
nodes {
name
// would like to be able to do something like this
// to get a list of all the courses and their respective
// assessment infos
assessmentInfoByStudentId (studentId: student_node.studentId) {
weightedMarkAverage
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有没有一种被认为是最佳实践的方法?现在是否有一种标准的方法可以内置到 GraphQL 中?
谢谢你的帮助!
我正在使用 nestjs graphql 框架,我想使用 apollo 标量上传
我已经能够在另一个不包含 nestjs 的项目中使用标量。
schema.graphql App.module.ts 注册 graphql
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
resolvers: { Upload: GraphQLUpload },
installSubscriptionHandlers: true,
context: ({ req }) => ({ req }),
playground: true,
definitions: {
path: join(process.cwd(), './src/graphql.classes.ts'),
outputAs: 'class',
},
uploads: {
maxFileSize: 10000000, // 10 MB
maxFiles: 5
}
}),
Run Code Online (Sandbox Code Playgroud)
pets.resolver.ts 变异 createPet
@Mutation('uploadFile')
async uploadFile(@Args('fileUploadInput') fileUploadInput: FileUploadInput) {
console.log("TCL: PetsResolver -> uploadFile -> file", fileUploadInput);
return {
id: '123454',
path: 'www.wtf.com',
filename: fileUploadInput.file.filename,
mimetype: fileUploadInput.file.mimetype
}
} …Run Code Online (Sandbox Code Playgroud) 我的一个项目突然无法在 Windows 笔记本电脑上编译,而完全相同的代码却可以在 Mac 上运行。我读过有关提升和添加 nohoist 的内容,这似乎解决了 Apollo 客户端的问题。
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/tslib",
"**/tslib/**"
]
}
Run Code Online (Sandbox Code Playgroud)
现在,我不使用工作区,但由于我在 package.json 中使用上面的代码,Yarn-W在添加或删除包时会询问参数:
error Running this command will add the dependency to the workspace root rather than
the workspace itself, which might not be what you want - if you really meant it, make it
explicit by running this command again with the -W flag (or --ignore-workspace-root-check).
Run Code Online (Sandbox Code Playgroud)
在我看来,这并不是最好的方法。我应该怎么办?
我目前正在尝试在我的posts. 这里使用 Apollo graphql 是我的 useQuery
const { data: postsData, fetchMore } = useQuery(POSTS_BY_USER_DRAFT, {
fetchPolicy: 'network-only',
variables: {
user: user.id,
start: 0,
limit: limit
},
onCompleted: () => {
setTotal(postsData[model].meta.pagination.total)
}})
Run Code Online (Sandbox Code Playgroud)
这是我的 onClick 处理程序,用于获取更多内容posts
const loadMorePosts = async () => {
const nextStart = start + limit
setStart(nextStart);
await fetchMore({
variables: {
user: user.id,
offset: nextStart,
limit: limit,
},
updateQuery: (prevResult, { fetchMoreResult }) => {
if (!fetchMoreResult) {
return prevResult
}
const prevData = prevResult[model].data …Run Code Online (Sandbox Code Playgroud) apollo ×10
graphql ×6
javascript ×3
reactjs ×3
react-apollo ×2
angular ×1
cookies ×1
express ×1
graphql-js ×1
hoisting ×1
nestjs ×1
next.js ×1
node.js ×1
react-dnd ×1
react-native ×1
vue-apollo ×1
yarnpkg ×1