gpt4 book ai didi

express-graphql - 无法读取 Object.renderGraphiQL 中未定义的属性 'startsWith'

转载 作者:行者123 更新时间:2023-12-02 20:25:12 25 4
gpt4 key购买 nike

我正在学习 udemy 的 GraphQL 教程,

https://www.udemy.com/introduction-to-graphql-and-apollo-building-modern-apis

我正在阅读操作 graphql 和 graphiql -> apollo -express - 服务器的指南。并得到了这个。视频中尚未定义此特定错误。这是一个免费的教程,第 9 课有这个。该怎么办。我找不到解决办法。请帮忙。

TypeError: Cannot read property 'startsWith' of undefined
at Object.renderGraphiQL (/home/dell/Desktop/graphql-
tutorial/node_modules/apollo-server-module-
graphiql/src/renderGraphiQL.ts:48:17)
at Object. (/home/dell/Desktop/graphql-tutorial/node_modules/apollo-
server-module-graphiql/src/resolveGraphiQLString.ts:62:10)
at step (/home/dell/Desktop/graphql-tutorial/node_modules/apollo-
server-module-graphiql/dist/resolveGraphiQLString.js:32:23)
at Object.next (/home/dell/Desktop/graphql-
tutorial/node_modules/apollo-server-module-
graphiql/dist/resolveGraphiQLString.js:13:53)
at fulfilled (/home/dell/Desktop/graphql-tutorial/node_modules/apollo-
server-module-graphiql/dist/resolveGraphiQLString.js:4:58)
at
at process._tickDomainCallback (internal/process/next_tick.js:228:7)

renderGraphiQLString.js

这是它说有错误的行 -->

 export function renderGraphiQL(data: GraphiQLData): string {
const endpointURL = data.endpointURL;
const endpointWs =
endpointURL.startsWith('ws://') || endpointURL.startsWith('wss://');
const subscriptionsEndpoint = data.subscriptionsEndpoint;
const usingHttp = !endpointWs;
const usingWs = endpointWs || !!subscriptionsEndpoint;
const endpointURLWs =
usingWs && (endpointWs ? endpointURL : subscriptionsEndpoint);

resolveGraphiQLString.js

 export async function resolveGraphiQLString(
query: any = {},
options: GraphiQLData | Function,
...args
): Promise<string> {
const graphiqlParams = createGraphiQLParams(query);
const graphiqlOptions = await resolveGraphiQLOptions(options,
...args);
const graphiqlData = createGraphiQLData(graphiqlParams,
graphiqlOptions);
return renderGraphiQL(graphiqlData);
}

server.js

 import express  from 'express';
import {graphqlExpress,graphiqlExpress} from 'apollo-server-express';
import bodyParser from 'body-parser';
import schema from './schema.js'


const server = express();

server.use('/graphql', bodyParser.json(), graphqlExpress(schema));

server.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql'
}));


server.listen(4000,() =>{
console.log('listening on port 4000');
});

最佳答案

我正在做相同的教程,但遇到了相同的错误。我现在才开始工作。对我来说,我将 endpointURL 拼写为 endpointUrl 因为在驼峰式大小写中大写首字母缩略词对我来说似乎很愚蠢,但当然这是一个特定的键,它必须是拼写正确。

对于你来说,我在代码中看到的唯一区别是我认为你应该将 {schema} 传递给 graphqlExpress,但你只传递 模式。所以这就是我的做法:

server.use("/graphql", bodyParser.json(), graphqlExpress({ schema }))

关于express-graphql - 无法读取 Object.renderGraphiQL 中未定义的属性 'startsWith',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50172153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com