![](/img/trans.png)
[英]src/index.ts:12:22 - error TS2307: Cannot find module './schema.graphql' or its corresponding type declarations
[英]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。 例如“schema.graphql”,所以當我放入與之前在模板字符串中編寫的代碼相同的代碼時。 進入“schema.graphql”我的應用程序因以下錯誤而崩潰:
找不到以下指針的任何 GraphQL 類型定義
這是我的代碼:
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 };
不僅如此,我還嘗試過這個解決方案 - Stackoverflow 解決方案
但這根本不起作用
對我來說很好。 package 版本:
"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"}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.