All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.graphql_java_generator.server.util.QueryParameters Maven / Gradle / Ivy

There is a newer version: 1.18
Show newest version
package com.graphql_java_generator.server.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Graphql clients can send GET or POST HTTP requests. The spec does not make an explicit distinction. So you may need
 * to handle both. The following was tested using a graphiql client tool found here :
 * https://github.com/skevy/graphiql-app
 *
 * You should consider bundling graphiql in your application
 *
 * https://github.com/graphql/graphiql
 *
 * This outlines more information on how to handle parameters over http
 *
 * http://graphql.org/learn/serving-over-http/
 */
public class QueryParameters {

	private String query;
	private String operationName;
	private Map variables = Collections.emptyMap();

	public String getQuery() {
		return query;
	}

	public String getOperationName() {
		return operationName;
	}

	public Map getVariables() {
		return variables;
	}

	public static QueryParameters from(String queryMessage) {
		QueryParameters parameters = new QueryParameters();
		Map json = JsonKit.toMap(queryMessage);
		parameters.query = (String) json.get("query");
		parameters.operationName = (String) json.get("operationName");
		parameters.variables = getVariables(json.get("variables"));
		return parameters;
	}

	private static Map getVariables(Object variables) {
		if (variables instanceof Map) {
			Map inputVars = (Map) variables;
			Map vars = new HashMap<>();
			inputVars.forEach((k, v) -> vars.put(String.valueOf(k), v));
			return vars;
		}
		return JsonKit.toMap(String.valueOf(variables));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy