Giter VIP home page Giter VIP logo

Comments (9)

jxnu-liguobin avatar jxnu-liguobin commented on June 26, 2024

This one looks interesting

from cynic.

obmarg avatar obmarg commented on June 26, 2024

@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:

  1. https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/fragment_derive/mod.rs#L388-L395
  2. https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/enum_derive/mod.rs#L47-L53
  3. https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/enum_derive/mod.rs#L149-L152
  4. https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/inline_fragments_derive/mod.rs#L32-L38
  5. https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/inline_fragments_derive/mod.rs#L123-L151
  6. https://github.com/obmarg/cynic/blob/master/cynic-codegen/src/input_object_derive/mod.rs#L49-L57
  7. 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.

jxnu-liguobin avatar jxnu-liguobin commented on June 26, 2024

@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 avatar obmarg commented on June 26, 2024

@obmarg Yeah, definitely just suggestions.

  1. 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.
  2. 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 the syn:Error

from cynic.

jxnu-liguobin avatar jxnu-liguobin commented on June 26, 2024

yeah

from cynic.

jxnu-liguobin avatar jxnu-liguobin commented on June 26, 2024
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.

obmarg avatar obmarg commented on June 26, 2024

@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.

jxnu-liguobin avatar jxnu-liguobin commented on June 26, 2024

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.

obmarg avatar obmarg commented on June 26, 2024

This was done in #196 and released as part of v0.12.0 - thanks once again @jxnu-liguobin 🎆

from cynic.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.