页面加载时的 Graphql 错误请求

如何解决页面加载时的 Graphql 错误请求

大家好,节日快乐,

我正在用 keystonejs 和 NextJS 构建一个网站。我在两者之间添加了 Apollo Client。

但是,我现在遇到了 Apollo Client 的问题。我也尝试了不同的地方,但结果是一样的,无论如何这是我的 _app.tsx 文件

import { useReducer } from "react";
import { useRouter } from "next/router";
import { ThemeProvider } from "styled-components";

import { HttpLink } from "apollo-link-http";
import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { Query,KeystoneProvider } from "@keystonejs/apollo-helpers";
import { ApolloProvider,gql } from "@apollo/client";

import { primaryTheme } from "../styles/theme";
import GlobalStyle from "../styles/global";

import { initialState,globalReducer } from "../context/reducer";

import Meta from "../components/global/Meta";
import { Header } from "../components/global/Header";
import { globalContext } from "../context/contex";
import Footer from "../components/global/Footer";
import Loading from "../components/global/Loading";

const client = new ApolloClient({
  ssrMode: true,cache: new InMemoryCache(),link: new HttpLink({
    uri: process.env.NEXT_PUBLIC_API_URL,fetchOptions: {
      mode: 'cors'
     },}),});


const QUERY = gql`
query {
  allOperations{
    id
    name
  }
}
`

const MyApp = ({ Component,pageProps }) => {
  const [store,dispatch] = useReducer(globalReducer,initialState);
  const router = useRouter();

  return (
    <ApolloProvider client={client}>
      <KeystoneProvider>

        <ThemeProvider theme={primaryTheme}>

          <Meta />
          {router.route !== "/" && <Header />}
          <Query
            query={QUERY}
          >
          {({ loading,data }) => {
            console.log(data);
            
            if(loading){ 
              return <Loading /> 
            } else { 

              return <Component {...pageProps} />
        }}}
          </Query>
          <Footer />
          <GlobalStyle />
        </ThemeProvider>
      </KeystoneProvider>
    </ApolloProvider>
  );
};


export default MyApp;

在每个页面页面加载时,我收到了这个 400 Bad request 错误

{errors: [{,…}]}
errors: [{,…}]
0: {,…}
extensions: {code: "GRAPHQL_VALIDATION_Failed",exception: {stacktrace: [,…]}}
code: "GRAPHQL_VALIDATION_Failed"
exception: {stacktrace: [,…]}
stacktrace: [,…]
0: "GraphQLError: Field "queries" of type "_ListQueries" must have a selection of subfields. Did you mean "queries { ... }"?"
1: "    at Object.Field (/Users/artticfox/Desktop/Work/frank/backend/node_modules/graphql/validation/rules/ScalarLeafsRule.js:40:31)"
2: "    at Object.enter (/Users/artticfox/Desktop/Work/frank/backend/node_modules/graphql/language/visitor.js:323:29)"
3: "    at Object.enter (/Users/artticfox/Desktop/Work/frank/backend/node_modules/graphql/utilities/TypeInfo.js:370:25)"
4: "    at visit (/Users/artticfox/Desktop/Work/frank/backend/node_modules/graphql/language/visitor.js:243:26)"
5: "    at Object.validate (/Users/artticfox/Desktop/Work/frank/backend/node_modules/graphql/validation/validate.js:69:24)"
6: "    at validate (/Users/artticfox/Desktop/Work/frank/backend/node_modules/apollo-server-core/dist/requestPipeline.js:221:34)"
7: "    at Object.<anonymous> (/Users/artticfox/Desktop/Work/frank/backend/node_modules/apollo-server-core/dist/requestPipeline.js:118:42)"
8: "    at Generator.next (<anonymous>)"
9: "    at fulfilled (/Users/artticfox/Desktop/Work/frank/backend/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)"
10: "    at runMicrotasks (<anonymous>)"
11: "    at processticksAndRejections (internal/process/task_queues.js:93:5)"
locations: [{line: 5,column: 7}]
0: {line: 5,column: 7}
column: 7
line: 5
message: "Field "queries" of type "_ListQueries" must have a selection of subfields. Did you mean "queries { ... }"?"
name: "ValidationError"
uid: "ckjbkc1j9001qng0d2itof7d9"

但我根本不请求列表查询

Bad Request

前 2 个 API 调用是 204,最后一个是 200,我得到了很好的查询。我的假设是由于 SSR 而发生这种情况,但我需要一个解决方案。我也尝试过加载和其他东西,但没有用。

这是我的 keystonejs 设置。

const { Keystone } = require("@keystonejs/keystone");
const { GraphQLApp } = require("@keystonejs/app-graphql");
const { AdminUIApp } = require("@keystonejs/app-admin-ui");
const { MongooseAdapter: Adapter } = require("@keystonejs/adapter-mongoose");
const { PasswordAuthStrategy } = require("@keystonejs/auth-password");
const { NextApp } = require('@keystonejs/app-next');
require("dotenv").config();
const OperationSchema = require("./lists/Operation.ts");
const UserSchema = require("./lists/User.ts");

const PROJECT_NAME = "frank";
const adapterConfig = {
  mongoUri: process.env.DATABASE,};

/**
 * You've got a new keystonejs Project! Things you might want to do next:
 * - Add adapter config options (See: https://keystonejs.com/keystonejs/adapter-mongoose/)
 * - Select configure access control and authentication (See: https://keystonejs.com/api/access-control)
 */

const keystone = new Keystone({
  adapter: new Adapter(adapterConfig),});

keystone.createList("Operation",OperationSchema);
keystone.createList("User",UserSchema);

const authStrategy = keystone.createAuthStrategy({
  type: PasswordAuthStrategy,list: "User",config: {
    identityField: "username",secretField: "password",},});

module.exports = {
  keystone,apps: [
    new GraphQLApp(),new AdminUIApp({ name: PROJECT_NAME,enableDefaultRoute: false }),new NextApp({ dir: '../frontend/' }),],};

后端在 localhost:3000 上运行 前端运行在 localhost:7777

提前致谢。

节日快乐

解决方法

我遇到了同样的问题,并找到了解决方法。

该错误是由@keystonejs/apollo-helpers 中过时的 GraphQL 查询引起的。您可以通过更新 dist 目录中的代码并更新包含 META_QUERY 变量的所有文件以包含缺少的查询子字段。

query ListMeta {
  _ksListsMeta {
    schema {
      type
      queries {
        item
        list
        meta
        __typename
      }
      relatedFields {
        type
        fields
      }
    }
  }
}

请记住,当重新安装/更新@keystonejs/apollo-helpers 软件包时,此解决方法将不起作用。

,

找到此错误的原因,Keystoneprovider 出于某种原因创建了此问题。如果有人知道原因,最好知道原因。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?