use of org.structr.core.graphql.GraphQLQuery in project structr by structr.
the class GraphQLWriter method stream.
public void stream(final SecurityContext securityContext, final Writer output, final GraphQLRequest request) throws IOException, FrameworkException {
final RestWriter writer = new StructrJsonWriter(securityContext, output);
if (indent) {
writer.setIndent(" ");
}
if (request.hasSchemaQuery()) {
// schema query is serialized from GraphQL execution result, doesn't need enclosing JSON object
for (final GraphQLQuery query : request.getQueries()) {
if (query.isSchemaQuery()) {
// use graphql-java schema response
final String originalQuery = request.getOriginalQuery();
final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
final ExecutionResult result = graphQL.execute(originalQuery);
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
if (result != null) {
final Map<String, Object> data = result.getData();
if (data != null) {
gson.toJson(data, output);
}
}
}
}
} else {
writer.beginDocument(null, null);
writer.beginObject();
for (final GraphQLQuery query : request.getQueries()) {
if (query.isSchemaQuery()) {
// use graphql-java schema response
final String originalQuery = request.getOriginalQuery();
final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
final ExecutionResult result = graphQL.execute(originalQuery);
final Gson gson = new GsonBuilder().setPrettyPrinting().create();
if (result != null) {
final Map<String, Object> data = result.getData();
if (data != null) {
gson.toJson(data, output);
}
}
} else {
writer.name(query.getFieldName());
writer.beginArray();
for (final GraphObject object : query.getEntities(securityContext)) {
root.serialize(writer, null, object, query, query.getRootPath());
}
writer.endArray();
}
}
// finished
writer.endObject();
writer.endDocument();
}
}
use of org.structr.core.graphql.GraphQLQuery in project structr by structr.
the class GraphQLCommand method createResult.
private List<GraphObject> createResult(final SecurityContext securityContext, final GraphQLRequest request) throws IOException, FrameworkException {
final List<GraphObject> resultList = new LinkedList<>();
if (request.hasSchemaQuery()) {
// schema query is serialized from GraphQL execution result, doesn't need enclosing JSON object
for (final GraphQLQuery query : request.getQueries()) {
if (query.isSchemaQuery()) {
// use graphql-java schema response
final String originalQuery = request.getOriginalQuery();
final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
final ExecutionResult result = graphQL.execute(originalQuery);
if (result != null) {
resultList.add(GraphObjectMap.fromMap(result.getData()));
}
}
}
} else {
for (final GraphQLQuery query : request.getQueries()) {
if (query.isSchemaQuery()) {
// use graphql-java schema response
final String originalQuery = request.getOriginalQuery();
final GraphQL graphQL = GraphQL.newGraphQL(SchemaService.getGraphQLSchema()).build();
final ExecutionResult result = graphQL.execute(originalQuery);
if (result != null) {
resultList.add(GraphObjectMap.fromMap(result.getData()));
}
} else {
for (final GraphObject object : query.getEntities(securityContext)) {
resultList.add(object);
}
}
}
}
return resultList;
}
Aggregations