创建一个空白项目,初始化
yarn init
安装依赖
yarn add apollo-server-koa graphql koa
创建index.js
const { ApolloServer, gql } = require('apollo-server-koa');
const Koa = require('koa');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'world'
}
}
const server = new ApolloServer({ typeDefs, resolvers });
const app = new Koa();
server.applyMiddleware({ app });
app.listen({port: 3010}, () => {
console.log(` Server ready at http://localhost:3010${server.graphqlPath}`);
});
运行
node ./index.js