我正在尝试获取在 Apollo Server 2 中工作的订阅的解析器。订阅在顶级字段(即直接在Subscription根目录下schema)时有效。
但是,如果订阅包含在另一个中type,我总是会在客户端的 websocket 连接上收到错误Subscription field must return Async Iterable. Received: undefined- 服务器的解析器永远不会执行。
即这个模式有效:
type Subscription {
postAdded: Post
}
Run Code Online (Sandbox Code Playgroud)
但这一个没有:
type Subscription {
post: PostSubscription
}
type PostSubscription {
postAdded: Post
}
Run Code Online (Sandbox Code Playgroud)
我的第二种情况的解析器看起来像这样,但我尝试了很多不同的变体,但没有成功:
Subscription: {
post: () => ({
PostSubscription: {}
})
},
PostSubscription: {
postAdded: {
subscribe: () => pubSub.asyncIterator(['postAdded'])
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 graphql 后端(elixir/phoenix)设置 React/Apollo 前端。我做了npm install apollo-boost react-apollo graphql --save然后尝试import ApolloClient from "apollo-boost";在我的index.js中但我得到了
Uncaught Error: Cannot find module 'graphql' from 'apollo-link-state/lib/bundle.umd.js'
at require (app.js:61)
at expanded (app.js:34)
at app.js:148
at connectionRemoveConfig.test (bundle.umd.js:6)
at bundle.umd.js:9
at bundle.umd.js:151
at initModule (app.js:42)
at require (app.js:59)
at expanded (app.js:34)
at app.js:148
Run Code Online (Sandbox Code Playgroud)
在我的浏览器控制台中。我找不到任何相关问题。你能指出哪里出了问题吗?这是我的package.json。
{
"repository": {},
"license": "MIT",
"scripts": {
"deploy": "brunch build --production",
"watch": "brunch watch --stdin"
},
"dependencies": {
"apollo-boost": "^0.1.16",
"babel-preset-react": "^6.24.1",
"graphql": "^14.0.2",
"phoenix": "file:../deps/phoenix", …Run Code Online (Sandbox Code Playgroud) 我正在使用并尝试针对IEX REST APIApollo-Server创建 REST 查询,该查询返回如下所示的数据:
{
"symbol": "AAPL",
"companyName": "Apple Inc.",
"exchange": "Nasdaq Global Select",
"industry": "Computer Hardware",
"website": "http://www.apple.com",
"description": "Apple Inc is an American multinational technology company. It designs, manufactures, and markets mobile communication and media devices, personal computers, and portable digital music players.",
"CEO": "Timothy D. Cook",
"issueType": "cs",
"sector": "Technology",
"tags": [
"Technology",
"Consumer Electronics",
"Computer Hardware"
]
}
Run Code Online (Sandbox Code Playgroud)
我正在使用数据源。我typeDefs的resolvers看起来像这样:
const typeDefs = gql`
type Query{
stock(symbol:String): …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用来自用户的 GraphQL API 数据的查询来检查我的用户是否已登录,如果它已登录,则应返回未定义或某些错误。
问题是我有一个 func 调用removeSession,当用户单击注销按钮时,它会从我的 localStorage 中删除令牌。问题现在开始,我意识到我的令牌已从 localStorage 中清除,但之后没有触发 userLAzyQuery。所以为了检查我开始手动删除 de token 并且在 onClick 之后我调用了sendQuery()
为了强制我的检查,我在查询后创建onCompleted和onError回调,你猜怎么着?也没有被调用。
头文件.js
import { Fragment, useEffect } from 'react';
import Link from 'next/link';
import { isAuthenticatedQuery } from '../queries';
import { useQuery, useLazyQuery } from '@apollo/react-hooks';
import Router from 'next/router';
function isActive(pathname) {
return typeof document !== 'undefined' && document.location.pathname === pathname;
}
const Header = () => {
const { loading, data: dataAuth, error } …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 apollo 服务器实现自定义指令。我从官方网站上拿了例子。
我的查询如下:
directive @upper on FIELD_DEFINITION
type Query {
hello: String @upper
}
Run Code Online (Sandbox Code Playgroud)
我的解析器如下所示:
Query:{
async hello(){
return "hello world";
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的自定义指令的阿波罗服务器配置:
const { ApolloServer, SchemaDirectiveVisitor } = require('apollo-server-express');
const { defaultFieldResolver } = require("graphql");
class UpperCaseDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
const result = await resolve.apply(this, args);
if (typeof result === "string") {
return result.toUpperCase();
}
return result;
};
}
}
const …Run Code Online (Sandbox Code Playgroud) 我现在正在学习 GraphQL,但遇到了一个我无法解决的问题。
我已将架构中的类型定义为
输入学校{名称:字符串!考试:[科目!]!}输入主题{名称:字符串!标记:数字!}
但是在运行服务器时。我将数据作为 "data": { "name":"varun", "exams":[], }
服务器不会抛出任何验证错误。请建议我解决方案。是否有任何可能的方法在作为空数组通过考试时获得验证错误。
我尝试了替代方法,但在模式级别上没有任何效果。我必须明确进行检查以检查考试字段不应该是空数组。谢谢
按照此处的说明操作:
https://www.apollographql.com/docs/react/performance/server-side-rendering/#server-side-rendering
做服务器端渲染我遇到了这个错误:
Invariant Violation:
fetch is not found globally and no fetcher passed, to fix pass a fetch for
your environment like https://www.npmjs.com/package/node-fetch.
For example:
import fetch from 'node-fetch';
import { createHttpLink } from 'apollo-link-http';
const link = createHttpLink({ uri: '/graphql', fetch: fetch });
Run Code Online (Sandbox Code Playgroud)
我添加了推荐的代码,导入fetch,将它传递给createHttpLink,我也安装了@types/node-fetch,但我收到了这个警告/错误:
Error:(75, 7) TS2322: Type 'typeof fetch' is not assignable to type '(input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>'.
Types of parameters 'url' and 'input' are incompatible.
Type …Run Code Online (Sandbox Code Playgroud) 我正在开发一个阵营目前正使用的绑在GraphQL突变形式useMutation的阿波罗客户端。在服务器上,我执行一些验证,如果出现错误,我拒绝突变。在客户端,我使用error对象来接收验证错误。我的钩子看起来像这样:
const [addDrone, { error }] = useMutation(ADD_DRONE)
Run Code Online (Sandbox Code Playgroud)
因此,我将error对象解包并在对话框中将其呈现给用户,让他/她知道出了什么问题。在用户关闭对话框后,我想让用户有机会修复错误,以便他/她可以重新提交表单。这就是事情变得毛茸茸的地方。我想error在用户关闭对话框时清除对象,但由于这个变量来自useMutation钩子,我无法改变或重置它。看起来useMutation被设计为一次发射,不再使用。
所以我的问题是,有没有办法将useMutation钩子“重置”回它的原始状态?
我是 Nextjs 的新手,对 Nextjs 中的客户端渲染和服务器端渲染有一些疑问
useQuery钩子,但只能在 React 组件函数上调用。这是否意味着它仅在从客户端渲染页面时运行apolloClient到 Nextjs 的帖子。它说总是
apolloClient为 SSR创建一个新实例,并且只apolloClient为 CSR创建一个实例
这是示例代码
export function initializeApollo(initialState = null) {
const _apolloClient = apolloClient ?? createApolloClient();
// If your page has Next.js data fetching methods that use Apollo Client,
// the initial state gets hydrated here
if (initialState) {
// Get existing cache, loaded during client side data fetching
const existingCache = _apolloClient.extract();
// Restore the cache using the …Run Code Online (Sandbox Code Playgroud) 我正在 GraphQL Playground 中测试一些实现,我想在其中发送一个特定的 cookie,以便我可以在我的解析器中获取它。我在操场上使用内置的 Http Headers 窗格:
但是,当我添加名为Cookieor 的标头cookie时,当我尝试在解析器中 console.log 时它不会显示。所有其他自定义 Http 标头都没有问题。
如上面的屏幕截图所示,出现了测试头,但没有出现 cookie 头。我正在使用cookieParser,这可能是cookie标题消失的原因,但我不确定。这是我的 console.log 部分的屏幕截图:
当我尝试 console.log 时req.cookies,我什么也没得到,这是使用 cookieParser 的好处之一。
我的 ApolloServer 实现如下:
const server = new ApolloServer({
typeDefs: schema
resolvers,
dataSources: () => ({
// ...
}),
context: ({req, res}) => ({
models,
session: req.session,
req,
res
}),
// ... and the rest is not important
});
Run Code Online (Sandbox Code Playgroud)
创建“自定义”cookie 标头可以解决问题,例如somecookie: <key>=<value>,但我认为这不是最佳做法,并且宁愿避免这种情况。我希望有人知道为什么我的 cookieheader没有出现,或者我可以做些什么让它出现?
apollo ×10
graphql ×7
graphql-js ×2
reactjs ×2
typescript ×2
cookies ×1
javascript ×1
next.js ×1
react-apollo ×1
react-hooks ×1