mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 21:10:32 +01:00
GraphQL Schema validation with custom scalars #539
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @jflevesque-genetec on GitHub (Aug 1, 2023).
Originally assigned to: @StefH on GitHub.
I've been trying to get the validation to work when using WithGraphQLSchema and can't seem to get it to work. By debugging the lib, I can see that I get an exception because my scalar type cannot be resolved:
"Unable to resolve reference to type 'MyCustomScalar' on 'MyMutation'"
I'm curious to know if this is something supported at the moment or if there's a way to define/specify the custom scalar inside the schema? At the moment, I had simply specified it using "scalar MyCustomScalar" inside the schema before my type, but clearly that isn't working.
Any inputs are well appreciated.
@StefH commented on GitHub (Aug 1, 2023):
Can you please provide your full GraphQL schema + how those 'MyCustomScalar' and 'MyMutation' are defined?
@jflevesque-genetec commented on GitHub (Aug 1, 2023):
Schema taken from the documentation and modified slightly. Here is the query I'm sending through Postman:
Exception: {"Unable to resolve reference to type 'DateTime' on 'MessageInput'"}
I was able to find a workaround by loading the Schema myself and manually adding class definitions implementing ScalarGraphType, something like
and its registration:
Since the exceptions are caught, I would always get an No mapping exists error message, which wasn't very helpful until I compiled the lib and used the pdbs to debug and looked at what was happening in the matcher. The error message at the exception level were pretty helpful to find this workaround.
@jflevesque-genetec commented on GitHub (Aug 1, 2023):
On a side note: even when registering scalar types, if the schema indicates a non-nullable response, it seems like the matcher will always throw. Not sure if its something that can be handled or not
@StefH commented on GitHub (Aug 1, 2023):
Thanks for the detailed description. I need some time to think on this and what to change in WireMock.
About
No mapping exists: in several other places in the matchers, I catch exceptions, and depending on https://github.com/WireMock-Net/WireMock.Net/blob/master/src/WireMock.Net/Matchers/IMatcher.cs#L21 I do throw an exception.However I think I need to change this behaviour, and just return the exception in the 404 message so that in case of an exception, the caller can figure out what could be wrong.
@jflevesque-genetec commented on GitHub (Aug 4, 2023):
On that point, it becomes a bit problematic, because if you have multiple matchers, then which exception are you supposed to throw? In my case, I have a global matcher which returns Unauthorized if my authorization header was missing, then 1 matcher per specific request I was handling. Since it is mocking a GraphQL server, then 2 matchers out of the 3 would have thrown.
I just don't know the right way to have access to the right information to be able to troubleshoot.
I know the GraphQL support was added very recently, but I noticed if a schema also includes interfaces, it really doesn't like that (I couldn't get it to work at all if an interface was present). It was easy to remove the mention of the
implements X, but it does make the schema differ from the source of truth. Maybe it could be something you could look at when you look at the scalar types problem, but no rush :)@StefH commented on GitHub (Aug 5, 2023):
1️⃣
I'm currently rewriting some logic when an exception occurs in a matcher.
The path I'm following now is that I catch these exception(s) and write these exceptions to the logging like this:
Would this be a solution for you?
2️⃣
About fixing the root-cause: I did not have time yet to investigate that. Probably a way to solve this is to expose some interface on the mapper and the json mapping to support adding extra types like you do:
https://github.com/WireMock-Net/WireMock.Net/pull/986
@jflevesque-genetec commented on GitHub (Aug 21, 2023):
1- It looks very promising. What would it look like if you have multiple matchers configured?
2- It would probably be the easiest. Otherwise, any way to inject a list of types or something when you setup the schema?
@StefH commented on GitHub (Aug 21, 2023):
2]
For some reason several simple standard build-in types are not registered by default by GraphQL.NET
I don't know why.
But when I change the code to below, your example works.
@StefH commented on GitHub (Oct 15, 2023):
https://github.com/WireMock-Net/WireMock.Net/pull/1011
@StefH commented on GitHub (Oct 15, 2023):
@jflevesque-genetec
You can try preview version 1.5.39-ci-17839 which supports default scalars like DateTime.
(https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
@jflevesque-genetec commented on GitHub (Oct 16, 2023):
Hi Stef, I'm currently testing out the CI build and wanted to confirm with you which behavior this addresses. Does this fix only target built-in scalar types like DateTime from the GraphQL lib or custom scalar types as well defined directly in the schema string? If its also the latter, it does not seem like it is working.
@StefH commented on GitHub (Oct 16, 2023):
Currently only scalars in the schema.
Custom scalars will be implemented in another PR.
@jflevesque-genetec commented on GitHub (Oct 16, 2023):
In that case, everything still seems to be working fine with the CI build
@StefH commented on GitHub (Oct 16, 2023):
@jflevesque-genetec
I've built some code to get all custom scalars from a schema.
However, I also need to know what the C# Type is for that custom scalar. Else there is no check if a value is having the correct type which means that the matcher will not work as intended.
So a mapping must be provided like:
With this mapping I can dynamically create these MyCustomScalar and AnotherScalar and provide these to the schema so that resolving works.
Also the ParseValue should be implemented so that when you pass a value "true" to the MyCustomScalar, an exception should be thrown.
@StefH commented on GitHub (Oct 19, 2023):
@jflevesque-genetec
In preview 1.5.39-ci-17870 i've added code use custom scalars.
like:
Can you test this?
@jflevesque-genetec commented on GitHub (Oct 19, 2023):
Hi Stef,
Sorry for the delay in testing. I kind of understand how to create the matcher, but it seems to lose a lot of the convenience of the
IRequestBuilder. Is there a plan to update the builder to be able to inject the types with the string schema or would it be necessary to go with the manually built matcher?Side note: there isn't a way at the moment to add a matcher to the IRequestBuilder.
So I'm a bit unsure how I'm supposed to test this.
@StefH commented on GitHub (Oct 26, 2023):
Hello @jflevesque-genetec ,
I missed your update in your last comment.
But using the RequestBuilder can be done like this (if this is what you mean)...
@jflevesque-genetec commented on GitHub (Oct 26, 2023):
That would actually be great. Easy to add the custom scalar types for the schema using the existing request builder. It would fill my needs perfectly.
@StefH commented on GitHub (Oct 26, 2023):
If you have time. Please test it
@StefH commented on GitHub (Nov 5, 2023):
@jflevesque-genetec
Did you have time yet to test this?
@StefH commented on GitHub (Nov 18, 2023):
@jflevesque-genetec
Did you have time yet to test this?
@jflevesque-genetec commented on GitHub (Nov 20, 2023):
Hi @StefH,
Sorry, I left on a business trip and then completely forgot. I'll test it this week.
@StefH commented on GitHub (Nov 29, 2023):
@jflevesque-genetec
Could you test it?
@jflevesque-genetec commented on GitHub (Dec 4, 2023):
Hi Stef,
In 1.5.39-ci-17870, the code does not compile when I try to use
.WithGraphQLSchema(TestSchema, customScalars). I've updated to 1.5.40-ci-17986 to see if it works in that version, which also does not work. There simply isn't any overload inIGraphQLRequestBuildertaking a dictionary to do as suggested.@StefH commented on GitHub (Dec 4, 2023):
@jflevesque-genetec
Maybe the preview version was automatically removed from MyGet.
I've triggered a new build for that branch, and the preview version number is
1.5.40-ci-17988.See also here:
https://github.com/WireMock-Net/WireMock.Net/pull/1012/files#diff-7c5e38fc36bbca0209ec077c0230d1c55fe0a40380398013cdea36bd689dcaa4R33
@jflevesque-genetec commented on GitHub (Dec 4, 2023):
Hi Stef,
I've just tried with 17988 and everything works
@StefH commented on GitHub (Dec 4, 2023):
Thanks.
I'll merge the branch to main and a new official version will be released shortly.