无法向 GraphQL 服务器发送请求

时间:2021-06-18 10:10:24

标签: graphql apollo-client mern apollo-server

我正在通过开发一个非常简单的全栈应用程序来学习 GraphQL。客户端正在 https://localhost:3001 运行。 GraphQL 服务器在 https:localhost:4000 运行。 GraphQL 服务器通过与 json-server 交互来获取数据(用户列表),https:localhost:3000 是一个在 Network 上运行的假 REST API。

当我启动我的应用程序时,我在客户端收到一个错误。

enter image description here

当我查看{"errors":[{"message":"request to https://localhost:3000/users failed, reason: write EPROTO 14596:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\\ws\\deps\\openssl\\openssl\\ssl\\record\\ssl3_record.c:332:\n","locations":[{"line":2,"column":3}],"path":["employees"],"extensions":{"code":"INTERNAL_SERVER_ERROR","exception":{"message":"request to https://localhost:3000/users failed, reason: write EPROTO 14596:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\\ws\\deps\\openssl\\openssl\\ssl\\record\\ssl3_record.c:332:\n","type":"system","errno":"EPROTO","code":"EPROTO","stacktrace":["FetchError: request to https://localhost:3000/users failed, reason: write EPROTO 14596:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\\ws\\deps\\openssl\\openssl\\ssl\\record\\ssl3_record.c:332:",""," at ClientRequest.<anonymous> (C:\\Users\\Delhivery\\Desktop\\odyssey-lift-off-part1\\server\\node_modules\\node-fetch\\lib\\index.js:1461:11)"," at ClientRequest.emit (events.js:315:20)"," at TLSSocket.socketErrorListener (_http_client.js:469:9)"," at TLSSocket.emit (events.js:315:20)"," at emitErrorNT (internal/streams/destroy.js:106:8)"," at emitErrorCloseNT (internal/streams/destroy.js:74:3)"," at processTicksAndRejections (internal/process/task_queues.js:80:21)"]}}}],"data":null} 标签时,错误如下:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";

const client = new ApolloClient({
  uri: "http://localhost:4000",
  cache: new InMemoryCache(),
});

ReactDOM.render(
  <ApolloProvider client={client}>
    <App />
  </ApolloProvider>,
  document.getElementById("root")
);

我不知道这个错误是什么意思。

代码片段如下:

client/src/index.js

const { ApolloServer } = require("apollo-server");
const typeDefs = require("./schema");
const resolvers = require("./resolvers");
const EmployeesAPI = require("../data-source/employees-api");

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return { employeesAPI: new EmployeesAPI() };
  },
});

server.listen(4000, () => {
  console.log(`
    Server is running!
    Listening on port 4000
    `);
});

server/index.js

const { RESTDataSource } = require("apollo-datasource-rest");

class EmployeesAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "https://localhost:3000";
  }

  getEmployees() {
    return this.get("users");
  }
}

module.exports = EmployeesAPI;

server/data-source/employees-api.js

https:localhost:3000/users

当我访问 with open('sanfrancisco_crashes_cp.json') as json_file: json_data = json.load(json_file) accidents = json_data['accidents'] for accident in accidents: accident['Turn'] = 'right' with open('sanfrancisco_crashes_cp.json', "w") as f: json.dump(json_data, f) 时,我获得了用户列表。

enter image description here

我怎样才能摆脱错误并成功获取数据?

0 个答案:

没有答案