What is this new syntax gql`string`

ⅰ亾dé卋堺 提交于 2019-11-28 07:20:31

问题


const GET_DOGS = gql`
  {
    dogs {
      id
      breed
    }
  }
`;

I found this new syntax from here.

Can you explain this syntax? Where can I find detail about it?


回答1:


It's called a tagged template. Template literals (`...`) can be prefixed with a function name. Upon evaluation, this function will be called and the static and dynamic parts of the template literal are passed to the function. Example:

function foo(staticParts, dynamicParts) {
  console.log(staticParts, dynamicParts);
}

foo`this is a ${42} test`

Tagged templates can be used to create domain specific languages, such as in this example.

There are many questions around tagged templates you can learn more from.




回答2:


It's called a template literal.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Not only does it allow you to do multi-line formatting like your example above, but it also makes it easier to mix expressions in with string content.




回答3:


That's GraphQL syntax. A json object is modelled between the backticks and data matching that structure is returned from GraphQL.



来源:https://stackoverflow.com/questions/50180381/what-is-this-new-syntax-gqlstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!