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

How do I transform the names of parameters for a class that is used as a query argument? #98

Open
WesToleman opened this issue May 6, 2020 · 3 comments

Comments

@WesToleman
Copy link

Background

I'm trying to pass the variables { "foo": {"fooNumber": "0123456789", "systemCode": "BAZ"} } to this query

query GetFooId(
  $foo: FooNumber
) {
  foobeta (foo: foo) {
    id
  }
}

What works

If I use a class that with parameters that are named identically it works.

var = new { number = "0123456789", systemCode = FooSystemType.BAZ };
var arg = new GraphQLQueryArgument("foo", foo));

Issue

However it does not work with a traditionally named class because parameter names are not transformed.

public class FooNumber
{
    [GraphQLFieldName("fooNumber")]
    public string Number { get; set; }

    public FooSystemType SystemCode { get; set; }
}

It generates an SAHB.GraphQLClient.Exceptions.GraphQLHttpExecutorServerErrorStatusCodeException because of a BadRequest.

Generated Query

Query

query($foo:FooNumber){foobeta(foo:$foo){id}}",

Variables

"foo": {
    "Number": "0123456789",
    "SystemCode": "BAZ"
}

Response

{
    "errors": [
        {
            "message": "Variable \"$foo\" got invalid value { Number: \"0123456789\", SystemCode: \"BAZ\" }; Field fooNumber of required type String! was not provided."
        },
        {
            "message": "Variable \"$foo\" got invalid value { Number: \"0123456789\", SystemCode: \"BAZ\" }; Field systemCode of required type FooSystemType! was not provided."
        },
        {
            "message": "Variable \"$foo\" got invalid value { Number: \"0123456789\", SystemCode: \"BAZ\" }; Field \"Number\" is not defined by type FooNumber."
        },
        {
            "message": "Variable \"$foo\" got invalid value { Number: \"0123456789\", SystemCode: \"BAZ\" }; Field \"SystemCode\" is not defined by type FooNumber. Did you mean systemCode?"
        }
    ],
}

Query classes

public class FooQuery
{
    [GraphQLArguments("foo", "FooNumber", "foo")]
    public FooBeta FooBeta { get; set; }
}

[GraphQLFieldName("foobeta")]
public class FooBeta : Foo
{
}

public class Foo
{
    public string Id { get; set; }
}

[JsonConverter(typeof(StringEnumConverter))]
public enum FooSystemType
{
    [EnumMember(Value = "BAZ")]
    BAZ,
}
sahb1239 added a commit that referenced this issue May 8, 2020
@sahb1239
Copy link
Owner

sahb1239 commented May 8, 2020

Hello @WesToleman

Currently the arguments are serilized using Newtonsoft.Json and therefore you should use [JsonProperty("fooNumber")] instead of [GraphQLFieldName("fooNumber")].

Please see the example here:
e53fa7e

In order to be able to support GraphQLFieldName the dependency on Newtonsoft.Json must be removed. This could be done in a later release.

@WesToleman
Copy link
Author

Thanks for the example @sahb1239. Can JsonProperty and GraphQLFieldName be used on the same property? I can imagine there might be scenarios where FooNumber is part of the query model. It's not a major issue for me at the moment but I would like to know if it does get solved in a future release.

@sahb1239
Copy link
Owner

Hello @WesToleman
It should be possible to use both attributes on the same property.

I can't at the moment give you an exact release date, but I will look at it when I get the needed time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants