【发布时间】: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