【问题标题】:schema.graphql file can't be found (node graphql-yoga)找不到 schema.graphql 文件(节点 graphql-yoga)
【发布时间】:2019-06-28 15:42:07
【问题描述】:

我正在关注如何使用 graphql 教程,我将在其中设置一个简单的 graphql 服务器。

index.js

const { GraphQLServer } = require('graphql-yoga');

// 1
let links = [{
  id: 'link-0',
  url: 'www.howtographql.com',
  description: 'Fullstack tutorial for GraphQL'
}];

const resolvers = {
  Query: {
    info: () => `This is the API of a Hackernews Clone`,
    // 2
    feed: () => links,
  },
  // 3
  Link: {
    id: (parent) => parent.id,
    description: (parent) => parent.description,
    url: (parent) => parent.url,
  }
};

// 3
const server = new GraphQLServer({
  typeDefs:'./schema.graphql',
  resolvers,
});

server.start(() => console.log(`Server is running on http://localhost:4000`));

如您所见,我在创建 GraphQLServer 时引用了我的架构文件。但是,当我运行服务器时,出现以下错误:

/Users/BorisGrunwald/Desktop/programmering/Javascript/GraphQL/hackernews-node/node_modules/graphql-yoga/dist/index.js:418
                throw new Error("No schema found for path: " + schemaPath);
            ^

我的文件结构:

谁能发现错误?

【问题讨论】:

    标签: javascript node.js graphql


    【解决方案1】:

    您提供了路径./schema.graphql,这使得node 在您运行它的目录中查找文件,而不是'src/schema.graphql' 的正确位置。

    所以你需要将路径更改为:

    //...
    typeDefs: 'src/schema.graphql',
    //...
    

    【讨论】:

    • 很抱歉,但我不确定我是否理解。我正在使用 Node,所以我不能使用 es6 import 对吗?对 Node 来说很新。
    • 实际上 Node 8.5.0+ 支持import,但实际上我发现了一些东西,我的答案比它应该的要复杂得多。我编辑了它。
    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2020-05-22
    • 2019-11-07
    • 2019-06-11
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    相关资源
    最近更新 更多