我无法在 typeDefs 中导入 schema.graphql 文件:无法为以下指针找到任何 GraphQL 类型定义:
I'm unable to import schema.graphql file in typeDefs : Unable to find any GraphQL type definitions for the following pointers:
我正在使用 apollo-server-express 通过 GraphQL 创建后端 API
现在,我想在一个单独的文件中编写 GraphQL 模式。例如"schema.graphql", 所以当我把之前在Template String 中写的相同的代码。进入“schema.graphql”我的应用程序因以下错误而崩溃:
Unable to find any GraphQL type definitions for the following pointers
这是我的代码:
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 };
不仅如此,我还尝试了这个解决方案 -
但这根本行不通
对我来说很好用。包版本:
"apollo-server-express": "^2.12.0",
"graphql-import": "^0.7.1",
示例:
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
:
type Query {
greeting: String
}
resolver.js
:
const Query = {
greeting: () => 'Hello World From NightDevs',
};
module.exports = { Query };
通过 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
得到结果:
{"data":{"greeting":"Hello World From NightDevs"}}
我正在使用 apollo-server-express 通过 GraphQL 创建后端 API
现在,我想在一个单独的文件中编写 GraphQL 模式。例如"schema.graphql", 所以当我把之前在Template String 中写的相同的代码。进入“schema.graphql”我的应用程序因以下错误而崩溃:
Unable to find any GraphQL type definitions for the following pointers
这是我的代码:
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 };
不仅如此,我还尝试了这个解决方案 -
但这根本行不通
对我来说很好用。包版本:
"apollo-server-express": "^2.12.0",
"graphql-import": "^0.7.1",
示例:
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
:
type Query {
greeting: String
}
resolver.js
:
const Query = {
greeting: () => 'Hello World From NightDevs',
};
module.exports = { Query };
通过 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
得到结果:
{"data":{"greeting":"Hello World From NightDevs"}}