add body to submission draft mutation in graphql

we need to allow setting a 'body' in the submission draft
mutation in graphql for text entry assignments

Test Plan:
* create an assignment as a teacher
* add a student to that assignment
* as a student, navigate to /graphiql
* run the following mutation
** set the submissionId to whatever your latest submissionId is
```
mutation {
  createSubmissionDraft(input: {submissionId: "1", attempt: 1, body: "stuff"}) {
    errors {
      message
      attribute
    }
    submissionDraft {
      _id
      body
    }
  }
}
```

* you should get a result like:
```
{
  "data": {
    "createSubmissionDraft": {
      "errors": null,
      "submissionDraft": {
        "_id": "42"
        "body": "stuff",
      }
    }
  }
}
```

* in the rails console, confirm the data (especially the body)
  is correct: SubmissionDraft.find(<id>)

fixes COMMS-2265

Change-Id: I6ba6a67e3a592e295c62365ca7fa13250d05e837
Reviewed-on: https://gerrit.instructure.com/203731
QA-Review: Matthew Lemon <mlemon@instructure.com>
Reviewed-by: Matthew Lemon <mlemon@instructure.com>
Tested-by: Jenkins
Product-Review: Ryan Norton <rnorton@instructure.com>
This commit is contained in:
Ryan Norton 2019-08-02 14:57:13 -06:00
parent d790a76222
commit b54f9f6a8f
3 changed files with 23 additions and 2 deletions

View File

@ -29,12 +29,14 @@ class Mutations::CreateSubmissionDraft < Mutations::BaseMutation
# create the `SubmissionDraft` for an old attempt and not return it back in
# subsequent graphql queries for submission drafts.
argument :attempt, Integer, required: false
argument :body, String, required: false
argument :file_ids, [ID], required: false, prepare: GraphQLHelpers.relay_or_legacy_ids_prepare_func('Attachment')
argument :submission_id, ID, required: true, prepare: GraphQLHelpers.relay_or_legacy_id_prepare_func('Submission')
field :submission_draft, Types::SubmissionDraftType, null: true
def resolve(input:)
submission = find_submission(input[:submission_id])
file_ids = (input[:file_ids] || []).compact.uniq
attachments = get_and_verify_attachments!(file_ids)
verify_allowed_extensions!(submission.assignment, attachments)
@ -44,6 +46,10 @@ class Mutations::CreateSubmissionDraft < Mutations::BaseMutation
submission_attempt: input[:attempt] || (submission.attempt + 1)
).first_or_create!
submission_draft.attachments = attachments
# for drafts we allow the body to be null or empty, so there's nothing to validate
submission_draft.body = input[:body]
submission_draft.save!
{submission_draft: submission_draft}
rescue ActiveRecord::RecordNotFound
raise GraphQL::ExecutionError, 'not found'

View File

@ -1005,6 +1005,7 @@ Autogenerated input type of CreateSubmissionDraft
"""
input CreateSubmissionDraftInput {
attempt: Int
body: String
fileIds: [ID!]
submissionId: ID!
}

View File

@ -31,6 +31,7 @@ RSpec.describe Mutations::CreateSubmissionDraft do
def mutation_str(
submission_id: @submission.id,
attempt: nil,
body: nil,
file_ids: []
)
<<~GQL
@ -38,6 +39,7 @@ RSpec.describe Mutations::CreateSubmissionDraft do
createSubmissionDraft(input: {
submissionId: "#{submission_id}"
#{"attempt: #{attempt}" if attempt}
#{"body: \"#{body}\"" if body}
fileIds: #{file_ids}
}) {
submissionDraft {
@ -47,6 +49,7 @@ RSpec.describe Mutations::CreateSubmissionDraft do
_id
displayName
}
body
}
errors {
attribute
@ -58,7 +61,7 @@ RSpec.describe Mutations::CreateSubmissionDraft do
end
def run_mutation(opts = {}, current_user = @student)
result = CanvasSchema.execute(mutation_str(opts), context: {current_user: current_user})
result = CanvasSchema.execute(mutation_str(opts), context: {current_user: current_user, request: ActionDispatch::TestRequest.create})
result.to_h.with_indifferent_access
end
@ -99,6 +102,17 @@ RSpec.describe Mutations::CreateSubmissionDraft do
).to eq second_result.dig(:data, :createSubmissionDraft, :submissionDraft, :submissionAttempt)
end
it 'allows you to set a body on the submission draft' do
result = run_mutation(
submission_id: @submission.id,
attempt: @submission.attempt,
body: 'some text body'
)
expect(
result.dig(:data, :createSubmissionDraft, :submissionDraft, :body)
).to eq 'some text body'
end
it 'returns an error if the attachments are not owned by the user' do
attachment = attachment_with_context(@teacher)
result = run_mutation(
@ -145,7 +159,7 @@ RSpec.describe Mutations::CreateSubmissionDraft do
).to eq 'submission draft cannot be more then one attempt ahead of the current submission'
end
it "uses the submission attempt plus one if an explicit attempt isn't provided" do
it 'uses the submission attempt plus one if an explicit attempt is not provided' do
result = run_mutation(
submission_id: @submission.id,
file_ids: [@attachments[0].id]