[英]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.