Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve GraphQL Query Schema validation and logging #6536

Merged
merged 31 commits into from
Jul 17, 2020

Conversation

hyzx86
Copy link
Contributor

@hyzx86 hyzx86 commented Jun 29, 2020

No description provided.

@hyzx86 hyzx86 requested a review from hishamco July 1, 2020 04:10
Copy link
Member

@deanmarcussen deanmarcussen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging uses a different string interpolation method to prevent allocations, so you should never use $ to format log messages.

I think I'm ok with this staying as LogWarning

@hyzx86 hyzx86 changed the title Added ValidateSettings failure reason log Added ValidateSettings failure reason log , add query schema validate ,and skip error query schema Jul 1, 2020
var queryField = BuildContentTypeFieldType(schema, contentType, query);
if (queryField != null)
var querySchema = JObject.Parse(query.Schema);
if (!querySchema.ContainsKey("type"))
Copy link
Contributor Author

@hyzx86 hyzx86 Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to add a sample schema on the page ,but I don't know the right schema for here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave that for a seperate pr

Copy link
Member

@deanmarcussen deanmarcussen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hyzx86 I have updated the logging statements with correct usage.

Please can you look at my comment on the controller error handling and see what the result is when adding a ModelState error. It should definitely use ModelState errors, it's possible you may have to have a notifier as well, but I would prefer it is all handled by ModelState errors.

Probably sorting that out is the last thing to do for this pr :)

if (!string.IsNullOrWhiteSpace(query.Schema) && !CheckSchema(query.Schema))
{
_notifier.Error(H["Invalid schema JSON supplied."]);
model.Editor = await _displayManager.BuildEditorAsync(query, updater: _updateModelAccessor.ModelUpdater, isNew: true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should add just add a ModelState error, and see what the result is when it is handled later in the code

            // If we got this far, something failed, redisplay form --> All errors should be handled by this section
            model.Editor = editor;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @deanmarcussen , thanks for your help , ModelState added

This comment was marked as off-topic.

var queryField = BuildContentTypeFieldType(schema, contentType, query);
if (queryField != null)
var querySchema = JObject.Parse(query.Schema);
if (!querySchema.ContainsKey("type"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave that for a seperate pr

@hyzx86 hyzx86 requested a review from deanmarcussen July 4, 2020 11:06

if (!string.IsNullOrWhiteSpace(query.Schema) && !CheckSchema(query.Schema))
{
ModelState.AddModelError("Schema", S["Invalid schema JSON supplied."]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great, this works better.

Sorry, what I should have said though, is to implement IValidatableObject on the QueriesCreateViewModel.

Apply the test for valid JSON on that View Model by implementing IValidatableObject.

Here is an example from where it is used elsewhere in the code https://github.com/OrchardCMS/OrchardCore/blob/dev/src/OrchardCore.Modules/OrchardCore.Users/ViewModels/LinkExternalLoginViewModel.cs

I think you will need to apply the same to the edit controller QueriesEditViewModel View Model

Then in the Queries.Fields.Edit.cshtml View you should be able to apply
<span asp-validation-for="Schema"></span> (like is done for Name in the same file) and you will get a validation error on the schema field itself

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the value of Schema comes from query variable, not model, model.Schema is always empty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
And the query object cannot be filled if it is verified during creation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I just added a string extension for testing json here #6614

But I have had a look at the Queries AdminController and understand what is driving that viewmodel now.

I was wrong to suggest IValidateableObject.

The query Schema, is actually managed and updated by the QueryDisplayDriver.cs

It already performs some checks for validity so the test you are doing for valid Json should move into the QueryDisplayDriver.cs UpdateAsync() method, along with the other checks for valid Names etc.

Copy link
Member

@deanmarcussen deanmarcussen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

While this looks like a lot of formatting changes, it as actually because things are being wrapped in a try catch block to prevent errors being thrown when parsing json.

@deanmarcussen deanmarcussen changed the title Added ValidateSettings failure reason log , add query schema validate ,and skip error query schema Improve GraphQL Query Schema validation and logging Jul 16, 2020
@agriffard agriffard merged commit 920d002 into OrchardCMS:dev Jul 17, 2020
@hyzx86 hyzx86 deleted the patch-3 branch June 13, 2024 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants