【问题标题】:How do I use schema.graphql file as typeDefs?如何使用 schema.graphql 文件作为 typeDefs?
【发布时间】:2022-07-08 00:49:15
【问题描述】:

我正在使用 Node.js 学习 GraphQL,我目前正在使用 graphql-yoga 作为 GraphQLServer。现在,我想分离类型定义和解析器,所以我使用了schema.graphql,在那里我定义了所有类型,但现在我不知道如何在 GraphQL Server 中将该文件用作 typeDefs。我在下面提供了我的文件。

index.js

const { createServer } = require('graphql-yoga');
const { resolvers } = require('./template1');

const server = createServer({
    schema: {
        resolvers,
        typeDefs: 'src/schema.graphql'
    }
});

server.start(() => {
    console.log('GraphQL Server started.');
});

schema.graphql

type Query {
  hello: String!
  posts: [Post!]!
  users: [User!]!
  comments: [Comment!]!
}

type Mutation {
  signUp(data: SignUpInput): User!
  createPost(data: CreatePostInput): Post!
  createComment(data: CreateCommentInput): Comment!
  deleteUser(id: ID!): User!
  deletePost(id: ID!): Post!
  deleteComment(id: ID!): Comment!
}

input SignUpInput {
  email:String!
  username: String!
  password: String!
}

input CreatePostInput {
  title: String!
  author: ID!
}

input CreateCommentInput {
  text: String!
  author: ID!
  post: ID!
}

type Post {
  id: ID!
  title: String!
  author: User!
  comments: [Comment!]
}

type User {
  id: ID!
  email: String!
  password: String!
  username: String!
  posts: [Post!]
  comments: [Comment!]
}

type Comment {
  id: ID!
  text: String!
  author: User!
  post: Post!
}

我收到此错误。

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    而不是直接传递架构使用

    typeDefs: fs.readFileSync( path.join(__dirname, 'schema.graphql'), 'utf8' )

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 2020-11-05
      • 2019-06-28
      • 2015-04-06
      • 2019-08-12
      • 2021-01-07
      • 2023-03-18
      • 2021-01-04
      • 2017-11-08
      相关资源
      最近更新 更多