You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apollo Server v4中导入gql以创建类型定义?

Apollo Server v4 中正确导入gql的方法

Apollo Server v4 已将gql标签从核心包@apollo/server中拆分,需通过独立包graphql-tag来使用:

  1. 安装依赖
    执行以下命令安装graphql-tag:
npm install graphql-tag
# 或使用yarn
yarn add graphql-tag
  1. 正确导入并使用
    替换原导入语句,直接从graphql-tag导入gql:
import gql from 'graphql-tag';

const typeDefs = gql`
  # 这里编写你的类型定义
  type Query {
    hello: String!
  }
`;

若使用TypeScript,无需额外配置类型,graphql-tag已自带完整的类型声明。

内容的提问来源于stack exchange,提问作者Bikrant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 07:05:31