These are chat archives for graphql-java/graphql-java

27th
Jun 2017
Brad Baker
@bbakerman
Jun 27 2017 00:24
@_bkern_twitter - I have avoided using scalars for date since scalrs cant take arguments
by using a object representation back to String you can push formatting of dates into your server instead of the client
{
   person {
      birthdate(fmt = “yyyy/mm/dd”)
}
actually I take that back - you can create a scalar for sermantic meaning say but I think its diminishing returns
we are very keen to take certain logic OUT of the clients - it makes for more light wegiht mobile clients
Barry
@_bkern_twitter
Jun 27 2017 13:41
ah ok interesting
so one field having trouble with getting properly mapping after sending into graphql-java is Locale. I sent it in as string en and en_US etc and I have it mapped as GraphQLString but when I query for it after the mutation it comes back null even though in the mutation I sent in that string. I wonder if I can use that object representation mentioned above
@bbakerman what would the java side of person/birthdate i guess I am unclear how to map that in java but I grasp your overall idea
Bojan Tomić
@kaqqao
Jun 27 2017 16:17
If you're using graphql-java directly, there's no concept of mapping a class to a GraphQL type. You just receive a string e.g. en_US(btw, it should be en-US to comply with ISO), and it's up to you to interpret it. You can create a Locale from it or not, it's your job not graphql-java's.
If you want Java-GraphQL mapping, take a look at graphql-java-annotations, schemagen or graphql-spqr
Barry
@_bkern_twitter
Jun 27 2017 16:18
do I have control over the parsing ? I notice it goes into execute as Object and then ends up being translated to my type internally
I do 'map' my POJO to GraphQL types
Bojan Tomić
@kaqqao
Jun 27 2017 16:20
Can you show the relevant part of the schema and resolution logic? It's hard to answer without knowing what you're doing
If your input type is a GraphQLString, you'll receive a String as the value
Barry
@_bkern_twitter
Jun 27 2017 16:24
yea I think thats where Im struggling the Java type is Locale but I map it as a GraphQLString and not sure how to handle that ..if I have opportunity to plug in string to Locale method or should I customize a new type ?
Bojan Tomić
@kaqqao
Jun 27 2017 16:28
You have a few options, you can either accept a string in the DataFetcher and deserialize it yourself, or you can create a custom scalar. Here's an example of a Locale scalar from graphql-spqr
Bojan Tomić
@kaqqao
Jun 27 2017 19:40
@_bkern_twitter If you're not making a new scalar, just have a Datafetcher like:
DataFetcher fetcher = new DataFetcher() {
            @Override
            public Object get(DataFetchingEnvironment env) {
                String localeString = env.getArgument("locale");  //"locale" is the input argument name, change is different
                Locale locale = Locale.forLanguageTag(localeString);
                //do what you need here
                return ...;
            }
        }
So you just get a string from the client, and parse it yourself in the fetcher logic

If you use a custom scalar, you'd skip that step and just do

Locale locale = env.getArgument("locale");

But this would mean your input is typed as the locale scalar, not as a string