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

Change variable name if it start with underscore. #116

Closed
TarekkMA opened this issue Apr 14, 2020 · 19 comments
Closed

Change variable name if it start with underscore. #116

TarekkMA opened this issue Apr 14, 2020 · 19 comments
Labels
bug Something isn't working
Projects

Comments

@TarekkMA
Copy link

Bug description
Fields starting with underscore cause issues since dart can't have private named parameters.

In the mutation/query I can alias field name to avoid the problem with fields starting with underscore. But in the input I can't do the same.

I think this issue can be solved by checking for underscore in the field name and using @JsonKey(name:"noUnderScoreName") to mitigate the issue

Specs
Artemis version: '>=5.0.0 <6.0.0'

mutation addStudent($input: StudentInput){
    addStudent(input:$input){
        id:_id,
    }
}

input StudentInput {
    _id: Int
    person: PersonInput
   ...
}

input PersonInput {
    ...
}
@TarekkMA TarekkMA added the bug Something isn't working label Apr 14, 2020
@comigor
Copy link
Owner

comigor commented Apr 14, 2020

Hello @TarekkMA!

Thanks for reporting this! It's related to #41 (in a time we were filtering out fields starting with double underscores).

@comigor comigor added this to Soon™ in Board Apr 14, 2020
@vasilich6107
Copy link
Collaborator

@comigor
Hi. I have some time to work on Artemis next week.
I can implement transformations from '_variable' to '$variable' if I understood correctly the approach suggested in #41 (comment)

@vasilich6107
Copy link
Collaborator

vasilich6107 commented May 3, 2020

In scrope of this issue I would like to cover the issue when variable is called like the Dart keyword.
Here is a list https://dart.dev/guides/language/language-tour#keywords

I have an example of this edge case in enum on previous project

enum CompletionStatus {
    completed
    new
    saved
    unknown
}

The new caused errors in generated code)

@vasilich6107
Copy link
Collaborator

vasilich6107 commented May 3, 2020

To sum up:

All items that start with underscore will be replaced by $

_variable => $variable

__typename => $$typename

All items matching the Dart keywords will be prepended with $

new => $new

factory => $factory

If you agree with this approach I'll implement it.

@comigor
Copy link
Owner

comigor commented May 4, 2020

@vasilich6107 that seems the way to go!

@vasilich6107
Copy link
Collaborator

vasilich6107 commented May 10, 2020

Small update.
In case of dart keyword match we need something more tricky)
There could be a case when the enum has new and _new at the same time so prepending keywords with $ will lead to two equal $new items.

I suggest to prepend dart keywords with kw$
new -> kw$new

@comigor
Copy link
Owner

comigor commented May 10, 2020

I think that's ok, but following this mindset, there can be both _thing and __thing fields, how would you deal with that? A dollar sign for each underscore?

I think I would just bail on fields matching keywords, suggesting to use aliases instead, but that opinion may be unpopular.

@vasilich6107
Copy link
Collaborator

As I wrote previously

_variable => $variable

__typename => $$typename

@comigor
Copy link
Owner

comigor commented May 10, 2020

oops, sorry, forgot about that message

yep, that seems better than the bug we have today

@vasilich6107
Copy link
Collaborator

vasilich6107 commented May 10, 2020

I do not think that you can alias the enum item in this case)

enum CompletionStatus {
    completed
    new
    saved
    unknown
}

@vasilich6107
Copy link
Collaborator

vasilich6107 commented May 10, 2020

The aliasing could always be a fallback when somebody would not like the kw$ prefix)))

@vasilich6107
Copy link
Collaborator

Hi @TarekkMA
Could you try this branch while it awaits the review?

artemis:
    git:
      url: https://github.com/comigor/artemis.git
      ref: bugfix/underscored-items-another-solution

@vasilich6107
Copy link
Collaborator

Closed by #142

Board automation moved this from Soon™ to Done Jun 1, 2020
@RageshAntony
Copy link

To sum up:

All items that start with underscore will be replaced by $

_variable => $variable

__typename => $$typename

All items matching the Dart keywords will be prepended with $

new => $new

factory => $factory

If you agree with this approach I'll implement it.

I am having a issue in this renaming

I am quering mongodb using graphql , so IDs are like '_id'

These _id converted to $id , like you said

But it also sending query in renamed variable like

{$id: 5f660d7a929072166f5addb2}

So I am getting exception the variable not supplied

But if I manually gave variables in WatchQueryOptions as {'_id': 5f660d7a929072166f5addb2} , query working ...

Pls help me

Code

Future getSeries() async {
  final args = SeriesByIdArguments($id: "5f660d7a929072166f5addb2");
  final WatchQueryOptions _options = WatchQueryOptions(
      errorPolicy: ErrorPolicy.all,
      fetchResults: true,
      variables: <String, dynamic>{
        '_id': "5f660d7a929072166f5addb2", // if I gave args.toJson() , execption thrown since $id is going 
      },
      documentNode: SeriesByIdQuery(
              variables: SeriesByIdArguments($id: "5f660d7a929072166f5addb2"))
          .document);

  final client = GraphQLClient(
    cache: InMemoryCache(),
    link: HttpLink(uri: 'http://192.168.31.107:8000/graphql'),
  );

  QueryResult result = await client.query(_options);
  final series = SeriesById$Query$Series.fromJson(result.data);
  print('series ' + result.data.toString());
  print(args.toJson());
}

@vasilich6107
Copy link
Collaborator

I’ll check

@vasilich6107
Copy link
Collaborator

vasilich6107 commented Oct 3, 2020

Works fine as you see with latest artemis beta version - https://tppr.me/pJ1td
If you still have a problems create a new issue according to template

@RageshAntony
Copy link

Works fine as you see with latest artemis beta version - https://tppr.me/pJ1td
If you still have a problems create a new issue according to template

i already using 6.12.3-beta.2 ...But toJson printing $id only

image

The implementation in generated class is only having $id for both fromJson and toJson

GraphQL query:

query seriesById($_id: MongoID!){
    seriesById(_id: $_id){
        title
        description
        rating
        maturity
        release
        language
        seasons
        seasonsInfo{
            seasonNo
            info
            release
            episodes
            _id
        }
        episodesData{
            seasonNo
            number
            title
            desc
            airDate
            durationMins
            poster
            uri
            _id
        }
        cast{
            name
            roll
            info
            _id
        }
        studio
        videoUrl
        videoImage
        createdBy
        for
        isPaid
        categoryId
        likes
        disLikes
        views
        isDeleted
        _id
        updatedAt
        createdAt
    }
}

@vasilich6107
Copy link
Collaborator

Please create a separate issue and fill it according to template

@RageshAntony
Copy link

Please create a separate issue and fill it according to template

yeah here
#223

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Board
  
Done
Development

No branches or pull requests

4 participants