Add new GraphQL types, schema (with fields) & base mutation

The current consul GraphQL API has two problems.

1) It uses some unnecessary complicated magic to automatically create
   the GraphQL types and querys using an `api.yml` file. This approach
   is over-engineered, complex and has no benefits. It's just harder to
   understand the code for people which are not familiar with the
   project (like me, lol).

2) It uses a deprecated DSL [1] that is soon going to be removed from
   `graphql-ruby` completely. We are already seeing deprecation warning
   because of this (see References).

There was one problem. I wanted to create the API so that it is fully
backwards compatible with the old one, BUT the old one uses field names
which are directly derived from the ruby code, which results in
snake_case field names - not the GraphQL way. When I'm using the
graphql-ruby Class-based syntax, it automatically creates the fields in
camelCase, which breaks backwards-compatibility.

So I've added deprecated snake_case field names to keep it
backwards-compatible.

[1] https://graphql-ruby.org/schema/class_based_api.html
This commit is contained in:
Finn Heemeyer
2022-01-05 13:23:11 +01:00
committed by Javi Martín
parent 5c6ab81c38
commit c984e666ff
24 changed files with 315 additions and 363 deletions

View File

@@ -1,12 +1,5 @@
require "rails_helper"
api_types = GraphQL::ApiTypesCreator.create
query_type = GraphQL::QueryTypeCreator.create(api_types)
ConsulSchema = GraphQL::Schema.define do
query query_type
max_depth 12
end
def execute(query_string, context = {}, variables = {})
ConsulSchema.execute(query_string, context: context, variables: variables)
end
@@ -40,8 +33,8 @@ describe "Consul Schema" do
let(:proposal) { create(:proposal, author: user) }
it "returns fields of Int type" do
response = execute("{ proposal(id: #{proposal.id}) { id } }")
expect(dig(response, "data.proposal.id")).to eq(proposal.id)
response = execute("{ proposal(id: #{proposal.id}) { cached_votes_up } }")
expect(dig(response, "data.proposal.cached_votes_up")).to eq(proposal.cached_votes_up)
end
it "returns fields of String type" do
@@ -373,6 +366,17 @@ describe "Consul Schema" do
expect(received_comments).not_to include(not_public_poll_comment.body)
end
it "only links public comments" do
user = create(:administrator).user
create(:comment, author: user, body: "Public")
create(:budget_investment_comment, author: user, valuation: true, body: "Valuation")
response = execute("{ user(id: #{user.id}) { public_comments { edges { node { body } } } } }")
received_comments = dig(response, "data.user.public_comments.edges")
expect(received_comments).to eq [{ "node" => { "body" => "Public" }}]
end
it "only returns date and hour for created_at" do
created_at = Time.zone.parse("2017-12-31 9:30:15")
create(:comment, created_at: created_at)