Search in sources :

Example 1 with GraphQLQuery

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();
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) GraphQL(graphql.GraphQL) Gson(com.google.gson.Gson) ExecutionResult(graphql.ExecutionResult) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) GraphQLQuery(org.structr.core.graphql.GraphQLQuery)

Example 2 with GraphQLQuery

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;
}
Also used : GraphQL(graphql.GraphQL) ExecutionResult(graphql.ExecutionResult) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) GraphQLQuery(org.structr.core.graphql.GraphQLQuery)

Aggregations

ExecutionResult (graphql.ExecutionResult)2 GraphQL (graphql.GraphQL)2 GraphObject (org.structr.core.GraphObject)2 GraphQLQuery (org.structr.core.graphql.GraphQLQuery)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 LinkedList (java.util.LinkedList)1