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);
}
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);
}
Aggregations