diff --git a/doc/api/examples/ruby/example_1.rb b/doc/api/examples/ruby/example_1.rb deleted file mode 100644 index eecbc4bc5..000000000 --- a/doc/api/examples/ruby/example_1.rb +++ /dev/null @@ -1,29 +0,0 @@ -require "net/http" - -API_ENDPOINT = "https://demo.consuldemocracy.org/graphql".freeze - -def make_request(query_string) - uri = URI(API_ENDPOINT) - uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" ")) - request = Net::HTTP::Get.new(uri) - request[:accept] = "application/json" - - Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https| - https.request(request) - end -end - -query = <<-GRAPHQL - { - proposal(id: 1) { - id, - title, - public_created_at - } - } -GRAPHQL - -response = make_request(query) - -puts "Response code: #{response.code}" -puts "Response body: #{response.body}" diff --git a/doc/api/examples/ruby/example_2.rb b/doc/api/examples/ruby/example_2.rb deleted file mode 100644 index d0349e911..000000000 --- a/doc/api/examples/ruby/example_2.rb +++ /dev/null @@ -1,68 +0,0 @@ -require "net/http" -require "json" - -API_ENDPOINT = "https://demo.consuldemocracy.org/graphql".freeze - -def make_request(query_string) - uri = URI(API_ENDPOINT) - uri.query = URI.encode_www_form(query: query_string.delete("\n").delete(" ")) - request = Net::HTTP::Get.new(uri) - request[:accept] = "application/json" - - Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https| - https.request(request) - end -end - -def build_query(options = {}) - page_size = options[:page_size] || 25 - page_size_parameter = "first: #{page_size}" - - page_number = options[:page_number] || 0 - after_parameter = page_number.positive? ? ", after: \"#{options[:next_cursor]}\"" : "" - - <<-GRAPHQL - { - proposals(#{page_size_parameter}#{after_parameter}) { - pageInfo { - endCursor, - hasNextPage - }, - edges { - node { - id, - title, - public_created_at - } - } - } - } - GRAPHQL -end - -page_number = 0 -next_cursor = nil -proposals = [] - -loop do - puts "> Requesting page #{page_number}" - - query = build_query(page_size: 25, page_number: page_number, next_cursor: next_cursor) - response = make_request(query) - - response_hash = JSON.parse(response.body) - page_info = response_hash["data"]["proposals"]["pageInfo"] - has_next_page = page_info["hasNextPage"] - next_cursor = page_info["endCursor"] - proposal_edges = response_hash["data"]["proposals"]["edges"] - - puts "\tHTTP code: #{response.code}" - - proposal_edges.each do |edge| - proposals << edge["node"] - end - - page_number += 1 - - break unless has_next_page -end diff --git a/docs/en/features/graphql.md b/docs/en/features/graphql.md index c28d9deab..01614abaa 100644 --- a/docs/en/features/graphql.md +++ b/docs/en/features/graphql.md @@ -14,9 +14,11 @@ * [Pagination](#pagination) * [Accessing several resources in a single request](#accessing-several-resources-in-a-single-request) * [Security limitations](#security-limitations) - * [Example of too deep query](#example-of-too-deep-query) - * [Example of too complex query](#example-of-too-complex-query) + * [Example of a query which is too deep](#example-of-a-query-which-is-too-deep) + * [Example of a query which is too complex](#example-of-a-query-which-is-too-complex) * [Code examples](#code-examples) + * [Simple example](#simple-example) + * [Example with pagination](#example-with-pagination) ## Characteristics @@ -30,11 +32,11 @@ ## GraphQL -The Consul Democracy API uses GraphQL [http://graphql.org](http://graphql.org), the [Ruby implementation](http://graphql-ruby.org/), to be specific. If you're not familiar with this kind of APIs, it's recommended to make some research about GraphQL before. +The Consul Democracy API uses [GraphQL](http://graphql.org), specifically the [Ruby implementation](http://graphql-ruby.org/). If you're not familiar with this kind of APIs, we recommended you to check the [GraphQL official documentation](https://graphql.org/learn/). -One of the characteristics that differentiates a REST API from a GraphQL one is that with the last one it's possible for the client to build its own *custom queries*, so the server will only return information in which we're interested. +One of the characteristics that differentiates a REST API from a GraphQL one is that with the latter one it's possible for the client to build its own *custom queries*, so the server will only return information in which we're interested. -GraphQL queries are written following a standard which resembles to JSON, for example: +GraphQL queries are written following a format which resembles JSON. For example: ```graphql { @@ -56,10 +58,10 @@ Responses are formatted in JSON: "data": { "proposal": { "id": 1, - "title": "Hacer las calles del centro de Madrid peatonales", + "title": "Increase the amount of green spaces", "public_author": { "id": 2, - "username": "electrocronopio" + "username": "abetterworld" } } } @@ -77,7 +79,7 @@ Following [the official recommendations](http://graphql.org/learn/serving-over-h ### Supported clients -Because it's an API that works through HTTP, any tool capable of making this kind of requests is capable of querying the API. +Since this is an API that works through HTTP, any tool capable of making this kind of requests is capable of querying the API. This section presents a few examples about how to make requests using: @@ -87,41 +89,41 @@ This section presents a few examples about how to make requests using: #### GraphiQL -[GraphiQL](https://github.com/graphql/graphiql) is a browser interface for making queries against a GraphQL API. It's also an additional source of documentation. It's deployed in the route `/graphiql` and it's the best way to get familiar with GraphQL-based APIs. +[GraphiQL](https://github.com/graphql/graphiql) is a browser interface for making queries against a GraphQL API. It's also an additional source of documentation. Consul Democracy uses the [graphiql-rails](https://github.com/rmosolgo/graphiql-rails) to access this interface at `/graphiql`; it's the best way to get familiar with GraphQL-based APIs. - + -It has three main panels: +It's got three main panels: * The left panel is used to write the query. * The central panel shows the result of the request. -* The right panel (occultable) shows a documentation autogenerated from the models and fields exposed in the API. +* The right panel (hideable) shows a documentation autogenerated from the models and fields exposed in the API. #### Postman -Example of `GET` request, with the query as part of the *query string*: +Here's an example of a `GET` request, with the query as part of the *query string*: - + -Example of `POST` request, with the query as part of the *body* and encoded as `application/json`: +And here's an example of a `POST` request, with the query as part of the *body* and encoded as `application/json`: - + The query must be located inside a valid JSON document, as the value of the `"query"` key: - + #### HTTP libraries -Sure you can use any HTTP library available for most programming languages. +You can use any of the HTTP libraries available for most programming languages. -**IMPORTANT**: Due to security protocols from the Madrid City Council servers, it's necessary to include a *User Agent* header from a web browser so the request is not rejected. For example: +**IMPORTANT**: Some servers might use security protocols that will make it necessary to include a *User Agent* header from a web browser so the request is not rejected. For example: -`User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36` +`User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0` ## Available information -The [config/api.yml](../../config/api.yml) file contains a complete list of all the models (and their attributes) which are currently being exposed in the API. +The `app/graphql/types/` folder contains a complete list of all the models (and their attributes) which are currently being exposed in the API. The models are the following: @@ -160,7 +162,7 @@ Response: "data": { "proposal": { "id": 2, - "title": "Crear una zona cercada para perros en Las Tablas", + "title": "Create a dog-friendly area near the beach", "comments_count": 10 } } @@ -190,12 +192,12 @@ Response: "edges": [ { "node": { - "title": "ELIMINACION DE ZONA APARCAMIENTO EXCLUSIVO FUNCIONARIOS EN MADRID" + "title": "Bus stop near the school" } }, { "node": { - "title": "iluminación de zonas deportivas" + "title": "Improve the lighting in the football stadium" } } ] @@ -206,7 +208,7 @@ Response: #### Pagination -The maximum (and default) number of records that each page contains is set to 25. For navigating through the different pages it's necessary to request also information relative to the `endCursor`: +The maximum (and default) number of records that each page contains is set to 25. In order to navigate through the different pages, it's necessary to request `endCursor` information: ```graphql { @@ -243,7 +245,7 @@ The response: } ``` -To retrieve the next page, you have to pass as a parameter the cursor received in the previous request, and so on: +To retrieve the next page, pass the cursor received in the previous request as a parameter: ```graphql { @@ -289,7 +291,7 @@ This query requests information about several models in a single request: `Propo ## Security limitations -Allowing a client to customize queries is a major risk factor. If too complex queries were allowed, it would be possible to perform a DoS attack against the server. +Allowing a client to customize queries is a major risk factor. If queries that are too complex were allowed, it would be possible to perform a DoS attack against the server. There are three main mechanisms to prevent such abuses: @@ -297,7 +299,7 @@ There are three main mechanisms to prevent such abuses: * Limit the maximum depth of the queries * Limit the amount of information that is possible to request in a query -### Example of too deep query +### Example of a query which is too deep The maximum depth of queries is currently set at 8. Deeper queries (such as the following) will be rejected: @@ -338,9 +340,9 @@ The response will look something like this: } ``` -### Example of too complex query +### Example of a query which is too complex -The main risk factor is when multiple collections of resources are requested in the same query. The maximum number of collections that can appear in the same query is limited to 2. The following query requests information from the `users`, `debates` and `proposals` collections, so it will be rejected: +The main risk factor is the option to request multiple collections of resources in the same query. The maximum number of collections that can appear in the same query is limited to 2. The following query requests information from the `users`, `debates` and `proposals` collections, so it will be rejected: ```graphql { @@ -372,12 +374,6 @@ The response will look something like this: ```json { "errors": [ - { - "message": "Query has complexity of 3008, which exceeds max complexity of 2500" - }, - { - "message": "Query has complexity of 3008, which exceeds max complexity of 2500" - }, { "message": "Query has complexity of 3008, which exceeds max complexity of 2500" } @@ -416,9 +412,9 @@ The response: "edges": [ { "node": { - "title": "Empadronamiento necesario para la admisión en GoFit Vallehermoso", + "title": "Make a discount to locals in sports centers", "geozone": { - "name": "Chamberí" + "name": "South area" } } } @@ -431,4 +427,113 @@ The response: ## Code examples -The [doc/api/examples](https://github.com/consuldemocracy/consuldemocracy/tree/master/doc/api/examples/ruby) directory contains examples of code to access the API. +### Simple example + +Here's a simple example of code accessing the Consul Democracy demo API: + +```ruby +require "net/http" + +API_ENDPOINT = "https://demo.consuldemocracy.org/graphql".freeze + +def make_request(query_string) + uri = URI(API_ENDPOINT) + uri.query = URI.encode_www_form(query: query_string) + request = Net::HTTP::Get.new(uri) + request[:accept] = "application/json" + + Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https| + https.request(request) + end +end + +query = <<-GRAPHQL + { + proposal(id: 1) { + id, + title, + public_created_at + } + } +GRAPHQL + +response = make_request(query) + +puts "Response code: #{response.code}" +puts "Response body: #{response.body}" +``` + +### Example with pagination + +And here is a more complex example using pagination, once again accessing the Consul Democracy demo API: + +```ruby +require "net/http" +require "json" + +API_ENDPOINT = "https://demo.consuldemocracy.org/graphql".freeze + +def make_request(query_string) + uri = URI(API_ENDPOINT) + uri.query = URI.encode_www_form(query: query_string) + request = Net::HTTP::Get.new(uri) + request[:accept] = "application/json" + + Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |https| + https.request(request) + end +end + +def build_query(options = {}) + page_size = options[:page_size] || 25 + page_size_parameter = "first: #{page_size}" + + page_number = options[:page_number] || 0 + after_parameter = page_number.positive? ? ", after: \"#{options[:next_cursor]}\"" : "" + + <<-GRAPHQL + { + proposals(#{page_size_parameter}#{after_parameter}) { + pageInfo { + endCursor, + hasNextPage + }, + edges { + node { + id, + title, + public_created_at + } + } + } + } + GRAPHQL +end + +page_number = 0 +next_cursor = nil +proposals = [] + +loop do + puts "> Requesting page #{page_number}" + + query = build_query(page_size: 25, page_number: page_number, next_cursor: next_cursor) + response = make_request(query) + + response_hash = JSON.parse(response.body) + page_info = response_hash["data"]["proposals"]["pageInfo"] + has_next_page = page_info["hasNextPage"] + next_cursor = page_info["endCursor"] + proposal_edges = response_hash["data"]["proposals"]["edges"] + + puts "\tHTTP code: #{response.code}" + + proposal_edges.each do |edge| + proposals << edge["node"] + end + + page_number += 1 + + break unless has_next_page +end +``` diff --git a/docs/es/features/graphql.md b/docs/es/features/graphql.md index d4bb79a66..dc0fcd151 100644 --- a/docs/es/features/graphql.md +++ b/docs/es/features/graphql.md @@ -17,24 +17,26 @@ * [Ejemplo de consulta demasiado profunda](#ejemplo-de-consulta-demasiado-profunda) * [Ejemplo de consulta demasiado compleja](#ejemplo-de-consulta-demasiado-compleja) * [Ejemplos de código](#ejemplos-de-codigo) + * [Ejemplo sencillo](#ejemplo-sencillo) + * [Ejemplo con paginación](#ejemplo-con-paginacion)