2

我正在阅读 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);

解决GraphiQLString.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);
  }

服务器.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');
 });
4

2 回答 2

6

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

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

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

于 2018-05-17T18:39:55.157 回答
-1

您的代码不正确。为了定义 graphiql 端点,您需要通过以下方式执行此操作:

const server = express();
server.use('/graphql', bodyParser.json(), graphqlExpress(schema));
server.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));

请记住,您应该将真实 graphql 端点的 endpointURL 传递给 graphiqlExpress 方法。

干杯!

于 2018-05-05T11:11:30.243 回答