Typescript 如何通过扩展从';节点模块';?
编辑:根据请求添加了Typescript 如何通过扩展从';节点模块';?,typescript,graphql,apollo,graphql-js,apollo-server,Typescript,Graphql,Apollo,Graphql Js,Apollo Server,编辑:根据请求添加了ARTICLES\u QUERY、tsconfig.json和package.json 编辑2:这是可行的,但似乎是一个丑陋的解决方案。如果有人有更好的,我会很感激的 export default class InterfaceGraphQLApi extends GraphQLDataSource { baseURL = "http://localhost:4545/admin/api"; query = super.query; 编辑3:@Max的解决方案解决
ARTICLES\u QUERY
、tsconfig.json
和package.json
编辑2:这是可行的,但似乎是一个丑陋的解决方案。如果有人有更好的,我会很感激的
export default class InterfaceGraphQLApi extends GraphQLDataSource {
baseURL = "http://localhost:4545/admin/api";
query = super.query;
编辑3:@Max的解决方案解决了查询字段的问题,但由于另一个“错误”,导致TS无法编译源代码:
文章查询
这里是
const ARTICLE_QUERY = gql`
query Article($id: ID!) {
Article(where: { id: $id }) {
title
text
video {
youtube {
... on OEmbedVideo {
html
type
}
}
}
podcast {
spotify {
... on OEmbedRich {
html
type
}
}
}
images {
file {
filename
}
}
}
}
`;
编辑4:Max编辑的解决方案工作正常
我对打字脚本比较陌生。我正在导入一个允许我为Apollo服务器项目定义GraphQL数据源的。在我的项目中,我扩展了这个库定义的类
import { GraphQLDataSource } from "apollo-datasource-graphql";
import { gql } from "apollo-server";
const ARTICLES_QUERY = gql`
query {
allArticles {
title
text
id
status
images {
file {
filename
}
}
}
}
`;
export default class InterfaceGraphQLApi extends GraphQLDataSource {
baseURL = "http://localhost:4545/admin/api";
async getArticles() {
try {
const response = await this.query(ARTICLES_QUERY);
然而,TypeScript抱怨这个问题。query
,说
query
方法来自导入的库的类。如何解决这个问题以使TypeScript满意
tsconfig.json是
{
"compilerOptions": {
"target": "ESNext",
"lib": [
"esnext",
"dom"
],
"skipLibCheck": true,
"outDir": "dist",
"strict": false,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
而package.json是
{
"dependencies": {
"apollo-datasource-graphql": "^1.3.2",
"apollo-server": "^2.10.1",
"graphql": "^14.6.0"
},
"devDependencies": {
"babel-eslint": "^10.0.3",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-prettier": "^3.1.2",
"pino": "^5.16.0",
"prettier": "^1.19.1",
"ramda": "^0.26.1",
"typescript": "^3.8.3"
},
"scripts": {
"dev": "tsc && node dist/index.js"
}
}
此外,我想知道是否有人能引导我了解Apollo Server和GraphQL所需的类型定义——我搜索了“Apollo Server类型定义”并找到了它,但它有许多依赖项,我还需要查找和下载。我想知道是否有类似于@types/apollo server
(当我做warn@types/apollo server
时,我得到了https://registry.yarnpkg.com/@类型%2Apollo服务器:未找到。
)
谢谢你提供的任何信息 阿波罗数据源图中有一个开放的PR: 同时,有一个解决方案可以帮助您。将您的导入更改为:
从“apollo数据源graphql”导入{GraphQLDataSource}代码>
到
import{GraphQLDataSource}来自“阿波罗数据源graphql/src”代码>
最后,我在研究中发现了一个有趣的部分。我在文档中找到了一个部分,从中可以将api定义为ressource而不是graphql服务器。在您提供的代码中实际上就是这样
此外,如果您有其他可能阻止tsc编译的问题,请在节点_模块下进入apollo datasource graphql文件夹并转到查询功能。选项参数有所需的类型。正如文档所说,您需要在选项中提供您作为第一个参数传递的查询作为如下查询
const response=wait this.query(ARTICLE\u query,{query:ARTICLE\u query,变量:{id})
您能将您的文章添加到您的问题中吗?@Max--添加了itdoesTypescript
在运行时投诉?gql来自“apollo server express”的import{gql}代码>?GraphQLDataSource来自'apollo datasource graphql'的import{GraphQLDataSource}代码>?并发布您的tsconfig和package.json将有助于回答您的问题。我已经创建了一个stackblitz,它编译解决了原始问题,但导致另一个问题,阻止TS编译。我对原始问题进行了编辑,以指出这一错误。感谢您提供指向这些文档的指针——据我所知,这些文档仅用于REST源代码,因此需要阿波罗数据源graphql
?@Cerulean:如果您有进一步的问题,请打开stackblitz或typescript游乐场,我可以帮您。您的第一个问题中的查询已得到回答,当您更改查询时,会出现另一个问题。或者打开一个github回购协议,我可以在那里帮助您。我已将答案更改为您上次编辑的答案。如果我需要进一步的信息,我将尝试上载一些内容。我将字段query
添加到options
中,效果非常好。然而,我现在得到了错误:当我尝试编译时,找不到模块'apollo datasource graphql/src'
,编译停止。它以前工作过,很奇怪。