|
39 | 39 | @GraphQLSchema |
40 | 40 | public class TodoSchema { |
41 | 41 |
|
42 | | - @GraphQLSchemaQuery |
43 | | - private RootObjectType root; |
44 | | - |
45 | | - private UserObjectType theOnlyUser = new UserObjectType(); |
46 | | - private List<TodoObjectType> todos = new ArrayList<>(); |
47 | | - |
48 | | - private TodoSimpleListConnection simpleConnectionTodo; |
49 | | - private int nextTodoId = 0; |
50 | | - |
51 | | - |
52 | | - public TodoSchema() { |
53 | | - addTodo("Do Something"); |
54 | | - addTodo("Other todo"); |
55 | | - |
56 | | - simpleConnectionTodo = new TodoSimpleListConnection(todos); |
57 | | - } |
58 | | - |
59 | | - public String addTodo(String text) { |
60 | | - TodoObjectType newTodo = new TodoObjectType(); |
61 | | - newTodo.setId(Integer.toString(nextTodoId++)); |
62 | | - newTodo.setText(text); |
63 | | - todos.add(newTodo); |
64 | | - return newTodo.getId(newTodo); |
65 | | - } |
66 | | - |
67 | | - public void removeTodo(String id) { |
68 | | - TodoObjectType del = todos.stream().filter(todo -> todo.getId(todo).equals(id)).findFirst().get(); |
69 | | - todos.remove(del); |
70 | | - } |
71 | | - |
72 | | - public void renameTodo(String id, String text) { |
73 | | - TodoObjectType matchedTodo = todos.stream().filter(todo -> todo.getId(todo).equals(id)).findFirst().get(); |
74 | | - matchedTodo.setText(text); |
75 | | - } |
76 | | - |
77 | | - public List<String> removeCompletedTodos() { |
78 | | - List<String> toDelete = todos.stream() |
79 | | - .filter(TodoObjectType::isComplete) |
80 | | - .map(todoObjectType -> todoObjectType.getId(todoObjectType)) |
81 | | - .collect(Collectors.toList()); |
82 | | - todos.removeIf(todo -> toDelete.contains(todo.getId(todo))); |
83 | | - return toDelete; |
84 | | - } |
85 | | - |
86 | | - public List<String> markAllTodos(boolean complete) { |
87 | | - List<String> changed = new ArrayList<>(); |
88 | | - todos.stream().filter(todo -> complete != todo.isComplete()).forEach(todo -> { |
89 | | - changed.add(todo.getId(todo)); |
90 | | - todo.setComplete(complete); |
91 | | - }); |
92 | | - |
93 | | - return changed; |
94 | | - } |
95 | | - |
96 | | - public void changeTodoStatus(String id, boolean complete) { |
97 | | - TodoObjectType todo = getTodo(id); |
98 | | - todo.setComplete(complete); |
99 | | - } |
100 | | - |
101 | | - public TodoObjectType getTodo(String id) { |
102 | | - return todos.stream().filter(todo -> todo.getId(todo).equals(id)).findFirst().get(); |
103 | | - } |
104 | | - |
105 | | - public List<TodoObjectType> getTodos(List<String> ids) { |
106 | | - return todos.stream().filter(todo -> ids.contains(todo.getId(todo))).collect(Collectors.toList()); |
107 | | - } |
108 | | - |
109 | | - public UserObjectType getTheOnlyUser() { |
110 | | - return theOnlyUser; |
111 | | - } |
112 | | - |
113 | | - |
114 | | - public TodoSimpleListConnection getSimpleConnectionTodo() { |
115 | | - return simpleConnectionTodo; |
116 | | - } |
117 | | - |
118 | | - public static class AddTodoIn { |
119 | | - private String text; |
120 | | - |
121 | | - public String getText() { |
122 | | - return text; |
123 | | - } |
124 | | - |
125 | | - public void setText(String text) { |
126 | | - this.text = text; |
127 | | - } |
128 | | - } |
129 | | - |
130 | | - // --- mutations |
131 | | - |
132 | | - @GraphQLMutation |
133 | | - @GraphQLDescription("Mutation to add new todo item") |
134 | | - public |
135 | | - @GraphQLOut("todoEdge") |
136 | | - TodoObjectType.TodoEdgeObjectType addTodoMutation(@GraphQLIn("addTodoInput") AddTodoIn addTodoInput) { |
137 | | - |
138 | | - TodoObjectType.TodoEdgeObjectType todoEdgeObjectType = new TodoObjectType.TodoEdgeObjectType(); |
139 | | - todoEdgeObjectType.setCursor("test-cursor"); |
140 | | - todoEdgeObjectType.setNode(new TodoObjectType()); |
141 | | - todoEdgeObjectType.getNode().setId("id-12345"); |
142 | | - todoEdgeObjectType.getNode().setText("simple text"); |
143 | | - todoEdgeObjectType.getNode().setComplete(false); |
144 | | - |
145 | | - return todoEdgeObjectType; |
146 | | - } |
147 | | - |
148 | | - @GraphQLMutation |
149 | | - public |
150 | | - @GraphQLOut("filename") |
151 | | - String uploadFile(GraphQLServletContext graphQLContext) { |
152 | | - return graphQLContext.getParts().values().stream().flatMap(Collection::stream).map(Part::getName).collect(Collectors.joining(", ")); |
| 42 | + @GraphQLSchemaQuery |
| 43 | + private RootObjectType root; |
| 44 | + |
| 45 | + private UserObjectType theOnlyUser = new UserObjectType(); |
| 46 | + private List<TodoObjectType> todos = new ArrayList<>(); |
| 47 | + |
| 48 | + private TodoSimpleListConnection simpleConnectionTodo; |
| 49 | + private int nextTodoId = 0; |
| 50 | + |
| 51 | + |
| 52 | + public TodoSchema() { |
| 53 | + addTodo("Do Something"); |
| 54 | + addTodo("Other todo"); |
| 55 | + |
| 56 | + simpleConnectionTodo = new TodoSimpleListConnection(todos); |
| 57 | + } |
| 58 | + |
| 59 | + public String addTodo(String text) { |
| 60 | + TodoObjectType newTodo = new TodoObjectType(); |
| 61 | + newTodo.setId(Integer.toString(nextTodoId++)); |
| 62 | + newTodo.setText(text); |
| 63 | + todos.add(newTodo); |
| 64 | + return newTodo.getId(newTodo); |
| 65 | + } |
| 66 | + |
| 67 | + public void removeTodo(String id) { |
| 68 | + todos.stream() |
| 69 | + .filter(todo -> todo.getId(todo).equals(id)) |
| 70 | + .findFirst() |
| 71 | + .ifPresent(it -> todos.remove(it)); |
| 72 | + } |
| 73 | + |
| 74 | + public void renameTodo(String id, String text) { |
| 75 | + todos.stream() |
| 76 | + .filter(todo -> todo.getId(todo).equals(id)) |
| 77 | + .findFirst() |
| 78 | + .ifPresent(it -> it.setText(text)); |
| 79 | + } |
| 80 | + |
| 81 | + public List<String> removeCompletedTodos() { |
| 82 | + List<String> toDelete = todos.stream() |
| 83 | + .filter(TodoObjectType::isComplete) |
| 84 | + .map(todoObjectType -> todoObjectType.getId(todoObjectType)) |
| 85 | + .collect(Collectors.toList()); |
| 86 | + todos.removeIf(todo -> toDelete.contains(todo.getId(todo))); |
| 87 | + return toDelete; |
| 88 | + } |
| 89 | + |
| 90 | + public List<String> markAllTodos(boolean complete) { |
| 91 | + List<String> changed = new ArrayList<>(); |
| 92 | + todos.stream().filter(todo -> complete != todo.isComplete()).forEach(todo -> { |
| 93 | + changed.add(todo.getId(todo)); |
| 94 | + todo.setComplete(complete); |
| 95 | + }); |
| 96 | + |
| 97 | + return changed; |
| 98 | + } |
| 99 | + |
| 100 | + public void changeTodoStatus(String id, boolean complete) { |
| 101 | + TodoObjectType todo = getTodo(id); |
| 102 | + todo.setComplete(complete); |
| 103 | + } |
| 104 | + |
| 105 | + public TodoObjectType getTodo(String id) { |
| 106 | + return todos.stream() |
| 107 | + .filter(todo -> todo.getId(todo).equals(id)) |
| 108 | + .findFirst() |
| 109 | + .orElseThrow(() -> throw IllegalStateException("No todo found with id " + id)); |
| 110 | + } |
| 111 | + |
| 112 | + public List<TodoObjectType> getTodos(List<String> ids) { |
| 113 | + return todos.stream().filter(todo -> ids.contains(todo.getId(todo))) |
| 114 | + .collect(Collectors.toList()); |
| 115 | + } |
| 116 | + |
| 117 | + public UserObjectType getTheOnlyUser() { |
| 118 | + return theOnlyUser; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + public TodoSimpleListConnection getSimpleConnectionTodo() { |
| 123 | + return simpleConnectionTodo; |
| 124 | + } |
| 125 | + |
| 126 | + public static class AddTodoIn { |
| 127 | + |
| 128 | + private String text; |
| 129 | + |
| 130 | + public String getText() { |
| 131 | + return text; |
153 | 132 | } |
154 | 133 |
|
155 | | - @GraphQLMutation |
156 | | - public String updateTodoMutation(@GraphQLIn("updateTodoInput") String newText) { |
157 | | - return "Simple output string"; |
| 134 | + public void setText(String text) { |
| 135 | + this.text = text; |
158 | 136 | } |
| 137 | + } |
| 138 | + |
| 139 | + // --- mutations |
| 140 | + |
| 141 | + @GraphQLMutation |
| 142 | + @GraphQLDescription("Mutation to add new todo item") |
| 143 | + public |
| 144 | + @GraphQLOut("todoEdge") |
| 145 | + TodoObjectType.TodoEdgeObjectType addTodoMutation( |
| 146 | + @GraphQLIn("addTodoInput") AddTodoIn addTodoInput) { |
| 147 | + |
| 148 | + TodoObjectType.TodoEdgeObjectType todoEdgeObjectType = new TodoObjectType.TodoEdgeObjectType(); |
| 149 | + todoEdgeObjectType.setCursor("test-cursor"); |
| 150 | + todoEdgeObjectType.setNode(new TodoObjectType()); |
| 151 | + todoEdgeObjectType.getNode().setId("id-12345"); |
| 152 | + todoEdgeObjectType.getNode().setText("simple text"); |
| 153 | + todoEdgeObjectType.getNode().setComplete(false); |
| 154 | + |
| 155 | + return todoEdgeObjectType; |
| 156 | + } |
| 157 | + |
| 158 | + @GraphQLMutation |
| 159 | + public |
| 160 | + @GraphQLOut("filename") |
| 161 | + String uploadFile(GraphQLServletContext graphQLContext) { |
| 162 | + return graphQLContext.getParts().values().stream().flatMap(Collection::stream) |
| 163 | + .map(Part::getName).collect(Collectors.joining(", ")); |
| 164 | + } |
| 165 | + |
| 166 | + @GraphQLMutation |
| 167 | + public String updateTodoMutation(@GraphQLIn("updateTodoInput") String newText) { |
| 168 | + return "Simple output string"; |
| 169 | + } |
159 | 170 |
|
160 | 171 | } |
0 commit comments