diff --git a/doc/api/examples/ruby/example_1.rb b/doc/api/examples/ruby/example_1.rb index eecbc4bc5..03cc1d41f 100644 --- a/doc/api/examples/ruby/example_1.rb +++ b/doc/api/examples/ruby/example_1.rb @@ -4,7 +4,7 @@ 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(" ")) + uri.query = URI.encode_www_form(query: query_string) request = Net::HTTP::Get.new(uri) request[:accept] = "application/json" diff --git a/doc/api/examples/ruby/example_2.rb b/doc/api/examples/ruby/example_2.rb index d0349e911..22b3e19d3 100644 --- a/doc/api/examples/ruby/example_2.rb +++ b/doc/api/examples/ruby/example_2.rb @@ -5,7 +5,7 @@ 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(" ")) + uri.query = URI.encode_www_form(query: query_string) request = Net::HTTP::Get.new(uri) request[:accept] = "application/json" diff --git a/docs/en/features/graphql.md b/docs/en/features/graphql.md index c28d9deab..2496dfedf 100644 --- a/docs/en/features/graphql.md +++ b/docs/en/features/graphql.md @@ -14,8 +14,8 @@ * [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) ## Characteristics @@ -30,11 +30,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 +56,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 +77,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 +87,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 +160,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 +190,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 +206,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 +243,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 +289,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 +297,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 +338,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 +372,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 +410,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" } } } diff --git a/docs/es/features/graphql.md b/docs/es/features/graphql.md index d4bb79a66..95d08e28d 100644 --- a/docs/es/features/graphql.md +++ b/docs/es/features/graphql.md @@ -22,19 +22,19 @@ * API de sólo lectura * Acceso público, sin autenticación -* Usa GraphQL por debajo - * El tamaño máximo (y por defecto) de página está establecido a 25 - * La profundiad máxima de las consultas es de 8 niveles +* Usa GraphQL: + * El tamaño máximo (y por defecto) de registros por página es 25 + * La profundidad máxima de las consultas es de 8 niveles * Como máximo se pueden solicitar 2 colecciones en una misma consulta * Soporte para peticiones GET (consulta dentro del *query string*) y POST (consulta dentro del *body*, como `application/json` o `application/graphql`). ## GraphQL -La API de Consul Democracy utiliza GraphQL [http://graphql.org](https://graphql.org), en concreto la [implementación en Ruby](http://graphql-ruby.org/). Si no estás familiarizado con este tipo de APIs, es recomendable investigar un poco sobre GraphQL previamente. +La API de Consul Democracy utiliza [GraphQL](https://graphql.org), en concreto la [implementación en Ruby](http://graphql-ruby.org/). Si no estás familiarizado con este tipo de APIs, te recomendamos consultar la [documentación oficial de GraphQL](https://graphql.org/learn/). -Una de las caracteríticas que diferencian una API REST de una GraphQL es que con esta última es posible construir *consultas personalizadas*, de forma que el servidor nos devuelva únicamente la información en la que estamos interesados. +Una de las características que diferencian una API REST de una GraphQL es que con esta última es posible construir *consultas personalizadas*, de forma que el servidor nos devuelva únicamente la información en la que estamos interesados. -Las consultas en GraphQL están escritas siguiendo un estándar que presenta ciertas similitudes con el formato JSON, por ejemplo: +Las consultas en GraphQL están escritas siguiendo un formato que presenta ciertas similitudes con el formato JSON, por ejemplo: ```graphql { @@ -56,10 +56,10 @@ Las respuestas son en formato JSON: "data": { "proposal": { "id": 1, - "title": "Hacer las calles del centro de Madrid peatonales", + "title": "Aumentar la cantidad de zonas verdes", "public_author": { "id": 2, - "username": "electrocronopio" + "username": "unmundomejor" } } } @@ -79,7 +79,7 @@ Siguiendo las [directrices oficiales](http://graphql.org/learn/serving-over-http Al ser una API que funciona a través de HTTP, cualquier herramienta capaz de realizar este tipo de peticiones resulta válida. -Esta sección contiene unos pequeños ejemplos sobre cómo hacer las peticiones a través de: +Esta sección contiene algunos ejemplos sobre cómo hacer las peticiones a través de: * GraphiQL * Extensiones de Chrome como Postman @@ -87,9 +87,9 @@ Esta sección contiene unos pequeños ejemplos sobre cómo hacer las peticiones #### GraphiQL -[GraphiQL](https://github.com/graphql/graphiql) es una interfaz de navegador para realizar consultas a una API GraphQL, así como una fuente adicional de documentación. Está desplegada en la ruta `/graphiql` y es la mejor forma de familiarizarse una API basada en GraphQL. +[GraphiQL](https://github.com/graphql/graphiql) es una interfaz de navegador para realizar consultas a una API GraphQL, así como una fuente adicional de documentación. Consul Democracy utiliza la gema [graphiql-rails](https://github.com/rmosolgo/graphiql-rails) para acceder a esta interfaz en la ruta `/graphiql`; esta es la mejor forma de familiarizarse con una API basada en GraphQL. - + Tiene tres paneles principales: @@ -101,27 +101,27 @@ Tiene tres paneles principales: Ejemplo de petición `GET`, con la consulta como parte del *query string*: - + Ejemplo de petición `POST`, con la consulta como parte del *body* y codificada como `application/json`: - + La consulta debe estar ubicada en un documento JSON válido, como valor de la clave `"query"`: - +