带Fastify的Apollo Graphql [英] Apollo Graphql with Fastify
问题描述
我一直在尝试使用Fastify运行Apollo Ghraphql服务器.索引文件的代码段如下:-
I have been trying to run Apollo Ghraphql server with Fastify. Code snippet for index file below:-
const { ApolloServer } = require('apollo-server-fastify');
const fastify = require('fastify')({ logger: true })
const config = require('./config')
const { typeDefs, resolvers } = require('./schema');
const server = new ApolloServer({
typeDefs,
resolvers
});
fastify.register(server.createHandler());
(async function () {
await fastify.listen(config.port, config.host, (err) => {
if(err) {
console.log(err)
process.exit(1)
} else {
console.log(`API server listening on port ${config.port} and host ${config.host}`)
}
})
})();
我的模式就像
const { gql } = require("apollo-server");
const typeDefs = gql`
type Query {
items: [Item!]!
item: Item,
hello: String!
}
type Item {
id: ID!
name: String!
message: String!
}
`;
const resolvers = {
Query: {
hello: () => "Hello World",
items: () => {
return {
id: 1,
name: 'test',
message: 'test111'
}
},
}
};
module.exports = {
typeDefs,
resolvers
};
当我启动服务器并尝试访问http://localhost:3000/graphql时,我收到消息缺少查询".在屏幕上.
When I start the server and try to access http://localhost:3000/graphql I am getting message "GET query missing." on the screen.
我是指-- https://www.npmjs.com/package/apollo-server-fastify .我错过任何一步了吗?请帮忙.
I am referring this - https://www.npmjs.com/package/apollo-server-fastify. Am I missing any step. Please help.
推荐答案
现在,fastify v3已于几周前发布,并且并非所有生态系统都已升级.
Right now fastify v3 has been released few weeks ago and not all the ecosystem has been upgraded as well.
因此您需要安装fastify v2 npm i fastify@2
,因为目前最新的是v3.
So you need to install fastify v2 npm i fastify@2
since right now the latest is v3.
这篇关于带Fastify的Apollo Graphql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!