我正在尝试在react-apollo应用程序中注销后重置商店.
所以我创建了一个名为"logout"的方法,当我点击一个按钮(并通过'onDisconnect'道具传递)时调用该方法.
要做到这一点,我试图遵循这个例子:https: //www.apollographql.com/docs/react/recipes/authentication.html
但在我的情况下,我希望LayoutComponent为HOC(并且它没有graphQL查询).
这是我的组件:
import React, {Component} from 'react';
import { withApollo, graphql } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import AppBar from 'material-ui/AppBar';
import Sidebar from 'Sidebar/Sidebar';
import RightMenu from 'RightMenu/RightMenu';
class Layout extends Component {
constructor(props) {
super(props);
}
logout = () => {
client.resetStore();
alert("YOUHOU");
}
render() {
return (
<div>
<AppBar title="myApp" iconElementRight={<RightMenu onDisconnect={ this.logout() } />} />
</div>
);
}
}
export default withApollo(Layout);
Run Code Online (Sandbox Code Playgroud)
这里的问题是"客户端"没有定义,我无法正常注销.您是否有任何想法帮助我处理这种情况或从apollo客户端注销的示例/最佳实践?
谢谢你提前
当我在学习过程中将Apollo和graphQL集成到我的一个项目中时,所有人都会接触到你们.到目前为止它还可以,但现在我想尝试一些突变,我正在努力使用Input类型和Query类型.我觉得它比它应该更复杂,因此我正在寻找关于如何管理我的情况的建议.我在网上找到的例子总是有非常基本的模式,但实际情况总是比较复杂,因为我的模式很大,看起来如下(我只复制一部分):
type Calculation {
_id: String!
userId: String!
data: CalculationData
lastUpdated: Int
name: String
}
type CalculationData {
Loads: [Load]
validated: Boolean
x: Float
y: Float
z: Float
Inputs: [Input]
metric: Boolean
}
Run Code Online (Sandbox Code Playgroud)
然后定义输入和加载,依此类推......
为此,我想要一个变种来保存"计算",所以在同一个文件中我有这个:
type Mutation {
saveCalculation(data: CalculationData!, name: String!): Calculation
}
Run Code Online (Sandbox Code Playgroud)
我的解析器如下:
export default resolvers = {
Mutation: {
saveCalculation(obj, args, context) {
if(context.user && context.user._id){
const calculationId = Calculations.insert({
userId: context.user._id,
data: args.data,
name: args.name
})
return Calculations.findOne({ _id: calculationId})
}
throw new Error('Need an account …Run Code Online (Sandbox Code Playgroud) 我有两个屏幕:
屏幕 1:结果
屏幕 2:编辑过滤器
当我在 Screen2 上编辑过滤器并按回时,我想重新获取 Screen1 上的查询(使用新构建的过滤器字符串变量)。编辑过滤器不使用突变或触发任何 Redux 操作(我将用户搜索过滤器/首选项存储在 localStorage/AsyncStorage 而不是数据库中,因此没有突变)。我只是更改表单的本地状态并使用它来构建我想传递给 Screen1 上的某个查询的过滤器字符串。如果有帮助,我可以访问两个屏幕上的过滤器字符串。
似乎refetch()仅限于其查询包装的组件http://dev.apollodata.com/react/receiving-updates.html#Refetch那么我将如何从不同的屏幕重新运行查询?
我尝试在 Screen1 和 Screen2 上放置相同的查询,然后在 Screen2 上调用 refetch,尽管查询有效并在 Screen2 上获取新数据,但我实际需要的 Screen1 上的同名查询并未更新。如果他们有相同的名字,不是应该的吗?(但过滤器变量改变了)
如果我只是错误地设计了这个并且有更简单的方法来做到这一点,请告诉我。我希望如果我有 2 个屏幕,在它们两个上放置相同的查询,并使用新的过滤器变量重新获取其中一个查询,那么重新获取应该在两个地方发生,但它目前正在单独处理它们。
在 IOS 上,应用程序运行正常。但在 Android 上,我收到此错误。这是我在客户端和服务器中的配置。请帮忙!
错误: 错误图像
这是客户端的配置:
import ApolloClient, { createNetworkInterface } from 'apollo-client';
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws';
const networkInterface = createNetworkInterface({ uri: 'http://localhost:3000/graphql' });
const wsClient = new SubscriptionClient('ws://localhost:3000/subscriptions', {
reconnect: true,
});
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient,
);
export const client = new ApolloClient({
networkInterface: networkInterfaceWithSubscriptions,
});
Run Code Online (Sandbox Code Playgroud)
这是服务器上的配置:
import express from 'express';
import {
graphqlExpress,
graphiqlExpress,
} from 'graphql-server-express';
import bodyParser from 'body-parser';
import cors from 'cors';
import { execute, subscribe } from …Run Code Online (Sandbox Code Playgroud) 我最近刚开始使用 Apollo,并希望通过新的 GraphQL 层公开一些现有的 REST API。
这些 REST 服务已经被一组广泛的自定义 Typescript 类型定义完全覆盖(请求和响应数据结构)。
我希望找到一种方法来重用这些现有类型,并且可能Query在顶部添加一些其他类型,从而能够生成 GraphQL 模式而不是复制代码。
令人惊讶的是,这似乎是一个相当罕见的用例?我发现有很多工具可以从 GraphQL 生成 Typescript 定义,但反过来似乎很难。
type-graphql只有在所有自定义类型都是classes时才有效。这对我不起作用;我没有一个单一的类,但简单的无数的type在这儿。
ts2graphql已经一年没有更新了,我什至无法让示例代码工作。
我还能尝试什么?
我正在尝试从我的反应应用程序中的服务器获取国家/地区列表graphql。该getAllCountry查询在操场上运行良好,但每当我在应用程序上调用相同的查询时,我都会收到以下错误:
- “需要查询选项。您必须在查询选项中指定您的 GraphQL 文档”(屏幕上显示错误),
- “未捕获的不变违规:需要查询选项。您必须在查询选项中指定您的 GraphQL 文档。” (控制台错误)
我的代码如下所示:
// gql query inside gqlQueries.js
export const GET_ALL_COUNTRIES = gql`
query getAllCountry {
getAllCountry {
name
id
countryCode
currencyCode
}
}
`;
// calling the query
import { queries as gql } from "./gqlQueries";
const getAllCountries = () => {
client
.query({
query: gql.GET_ALL_COUNTRIES
})
.then((res) => {
console.log(res.data);
})
.catch((err) => console.log(err));
};
Run Code Online (Sandbox Code Playgroud)
我非常确定我的客户端配置正确,因为我的gqlQueries.js文件中有其他查询,并且除了这个特定的查询 ( getAllCountry) 之外,它们都工作正常。
import { IResolvers } from "graphql-tools";
我试图从 graphql-tools 导入 IResolvers 并收到消息 Module: '"../node_modules/graphql-tools"' has no returned member 'IResolvers'。
我的依赖项是:“apollo-server-express”:“^ 3.1.2”,“graphql”:“^ 15.5.1”,“graphql-tools”:“^ 8.1.0”
这是因为我使用的是 apollo 3 而不是具有 IResolver 的 apollo 2 吗?
apollo v3 中是否有任何选项可以在生产模式下禁用游乐场?在 v2 中,您可以通过传递playground: false给 ApolloServer 选项来禁用它,但在 v3 中找不到任何选项。
我正在使用 apollo-server-fastify。
编辑:我已禁用游乐场,但我仍然可以访问 localhost:4000/graphql 并收到此消息:
GET query missing.
Run Code Online (Sandbox Code Playgroud)
我的代码:
const app = fastify({ trustProxy: true });
const server = new ApolloServer({
schema,
context: ({ request }: { request: FastifyRequest }) => {
const auth = request.headers.authorization || "";
return {
auth,
session: request.session,
sessionStore: request.sessionStore,
};
},
plugins: [
myPlugin,
env === "production"
? ApolloServerPluginLandingPageDisabled()
: ApolloServerPluginLandingPageGraphQLPlayground(),
],
});
const corsOptions = {
origin: true,
optionsSuccessStatus: 200,
credentials: …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
react-apollo ×4
reactjs ×3
react-native ×2
typescript ×2
android ×1
apollostack ×1
fastify ×1
hoisting ×1
javascript ×1
meteor ×1
networking ×1
yarnpkg ×1