无法读取属性' startsWith'在Object.renderGraphiQL

时间:2018-05-04 09:53:52

标签: express-graphql graphiql

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

2 个答案:

答案 0 :(得分:3)

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

对于您而言,我在代码中看到的唯一区别是我认为您应该将{schema}传递给graphqlExpress,但您只是通过了schema。所以,我有这样的方式:

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

答案 1 :(得分:-1)

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

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

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

干杯!