简体   繁体   English

Apollo 服务器中的 Typescript 语法

[英]Typescript Syntax in Apollo server

As you know in appollo server you can define the server's schema by passing a string into gql.正如您在 apollo 服务器中所知道的,您可以通过将字符串传递给 gql 来定义服务器的模式。

const typeDefs = gql`
type Query {
  getBtcRates: [BtcRate]
}
`'

But what is gql?但是什么是gql? Is it a function or a method?是function还是方法?

It's definition是定义

export const gql: (
  template: TemplateStringsArray | string,
  ...substitutions: any[]
) => DocumentNode = gqlTag;

To me it looks more like a function, but this syntax is unknown to me, so wonder what exactly it is and why this is written this way.对我来说,它看起来更像是 function,但我不知道这种语法,所以想知道它到底是什么以及为什么这样写。

gql is using syntax called tagged templates and is not TypeScript specific. gql使用称为标记模板的语法,并且不是 TypeScript 特定的。 For another example, styled-components also relies on this syntax.再举一个例子, styled-components也依赖于这种语法。

From the docs:从文档:

Tags allow you to parse template literals with a function.标签允许您使用 function 解析模板文字。 The first argument of a tag function contains an array of string values.标签 function 的第一个参数包含一个字符串值数组。 The remaining arguments are related to the expressions.其余的 arguments 与表达式有关。

A basic example of how this works:这是如何工作的一个基本示例:

 var variable = 'world'; function myTag(strings, exp) { var str0 = strings[0]; // "Hello " var str1 = strings[1]; // ";" return `${str0}${exp}${str1}`; } var output = myTag`Hello ${ variable }.`; console.log(output); // Hello world!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apollo GraphQL 服务器 + 打字稿 - Apollo GraphQL Server + TypeScript apollo-server-express、apollo-server 和 apollo-cache-control 中的 TypeScript 导入问题 - TypeScript import issues in apollo-server-express, apollo-server & apollo-cache-control 如何在 Firestore Cloud Functions 上使用 Apollo 服务器 GraphQL 处理 TypeScript 中的身份验证 - How to handle authentication in TypeScript with Apollo Server GraphQL on Firestore Cloud Functions 在 VS Code 中调试 Typescript Apollo Server (graphQL) 时未绑定断点 - Breakpoint not bound when Debugging Typescript Apollo Server (graphQL) in VS Code 在 nestjs 应用程序中使用新的 apollo 服务器版本时出现打字稿错误 - Typescript error using new apollo server version in nestjs application 使用 SuperTest 和 Jest 测试 TypeScript Apollo 服务器:'request.post' 不是 function - Using SuperTest and Jest to test TypeScript Apollo Server: 'request.post' is not a function 使用 TypeScript 在 Apollo 服务器中配置 REST 数据源时,对“任何”类型成员的不安全访问 - Unsafe access to member with 'any' type when configuring REST data source in Apollo server with TypeScript React Apollo查询/变异打字稿 - React Apollo Query/Mutation Typescript 如何在typescript + apollo中输入变异 - How to type a mutation in typescript + apollo 打字稿语法 - Typescript syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM