-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to #1119 Example queries: ```GraphQL query labels { labels { edges { node { id name color files { edges { node { id } } } } } } } mutation AttachLabel { labelFile(input: {fileId: "27", labelId: "1"}) { file { id labels { edges { node { id } } } } } } mutation CreateLabel { createLabel(input: {name: "PLA", color: "#111"}) { label { id name color } } } query file { file(id: "28") { labels { edges { node { id name } } } } } ```
- Loading branch information
1 parent
d77de99
commit 326548c
Showing
8 changed files
with
251 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Files | ||
class CreateLabel < Base::Mutation | ||
description 'Creates a label' | ||
|
||
argument :name, String, required: true | ||
argument :color, String, required: true | ||
|
||
field :label, Types::Label, null: true | ||
|
||
def resolve(name:, color:) | ||
label = ::Label.new(name:, color:) | ||
|
||
if label.save | ||
{ label: } | ||
else | ||
{ label: nil } | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mutations | ||
module Files | ||
class LabelFile < Base::Mutation | ||
description 'Attaches a label to a file' | ||
|
||
argument :file_id, ID, required: true | ||
argument :label_id, ID, required: true | ||
|
||
field :label, Types::Label, null: true | ||
field :file, Types::File, null: true | ||
|
||
def resolve(file_id:, label_id:) | ||
label = ::Label.find(label_id) | ||
file = ::FileRecord.find(file_id) | ||
|
||
file.labels << label if file.present? && label.present? | ||
|
||
file.save! | ||
{ label:, file: } | ||
rescue StandardError | ||
{ label:, file: } | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
class Label < Base::Object | ||
field :id, ID, null: false | ||
field :name, String, null: false | ||
field :color, String, null: false | ||
field :files, File.connection_type, method: :file_records, null: false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
module Mutations | ||
module Files | ||
module CreateLabelSpec | ||
include ActiveJob::TestHelper | ||
|
||
RSpec.describe CreateLabel, type: :request do | ||
let(:variables) do | ||
{ | ||
input: { | ||
name: 'PETG', | ||
color: '#ff0000' | ||
} | ||
} | ||
end | ||
|
||
describe '.resolve' do | ||
it 'creates a label' do | ||
expect do | ||
post '/graphql', params: { query:, variables: } | ||
end.to change { Label.count }.by(1) | ||
end | ||
|
||
it 'returns the label' do | ||
post '/graphql', params: { query:, variables: } | ||
json = JSON.parse(response.body) | ||
data = json['data']['createLabel'] | ||
|
||
expect(data['label']).to include( | ||
'id' => be_present, | ||
'name' => 'PETG', | ||
'color' => '#ff0000' | ||
) | ||
end | ||
|
||
context 'with invalid arguments' do | ||
let(:variables) do | ||
{ | ||
input: { | ||
name: 'PETG', | ||
color: 'blue' | ||
} | ||
} | ||
end | ||
it 'does not create a label' do | ||
expect do | ||
post '/graphql', params: { query:, variables: } | ||
end.to change { Label.count }.by(0) | ||
end | ||
|
||
it 'returns a nil label' do | ||
post '/graphql', params: { query:, variables: } | ||
json = JSON.parse(response.body) | ||
data = json['data']['createLabel'] | ||
|
||
expect(data['label']).to be_nil | ||
end | ||
end | ||
end | ||
|
||
def query | ||
<<~GQL | ||
mutation CreateLabel($input: CreateLabelInput!) { | ||
createLabel(input: $input) { | ||
label { | ||
id | ||
name | ||
color | ||
} | ||
} | ||
} | ||
GQL | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
module Mutations | ||
module Files | ||
module LabelFileSpec | ||
include ActiveJob::TestHelper | ||
|
||
RSpec.describe LabelFile, type: :request do | ||
let(:file) { create(:file_record) } | ||
let(:label) { create(:label) } | ||
let(:variables) do | ||
{ | ||
input: { | ||
fileId: file.id, | ||
labelId: label.id | ||
} | ||
} | ||
end | ||
|
||
describe '.resolve' do | ||
it 'attaches the label to the file' do | ||
expect do | ||
post '/graphql', params: { query:, variables: } | ||
end.to change { file.labels.count }.by(1) | ||
end | ||
|
||
it 'returns the label and the file' do | ||
post '/graphql', params: { query:, variables: } | ||
json = JSON.parse(response.body) | ||
data = json['data']['labelFile'] | ||
|
||
expect(data['label']).to include( | ||
'id' => label.id.to_s | ||
) | ||
expect(data['file']).to include( | ||
'id' => file.id.to_s | ||
) | ||
end | ||
|
||
context 'with invalid arguments' do | ||
let(:variables) do | ||
{ | ||
input: { | ||
fileId: file.id, | ||
labelId: '' | ||
} | ||
} | ||
end | ||
it 'does not create a label' do | ||
expect do | ||
post '/graphql', params: { query:, variables: } | ||
end.to change { file.labels.count }.by(0) | ||
end | ||
|
||
it 'returns a nil label' do | ||
post '/graphql', params: { query:, variables: } | ||
json = JSON.parse(response.body) | ||
data = json['data']['labelFile'] | ||
|
||
expect(data['label']).to be_nil | ||
end | ||
|
||
it 'returns a nil file' do | ||
post '/graphql', params: { query:, variables: } | ||
json = JSON.parse(response.body) | ||
data = json['data']['labelFile'] | ||
|
||
expect(data['file']).to be_nil | ||
end | ||
end | ||
end | ||
|
||
def query | ||
<<~GQL | ||
mutation LabelFile($input: LabelFileInput!) { | ||
labelFile(input: $input) { | ||
file { | ||
id | ||
} | ||
label { | ||
id | ||
} | ||
} | ||
} | ||
GQL | ||
end | ||
end | ||
end | ||
end | ||
end |