我正在尝试从我的反应应用程序中的服务器获取国家/地区列表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) 之外,它们都工作正常。
我有一个使用 Apollo Client v1 的基本应用程序:
const client = new ApolloClient({...
...
<ApolloProvider client={client}>
<App />
</ApolloProvider>,
Run Code Online (Sandbox Code Playgroud)
我想知道如何让我的应用程序的其余部分访问该client对象,以便他们可以使用client.query?
导出客户端然后将其导入另一个文件中的唯一方法吗?
由于我的应用程序包装在 a 中ApolloProvider,我想可能有一种client使用 Apollo 特定导入进行导入的方法?
所以,我在我的 iOS 应用程序中运行了一个 apollo 服务器和一个 apollo 客户端。
我正在尝试实施身份验证过程。按照阿波罗有关授权和身份验证的文档,我最终尝试了他们提供的代码:
context: ({ req }) => {
throw new AuthenticationError('you must be logged in');
},
Run Code Online (Sandbox Code Playgroud)
但是,在测试代码时,我发现抛出错误,无论是 javascript 错误还是 apollo 错误,它总是会发回 http 400 错误:
Error: Response not successful: Received status code 400
at new ApolloError (errors.cjs.js:31)
at core.cjs.js:1493
at both (utilities.cjs.js:963)
at utilities.cjs.js:956
at tryCallTwo (core.js:45)
at doResolve (core.js:200)
at new Promise (core.js:66)
at Object.then (utilities.cjs.js:956)
at Object.error (utilities.cjs.js:964)
at notifySubscription (Observable.js:140)
Run Code Online (Sandbox Code Playgroud)
认证错误不会被发回。即使当我抛出自定义错误时,使用自定义状态代码和所有内容它总是返回相同的错误。
难道我做错了什么 ?或者这是一个问题?
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)
在我看来,这并不是最好的方法。我应该怎么办?
我目前正在开发一个项目,在该项目中我使用了 nuxt、vue-apollo 和包装 vue-apollo 并将其集成到 nuxt 中的 nuxt apollo-module。
我的页面中定义了一个相当简单的智能查询:
<template>
<div>
<ul>
<li v-for="itinerary in itineraries" :key="itinerary.id">
{{ itinerary.id }}
</li>
</ul>
</div>
</template>
<script>
import gql from 'graphql-tag';
export default {
name: 'HomePage',
apollo: {
itineraries: {
prefetch: true,
query: gql`
query {
itineraries {
id
}
}
`
}
},
data() {
return {
itineraries: []
};
}
};
</script>
Run Code Online (Sandbox Code Playgroud)
从我从文档中了解到,预取应该在服务器上启动查询,但它不会阻塞,因此服务器端渲染将在返回数据之前继续。为了避免水化错误,nuxt apollo 模块应该使用 apollo 缓存来存储结果,如 Vue SSR 指南中所述:https : //ssr.vuejs.org/guide/data.html#data-store .
但是,当我加载页面时,我在控制台中看到了水化错误:
子节点与 VNode 不匹配:
NodeList …
在尝试访问后端(Apollo GraphQL)时清除 NextAuth.js 会话的最佳方法是什么,并且由于令牌已过期或无效而返回 401?
我想到了errorLinkand signout,但据我所知signout不能在服务器端使用getServerSideProps,而只能在客户端使用。
推荐的方法是什么?有没有其他方法可以实现中间件来处理这种情况?
这将是errorLink我正在尝试实现的概念的证明,代码被困在其中,if但我无法使用,signOut()因为它仅在客户端可用。
const errorLink = onError(({ graphQLErrors }) => {
if (graphQLErrors?.[0]?.message === 'Unauthenticated.') {
// signOut();
}
});
function createApolloClient(session) {
return new ApolloClient({
cache: new InMemoryCache(),
ssrMode: typeof window === 'undefined',
link: from([
errorLink,
createUploadLink({
uri: GRAPHQL_URI,
credentials: 'same-origin',
headers: { Authorization: session?.accessToken ? `Bearer ${session.accessToken}` : '' },
}),
]),
});
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在使用 apollo-angular 构建一个前端应用程序。在我的应用程序中,我使用带有短期访问令牌和长期刷新令牌的 JWT 身份验证系统(它们在仅 HTTP cookie 中传输,而不是开始存储在 HTTP 标头中)。
当我运行我的应用程序时,我可以成功登录,但是当访问令牌过期时,我收到以下错误并且我在浏览器上看不到任何内容。
Error: Network error: Cannot read property 'refresh' of undefined at new ApolloError
我的代码如下:
GraphQLModule(它是在 AppModule 中导入的)(部分基于此问题)
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { ApolloModule, APOLLO_OPTIONS } from 'apollo-angular';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpLinkModule, HttpLink } from 'apollo-angular-link-http';
import { ApolloLink, Observable } from 'apollo-link';
import { onError } from 'apollo-link-error';
import { AuthService } from …Run Code Online (Sandbox Code Playgroud) 现在我尝试在 Strapi GraphQL 游乐场中的位置内使用多个 _and 进行查询。我正在使用的查询是:
{
clients(
where: {
_and: [{ name: { _contains: "a" } }, { endDate: { _gte: "2017-12-31" } }]
}
) {
id
name
endDate
reasonForEnding
}
}
Run Code Online (Sandbox Code Playgroud)
但出现错误提示如下:
"Error: Your filters contain a field '_and' that doesn't appear on your model definition nor it's relations".
Run Code Online (Sandbox Code Playgroud)
如何使用 GraphQL 在 Strapi 中使用多个 _and 条件 where 子句正确进行查询
apollo ×10
graphql ×6
reactjs ×3
angular ×2
javascript ×2
react-apollo ×2
fastify ×1
hoisting ×1
next-auth ×1
next.js ×1
nuxt.js ×1
react-native ×1
strapi ×1
typescript ×1
vue-apollo ×1
vue.js ×1
where-clause ×1
yarnpkg ×1