net.morimekta.providence.graphql.gql.GQLUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of providence-graphql Show documentation
Show all versions of providence-graphql Show documentation
Providence Core extension for GraphQL.
package net.morimekta.providence.graphql.gql;
import net.morimekta.providence.PMessage;
import net.morimekta.providence.descriptor.PField;
import net.morimekta.util.Binary;
import net.morimekta.util.Strings;
import net.morimekta.util.json.JsonWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
public class GQLUtil {
public static String toArgumentString(Object o) {
if (o instanceof PMessage) {
PMessage m = (PMessage) o;
StringBuilder builder = new StringBuilder("{");
boolean first = true;
for (PField pf : m.descriptor().getFields()) {
if (m.has(pf.getId())) {
if (!first) {
builder.append(", ");
} else {
first = false;
}
builder.append(pf.getName())
.append(": ")
.append(toArgumentString(m.get(pf.getId())));
}
}
return builder.append("}").toString();
} else if (o instanceof Binary) {
return "\"" + ((Binary) o).toBase64() + "\"";
} else if (o instanceof CharSequence) {
StringWriter writer = new StringWriter();
new JsonWriter(new PrintWriter(writer))
.value((CharSequence) o)
.flush();
return writer.toString();
} else {
return Strings.asString(o);
}
}
}