简体   繁体   English

React 应用程序中的 Apollo GraphQL 查询

[英]Apollo GraphQL Queries in React Apps

Hey I am just starting to learn GraphQL for React, and Full Stack Applications and looking at Apollo and wondering why in my React.js I would right a query in graphqls query language instead of the json format?嘿,我刚刚开始学习 GraphQL 用于 React 和全栈应用程序并查看 Apollo 并想知道为什么在我的 React.js 中我会用 graphqls 查询语言而不是 json 格式来正确查询?

Example Apollo React wants Apollo React 想要的示例

query: gql`
      query GetRates {
        rates(currency: "USD") {
          currency
        }
      }
    `

but on the server, you can write但在服务器上,你可以写

{
  books {
    title
    author
  }
}

Why can't I write the second in my React application?为什么我不能在我的 React 应用程序中写第二个? Am I missing something, is there a better React GraphQL client to use?我错过了什么吗,有没有更好的 React GraphQL 客户端可以使用?

See GraphQL docs .请参阅GraphQL 文档

The following is a shorthand where you omit both the query keyword and the query name:以下是省略查询关键字和查询名称的简写:

{
  books {
    title
    author
  }
}

It is the same as:它与以下内容相同:

query getBooks {
  books {
    title
    author
  }
}

gql from apollo-client employs additional logic to parse GraphQL strings.来自apollo-client gql使用额外的逻辑来解析 GraphQL 字符串。 Think about query parameters for example.例如,考虑查询参数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM