So, I'm implementing this library to sync up with a GraphQL server, and as pointed in #253 there's an issue with parsing template values when the value is in a nested object.
postTemplate: {
query: 'mutation SendLocation( $lat:String! $lon: String!) { sendLocation(input: { lat:$lat lon:$lon }) { status } }',
operationName: 'SendLocation',
variables: {
lat: '@latitude',
lon: '@longitude'
}
}
Both @latitude and @longitude should be replaced by the correspondent coordinates.
Location parameters should be replaced in any level within a nested object when defined in postTemplate.
Only first-level variables are replaced, while nested objects are unchecked for location parameter replacements.
I actually fixed this locally, but i'm unable to view the files required for the fix in my forked repository (i'm new to forks so if someone has a solution to show all the package files like the npm module I would gladly appreciate it) @mauron85
The fix is in android/src/main/java/com/marianhello/bgloc/data/HashMapLocationTemplate.java:
replace the method locationToJson with the updated version including a new method which recursively replaces the location parameters within a nested JSONObject:
@Override
public Object locationToJson(BackgroundLocation location) throws JSONException {
JSONObject jObject = new JSONObject();
Iterator<?> it = mMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> pair = (Map.Entry) it.next();
Object value = null;
Object key = pair.getValue();
if (key instanceof String) {
value = location.getValueForKey((String)key);
} else if(key instanceof JSONObject) { // Iterate one more time for nested objects
JSONObject sJObject = iterateJSONObject(location, (JSONObject) key);
value = sJObject;
}
jObject.put(pair.getKey(), value != null ? value : pair.getValue());
}
return jObject;
}
public JSONObject iterateJSONObject(BackgroundLocation location, JSONObject jObject) throws JSONException {
JSONObject tmpObj = (JSONObject) jObject;
Iterator<?> it = tmpObj.keys();
JSONObject sJObject = new JSONObject();
while(it.hasNext()) {
String sKey = (String) it.next();
Object sValue = tmpObj.get(sKey);
System.out.println(sValue);
if(sValue instanceof String) {
sValue = location.getValueForKey((String) sValue);
System.out.println(sValue);
} else if(sValue instanceof JSONObject) {
sValue = iterateJSONObject(location, (JSONObject) sValue);
}
sJObject.put((String) sKey, sValue != null ? sValue : tmpObj.get(sKey));
}
return sJObject;
}
set postTemplate to something like this:
postTemplate: {
dummyNested: {
nestSomeMore: {
lat: '@lat'
}
}
}
Parameters referenced in nested objects won't be replaced.
To successfully use location parameters in a POST request to a GraphQL Server. GraphQL endpoints have an specific JSON schema when using POST requests, so location parameters should be replaced in any level of nesting.
NOTE: I'm yet to develop in iOS so the fix works only in Android at the moment. Suggestions are welcome.
My temporary solution for this case is just take data from request.
Thanks for your support and still waiting for next release.
Any suggestions on how to git pull the android/common submodule to add the changes and then issue a pull request @mauron85 ?
I think your problem relates to Experimental Gradle 3 support
https://github.com/mauron85/react-native-background-geolocation
You can follow the description of this lib or his example :
https://github.com/mauron85/react-native-background-geolocation-example
thanks @manhncse02926 but I think we are both confused here. I can run the module successfully, even with the fix I posted above for the postTemplate issue.
But I forked the repo to change the files and submit a pull request, but for some reason the android/common folder (which is where the HashMapLocationTemplate.java file is) is empty. I'v tried git checkout to the latest commit, switched branches and pulling from upstream but with no luck.
Oh, I haven't done this case so I cannot find out a solution :( sorry
Thank you anyways, i'm gonna make sure the fix is in the next release so you don't have to hijack the requests in order to get the values correctly. Cheers!
@dukuo You need to implement fix in this repo https://github.com/mauron85/background-geolocation-android. This is actual repository android/common is just link (submodule) to it.
Also before I can merge your solution, there should be ios implementation too.
Alright.. I don't own a OSX machine, so I'll see what I can do, for now
I'll fork the repo and figure out the iOS fix.
Thanks
On Mon, Aug 20, 2018, 14:30 Marián Hello notifications@github.com wrote:
@dukuo https://github.com/dukuo You need to implement fix in this repo
https://github.com/mauron85/background-geolocation-android. This is
actual repository android/common is just link (submodule) to it.Also before I can merge your solution, there should be ios implementation
too.—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mauron85/react-native-background-geolocation/issues/267#issuecomment-414398631,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAVNnA3-jYxXmLnH2EXDxOBgHLHV1yRzks5uSvI3gaJpZM4WC5ge
.
gj @dukuo! really want it merged
sorry I haven't replied. My mac is as good as dead, so if someone with one can help here that'll be awesome
Implemented in 0.5.0-alpha.45. Closing as resolved.
Most helpful comment
@dukuo You need to implement fix in this repo https://github.com/mauron85/background-geolocation-android. This is actual repository android/common is just link (submodule) to it.
Also before I can merge your solution, there should be ios implementation too.