Comments (2)
Hi @pyros2097
I just got a chance to look at the code you posted and ran it through both graphql-go
and graphql-js
to verify the output, and you are right, at first glance, the behaviour is suspect.
Hang on tight cos this issue has multiple layers to it:
1.validator
not implemented yet in graphql-go
Validation of arg inputs (for e.g. is the input matches the expected input type, etc) are supposed to be done through the validator
before it gets passed to the executor
.
But currently, validator
has not been implemented yet.
2. panic()
is not expected to be used in definition of scalars at the moment.
I see that currently you try to throw a panic()
if the scalar value is unexpected.
If you study how built-in scalars are defined (both in graphql-go
and graphql-js
, you would notice that if a scalar value/literal can't be parsed/serialized, it will return nil
.
Currently, if you throw a panic in ParseValue/ParseLiteral/Serialize (unlike Resolve in ObjectConfig), it won't be caught by graphql-go
.
Side note: according to GraphQL spec, regarding Scalars:
- "The only requirement is that the server must yield values which adhere to the expected Scalar type"
- "raise a field error" if it can't parse the given value.
3. Query string in params2
does not get validated because it did not specify the correct type
The current query in params2
of your example:
mutation M($input: String!) {
ObjectCreate(id: $input) {
id
}
}
But ObjectCreate
field expected a nullable value of type ID
, as specified here:
...
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.FieldConfigMap{
"ObjectCreate": &graphql.FieldConfig{
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: ID, // <-- nullable value of type `ID`
},
},
},
},
}),
Try to update it to:
mutation M($input: ID) { // <--- Update it to nullable `ID`, as specified in `ObjectCreate` field
ObjectCreate(id: $input) {
id
}
}
After updating the query, now your ParseValue function will be executed and should throw fits all over ;)
The reason why your original didn't throw error when you specified the wrong type for the input argument is because validator
has not been implemented. (see point 1).
If validator
was doing its job, it should have an return error similar to this:
Variable "$input" of type "String!" used in position expecting type "ID".
So to address your original question, the reasons why your custom scalar does not seem to validate input are because:
- the query that used
variables
specified the wrong input type. - and
validator
is not there to do its job to let you know in the first place.
This issue highlights at least two things that are incomplete in graphql-go
implementation:
validator
, to validate queries- how to handle errors in user-defined functions (e.g
ParseValue, ParseLiteral, Serialize, Resolve
etc)
I'll create issues to highlight these flaws in implementation.
Cheers!
from graphql.
Hi @sogko,
Your'e exactly right. It wasn't validating my type because my query was wrong. Anyway it seems if we pass the input directly like in the first query it validates it correctly and the panic in the ParseLiteral
is triggered and returned as a graphql error. Thanks for the reply. I'll be closing this issue.
from graphql.
Related Issues (20)
- Create graphql schema from string HOT 3
- The code generator does not take into account naming conflicts HOT 1
- How to take metro in graphql out of the solve?
- Is this project still mantained? HOT 2
- New release?
- Potential goroutine leak in TestContextDeadline HOT 1
- Enums with trailing white space cause error.
- Playground
- make union input type ? it just have output union type
- With fiber HOT 1
- Printing GraphQL documents is slow HOT 1
- Bug with underscore and same name in keys from json response
- MongoDB _id field
- Printer returns invalid SDL if Block String comment contains double-quote
- When using custom scalar types, it's crucial to provide error feedback to users when issues arise with their submitted data. However, triggering exceptions within the ParseValue and ParseLiteral methods can lead to program crashes when using the graphql.Do method. This prevents the necessary error messages from being delivered to users.
- String type no longer recongised HOT 1
- Is it possible to go from `*graphql.Schema` to an ast node? HOT 1
- Error handling HOT 1
- Disabling Field Suggestions
- Is possible to typing typeConfig of graphql.Object after Object done.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql.