[英]I'm unable to import schema.graphql file in typeDefs : Unable to find any GraphQL type definitions for the following pointers:
I'm using apollo-server-express to create backend apis with GraphQL我正在使用 apollo-server-express 使用 GraphQL 创建后端 api
Now, I want to Write GraphQL Schema in a separate file.现在,我想在单独的文件中编写 GraphQL Schema。 eg "schema.graphql", So when I put the same code as I wrote in Template String before.
例如“schema.graphql”,所以当我放入与之前在模板字符串中编写的代码相同的代码时。 into the "schema.graphql" My application is crashed with below error:
进入“schema.graphql”我的应用程序因以下错误而崩溃:
Unable to find any GraphQL type definitions for the following pointers
找不到以下指针的任何 GraphQL 类型定义
Here is my code:这是我的代码:
server.js
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { importSchema } = require('graphql-import');
const fs = require('fs');
const path = '/graphql';
const apolloServer = new ApolloServer({
typeDefs: importSchema('./greet.graphql'),
resolvers: require('./graphql/resolver'),
});
const app = express();
apolloServer.applyMiddleware({ app, path });
app.listen(8080, () => {
console.log('Server Hosted');
});
greet.graphql
type Query {
greeting: String
}
resolver.js
const Query = {
greeting: () => 'Hello World From NightDevs',
};
module.exports = { Query };
Not only this, but Ive tried this solution also - Stackoverflow Solution不仅如此,我还尝试过这个解决方案 - Stackoverflow 解决方案
But This doesn't work at all但这根本不起作用
Works fine for me.对我来说很好。 package versions:
package 版本:
"apollo-server-express": "^2.12.0",
"graphql-import": "^0.7.1",
Example:例子:
server.js
: server.js
:
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { importSchema } = require('graphql-import');
const path = require('path');
const apolloServer = new ApolloServer({
typeDefs: importSchema(path.resolve(__dirname, './greet.graphql')),
resolvers: require('./resolver')
});
const app = express();
const graphqlEndpoint = '/graphql';
apolloServer.applyMiddleware({ app, path: graphqlEndpoint });
app.listen(8080, () => {
console.log('Server Hosted');
});
greet.graphql
: greet.graphql
:
type Query {
greeting: String
}
resolver.js
: resolver.js
:
const Query = {
greeting: () => 'Hello World From NightDevs',
};
module.exports = { Query };
Testing via curl
:通过
curl
进行测试:
curl 'http://localhost:8080/graphql' -H 'Accept-Encoding: gzip, deflate, br' -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Connection: keep-alive' -H 'DNT: 1' -H 'Origin: http://localhost:8080' --data-binary '{"query":"query {\n greeting\n}"}' --compressed
Get result:得到结果:
{"data":{"greeting":"Hello World From NightDevs"}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.