graphql-client/guides/heredoc.md

44 строки
815 B
Markdown
Исходник Постоянная ссылка Обычный вид История

2016-11-04 21:54:00 +03:00
# Heredoc style
Prefer quoted heredoc style when defining GraphQL query strings.
2018-05-02 23:02:50 +03:00
```ruby
2016-11-04 21:54:00 +03:00
# good
FooQuery = <<-'GRAPHQL'
{ version }
GRAPHQL
```
2018-05-02 23:02:50 +03:00
```ruby
2016-11-04 21:54:00 +03:00
# bad
FooQuery = <<-GRAPHQL
{ version }
GRAPHQL
```
Using a single quoted heredoc disables interpolation. GraphQL queries should not be constructed via string concatenate, especially at runtime. Interpolating user values into a query may lead to a "GraphQL injection" security vulnerability. Pass `variables:` instead of string interpolation.
2018-05-02 23:02:50 +03:00
```ruby
2016-11-04 21:54:00 +03:00
# good
FooQuery = <<-'GRAPHQL'
query($id: ID!) {
node(id: $id) {
}
}
GRAPHQL
query(FooQuery, variables: { id: id })
```
2018-05-02 23:02:50 +03:00
```ruby
2016-11-04 21:54:00 +03:00
# bad
FooQuery = <<-GRAPHQL
query {
node(id: "#{id}") {
}
}
GRAPHQL
query(FooQuery)
```
Bonus: Quoted heredocs syntax highlight look better in Atom.