Esempio n. 1
0
def rpc_graphql(handler, session, query, query_vars=None):
	"""
	Execute a GraphQL query and return the results. If the query fails to
	execute the errors returned are populated in the **errors** key of the
	results dictionary. If the query executes successfully the returned data
	is available in the **data** key of the results dictionary.

	:param str query: The GraphQL query to execute.
	:param dict query_vars: Any variables needed by the *query*.
	:return: The results of the query as a dictionary.
	:rtype: dict
	"""
	query_vars = query_vars or {}
	result = graphql.schema.execute(
		query,
		context_value={
			'plugin_manager': handler.server.plugin_manager,
			'rpc_session': handler.rpc_session,
			'session': session
		},
		variable_values=query_vars
	)
	errors = None
	if result.errors:
		errors = []
		for error in result.errors:
			if hasattr(error, 'message'):
				errors.append(error.message)
			elif hasattr(error, 'args') and error.args:
				errors.append(str(error.args[0]))
			else:
				errors.append(repr(error))
	return {'data': result.data, 'errors': errors}
Esempio n. 2
0
def rpc_graphql(handler, session, query, query_vars=None):
	"""
	Execute a GraphQL query and return the results. If the query fails to
	execute the errors returned are populated in the **errors** key of the
	results dictionary. If the query executes successfully the returned data
	is available in the **data** key of the results dictionary.

	:param str query: The GraphQL query to execute.
	:param dict query_vars: Any variables needed by the *query*.
	:return: The results of the query as a dictionary.
	:rtype: dict
	"""
	query_vars = query_vars or {}
	result = graphql_schema.execute(
		query,
		context_value={
			'plugin_manager': handler.server.plugin_manager,
			'rpc_session': handler.rpc_session,
			'server_config': handler.config,
			'session': session
		},
		variable_values=query_vars
	)
	errors = None
	if result.errors:
		errors = []
		for error in result.errors:
			if hasattr(error, 'message'):
				errors.append(error.message)
			elif hasattr(error, 'args') and error.args:
				errors.append(str(error.args[0]))
			else:
				errors.append(repr(error))
	return {'data': result.data, 'errors': errors}