Comments (9)
This one looks interesting
from cynic.
@jxnu-liguobin would be happy for you to pick this one up if you wanted.
Probably just a case of looking for the various places where we would otherwise raise a "can't find a field/type with this name" error, and updating it to suggest the nearest possible match. Possibly using that library I linked in the issue though only if it makes sense. Some of the places this could be done:
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/fragment_derive/mod.rs#L388-L395
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/enum_derive/mod.rs#L47-L53
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/enum_derive/mod.rs#L149-L152
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/inline_fragments_derive/mod.rs#L32-L38
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/inline_fragments_derive/mod.rs#L123-L151
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/input_object_derive/mod.rs#L49-L57
- https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/input_object_derive/mod.rs#L180-L188
Would be happy to accept a PR that fixed any of these - even if you don't have time to do all of them.
from cynic.
@obmarg Direct update seems to have hidden danger. I understand that we only need to make suggestive ?
There are two questions
1.which matching algorithm
2.be displayed as log (level)?
from cynic.
@obmarg Yeah, definitely just suggestions.
- Not sure on the matching algorithm actually. Have had success with levenshtein & jaro-winkler before, and would be happy with either of those. Though if you want to look into it and use something else that is up to you.
- All of that code I linked runs in the rust compiler as part of a macro. Macros don't typically get to log out to the user in the same way that other code would. However, each of those cases returns a
syn::Error
which gets translated into a compiler error. I would probably just add the suggestions in as part of the text on thesyn:Error
from cynic.
yeah
from cynic.
pub fn guess_value(candidates: &Vec<String>, field_name: &str, k: usize) -> Option<String> {
return candidates.iter().find(|x|
match hamming(x.as_str(), field_name) {
//For example, consider the code consisting of two codewords "000" and "111".
//The hamming distance between these two words is 3, and therefore it is k=2 error detecting.
//Which means that if one bit is flipped or two bits are flipped, the error can be detected.
//If three bits are flipped, then "000" becomes "111" and the error can not be detected.
Ok(distance) =>
if distance <= k {
true
} else {
false
}
Err(_) => false
}).map(|x| x.to_owned());
}
I'm going to use function like it, but I'm confused about the naming of some structures, which makes it difficult for me to determine the specific content of the parameters. I think the structure should be given appropriate comments, and the type of graphql should have appropriate corresponding relationship, which is easier to understand.
(this may be due to the module management of rust, which is not as clear as JVM package)
Use it
let candidates :Vec<String>= schema.definitions.iter().map(|def| {
if let Definition::TypeDefinition(TypeDefinition::Enum(e)) = def {
e.clone().name //Is this the name of the graphql ENUM field?
} else {
"".to_owned()
}
}).collect();
//I really don't know. Does input include field name?
let guess_value= guess_value(&candidates, (*(input.graphql_type)).borrow(), 3);
if enum_def.is_none() {
return Err(syn::Error::new(
input.graphql_type.span(),
format!(
"Could not find an enum named {} in {}.{}",
*input.graphql_type, *input.schema_path,
format_guess_value(guess_value).as_str()
),
));
}
from cynic.
@jxnu-liguobin Sorry, this confusion is probably my fault. If you want to point me at specific things you'd like me to document then I'm happy to do so. Been a ton of work just to support all of GraphQL so not always had the time to make sure the internal code is perfect.
Anyway, what you have there looks close to right. That particular example isn't actually looking for a field name, but the name of a particular enum type in the GraphQL schema - I included things like that in my list because they're functionally the same problem, sorry for being confusing.
To give you a run-down of the things in that second code snippet:
schema.defintions
is a vec of all the things in a GraphQL schema: object definitions, input object definitons, scalar defintions, enum definitions etc.- You're correctly iterating over it and grabbing the name of all the enums in the schema.
input
is the input to this macro invocation. The code that invokes this looks roughly like the following codeblock:
#[derive(cynic::Enum, Clone, Copy, Debug)]
#[cynic(
graphql_type = "Market",
schema_path = "path/to/schema.graphql"
)]
pub enum Market {
Uk,
Ie,
}
input.graphql_type
corresponds to the graphql_type
attribute on that enum, which tells cynic which enum in the GraphQL schema it should look up and use to generate code. So what you have there looks about right - you're searching for the expected enum name in all the possible enum names and using that as a suggestion.
Do you maybe want to get this one example working right and then submit a PR? There's a few rust pointers I can give you, but it'll be much easier in the context of some working code in a PR.
And if you want to request that I document some specific bits of the code also let me know, should be able to do that fairly easily.
from cynic.
Thank you for your explanation.It may be easier to read if you can add instructions for the struct's fields and public methods.
And i have submitted a draft pr , please take a look
from cynic.
This was done in #196 and released as part of v0.12.0 - thanks once again @jxnu-liguobin 🎆
from cynic.
Related Issues (20)
- `cynic generate` improvements HOT 1
- `cynic author`
- cynic-parser tracking issue
- GraphQlResponse Clonable? HOT 4
- cynic-introspection SDL is missing directives
- Fix default root handling in cynic-codegen HOT 1
- Object in array in arguments attribute fails to compile
- Catch unintentionally recursive structs at compile time HOT 8
- Fully fledged example for working with Large APIs HOT 2
- Macro with generator.cynic-rs.dev functionality HOT 5
- Vec<String> variables or arguments not being passed correctly in when I upgrade from v1.0 to cynic v3.4.3 HOT 3
- Update reqwest to 0.12 HOT 5
- reinstate cli builds as part of releases
- Deprecate & remove pre 2.0.0 argument syntax
- Put spans on things somehow HOT 1
- admonishments in the book are broken
- Cannot retrieve `GraphQLResponse`s with `ErrorExtensions` properly HOT 4
- Dependency Dashboard
- Action Required: Fix Renovate Configuration
- How exactly to build wrapper libraries? HOT 1
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 cynic.