Search in sources :

Example 1 with DefaultEdge

use of graphql.relay.DefaultEdge in project synapse by americanexpress.

the class ConnectionUtil method create.

/**
 * Create the {@link Connection}.
 * @param <T> type of {@link UniversallyUniqueIdentifiable} element
 * @param elements used to create the edges of the connection
 * @param first number of elements
 * @param after the opaque cursor
 * @return the {@link Connection}
 */
public static final <T extends UniversallyUniqueIdentifiable> Connection<T> create(List<T> elements, long first, String after) {
    // Get the limit of this stream which is either the number of elements
    // specified by "first"; otherwise the full elements size
    long limit = first > 0 ? first : elements.size();
    // Create the edges for the connection
    List<Edge<T>> edges = elements.stream().limit(limit).map(element -> new DefaultEdge<>(element, ConnectionCursorUtil.from(element.getId()))).collect(Collectors.toList());
    // Create the page information for the connection
    ConnectionCursor startCursor = ConnectionCursorUtil.getStartCursor(edges);
    ConnectionCursor endCursor = ConnectionCursorUtil.getEndCursor(edges);
    boolean hasPreviousPage = after != null;
    boolean hasNextPage = edges.size() >= first;
    PageInfo pageInfo = new DefaultPageInfo(startCursor, endCursor, hasPreviousPage, hasNextPage);
    return new DefaultConnection<>(edges, pageInfo);
}
Also used : PageInfo(graphql.relay.PageInfo) List(java.util.List) DefaultPageInfo(graphql.relay.DefaultPageInfo) Connection(graphql.relay.Connection) DefaultConnection(graphql.relay.DefaultConnection) UniversallyUniqueIdentifiable(io.americanexpress.synapse.service.graphql.model.UniversallyUniqueIdentifiable) Edge(graphql.relay.Edge) ConnectionCursor(graphql.relay.ConnectionCursor) DefaultEdge(graphql.relay.DefaultEdge) Collectors(java.util.stream.Collectors) ConnectionCursor(graphql.relay.ConnectionCursor) PageInfo(graphql.relay.PageInfo) DefaultPageInfo(graphql.relay.DefaultPageInfo) DefaultConnection(graphql.relay.DefaultConnection) DefaultEdge(graphql.relay.DefaultEdge) Edge(graphql.relay.Edge) DefaultEdge(graphql.relay.DefaultEdge) DefaultPageInfo(graphql.relay.DefaultPageInfo)

Example 2 with DefaultEdge

use of graphql.relay.DefaultEdge in project light-example-4j by networknt.

the class TodoSchemaMutations method createAddTodoMutation.

private void createAddTodoMutation() {
    GraphQLInputObjectField textField = newInputObjectField().name("text").type(new GraphQLNonNull(GraphQLString)).build();
    List<GraphQLInputObjectField> inputFields = Arrays.asList(textField);
    GraphQLFieldDefinition todoEdge = newFieldDefinition().name("todoEdge").type(todoSchema.getTodosEdge()).dataFetcher(environment -> {
        Map source = (Map) environment.getSource();
        String todoId = (String) source.get("todoId");
        Todo todo = todoSchema.getTodo(todoId);
        return new DefaultEdge(todo, todoSchema.getSimpleConnection().cursorForObjectInConnection(todo));
    }).build();
    List<GraphQLFieldDefinition> outputFields = Arrays.asList(todoEdge, getViewerField());
    DataFetcher mutate = environment -> {
        Map<String, Object> input = environment.getArgument("input");
        String text = (String) input.get("text");
        String newId = todoSchema.addTodo(text);
        Map<String, String> result = new LinkedHashMap<>();
        result.put("clientMutationId", (String) input.get("clientMutationId"));
        result.put("todoId", newId);
        return result;
    };
    addTodo = todoSchema.getRelay().mutationWithClientMutationId("AddTodo", "addTodo", inputFields, outputFields, mutate);
}
Also used : Arrays(java.util.Arrays) GraphQLString(graphql.Scalars.GraphQLString) DefaultEdge(graphql.relay.DefaultEdge) GraphQLID(graphql.Scalars.GraphQLID) Collectors(java.util.stream.Collectors) GraphQLFieldDefinition.newFieldDefinition(graphql.schema.GraphQLFieldDefinition.newFieldDefinition) GraphQLInputObjectField.newInputObjectField(graphql.schema.GraphQLInputObjectField.newInputObjectField) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) GraphQLBoolean(graphql.Scalars.GraphQLBoolean) Map(java.util.Map) Edge(graphql.relay.Edge) graphql.schema(graphql.schema) DefaultEdge(graphql.relay.DefaultEdge) GraphQLString(graphql.Scalars.GraphQLString) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

DefaultEdge (graphql.relay.DefaultEdge)2 Edge (graphql.relay.Edge)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 GraphQLBoolean (graphql.Scalars.GraphQLBoolean)1 GraphQLID (graphql.Scalars.GraphQLID)1 GraphQLString (graphql.Scalars.GraphQLString)1 Connection (graphql.relay.Connection)1 ConnectionCursor (graphql.relay.ConnectionCursor)1 DefaultConnection (graphql.relay.DefaultConnection)1 DefaultPageInfo (graphql.relay.DefaultPageInfo)1 PageInfo (graphql.relay.PageInfo)1 graphql.schema (graphql.schema)1 GraphQLFieldDefinition.newFieldDefinition (graphql.schema.GraphQLFieldDefinition.newFieldDefinition)1 GraphQLInputObjectField.newInputObjectField (graphql.schema.GraphQLInputObjectField.newInputObjectField)1 UniversallyUniqueIdentifiable (io.americanexpress.synapse.service.graphql.model.UniversallyUniqueIdentifiable)1 Arrays (java.util.Arrays)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1