从app.js
文件开始:
import { Server } from "./src/Server";
import { config } from "dotenv";
config();
Server.StartServer();
下面是Server.ts
引导 apollo-server-express 的文件
import express from "express";
import http from "http";
import { ApolloServer } from "apollo-server-express";
import { GraphQLServerOptions } from "apollo-server-core/src/graphqlOptions";
import { schema } from "./graphql/index";
export class Server {
public static StartServer() {
const app: express.Application = express();
const server = new ApolloServer({
schema,
graphiql: true,
} as unknown as GraphQLServerOptions);
server.applyMiddleware({ app });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen(process.env.PORT, function () {
console.log(`server is running on port ${process.env.PORT}`);
});
}
}
这是user.resolvers.ts
我的解析器放在这里的文件:
import { IResolvers } from "graphql-tools";
export const resolver: IResolvers = {
Mutation: {
register: function (parent, args, content, info) {
console.log(parent);
},
},
Query: {
getUser: function (parent, args, content, info) {
console.log(parent);
},
},
};
在这里,我们使用 inuser.schema.ts
文件的 typeDef:
import { gql } from "apollo-server-express";
export const typeDef = gql`
type User {
username: String
password: String
}
type Mutation {
register(input: {username: String!, passwprd: String!})
}
type Query {
getUSer(username): User
}
`;
最后在./src/graphql/index.ts
文件中我正在为解析器和 typeDefs 进行合并,我正在制作 executableSchema 以将其添加到 ApolloServer 配置对象,但我面临以下错误:
任何想法和建议将不胜感激。早期感谢贡献者:)