mirror of
https://github.com/wassname/talk.git
synced 2026-08-08 11:28:12 +08:00
[CORL 133] API Review (#2197)
* refactor: removed unused subscription code
* refactor: removed management api's
* refactor: cleanup of connections
* refactor: refactored comments edge
* refactor: simplified connection resolving
* feat: added story connection edge
* fix: added story index
* feat: added user pagination and user edge
* fix: added filter to comment query
* fix: removed unused resolvers
* fix: creating a comment reply should require auth
* refactor: cleanup of graph files
* feat: removed display name, made username non-unique
* fix: fixed tests
* fix: fixed tests
* fix: added more api docs
* fix: fixed bug with installer
* refactor: fixes and updates
* fix: added linting for graphql, fixed schema
* feat: added docker build tests
* fix: upped output timeout
* fix: fixed stacktraces in production builds
* fix: removed `git add`
- `git add` was causing issues with
partial staged changs on files
* feat: improved error messaging for auth
* refactor: cleaned up queue names
* fix: merge error
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { merge } from "lodash";
|
||||
|
||||
import { FilterQuery } from "talk-server/models/helpers/query";
|
||||
import Query, { FilterQuery } from "talk-server/models/helpers/query";
|
||||
|
||||
export type Cursor = Date | number | string | null;
|
||||
|
||||
@@ -10,14 +10,15 @@ export interface Edge<T> {
|
||||
}
|
||||
|
||||
export interface PageInfo {
|
||||
hasNextPage?: boolean;
|
||||
hasPreviousPage?: boolean;
|
||||
hasNextPage: boolean;
|
||||
hasPreviousPage: boolean;
|
||||
startCursor?: Cursor;
|
||||
endCursor?: Cursor;
|
||||
}
|
||||
|
||||
export interface Connection<T> {
|
||||
edges: Array<Edge<T>>;
|
||||
nodes: T[];
|
||||
pageInfo: PageInfo;
|
||||
}
|
||||
|
||||
@@ -59,6 +60,7 @@ export function createConnection<T>(
|
||||
return merge(
|
||||
{
|
||||
edges: [],
|
||||
nodes: [],
|
||||
pageInfo: {
|
||||
hasNextPage: false,
|
||||
hasPreviousPage: false,
|
||||
@@ -117,3 +119,53 @@ export function nodesToEdges<T>(
|
||||
cursor: transformer(node, index),
|
||||
}));
|
||||
}
|
||||
|
||||
export function doesNotContainNull<T>(items: Array<T | null>): items is T[] {
|
||||
return items.every(item => Boolean(item));
|
||||
}
|
||||
|
||||
/**
|
||||
* resolveConnection will transform a query, pagination args into a full typed
|
||||
* connection. This will add `1` to the length of elements being retrieved to
|
||||
* determine if there is another page of results to be loaded. The additional
|
||||
* node requested will be trimmed from the connection output.
|
||||
*
|
||||
* @param query the query to use when retrieving the documents.
|
||||
* @param input the pagination arguments
|
||||
* @param transformer the node transformer which converts a node to a custor
|
||||
*/
|
||||
export async function resolveConnection<T>(
|
||||
query: Query<T>,
|
||||
input: PaginationArgs,
|
||||
transformer: NodeToCursorTransformer<T>
|
||||
) {
|
||||
// We load one more than the limit so we can determine if there is another
|
||||
// page of entries. This gets trimmed off below after we've checked to see if
|
||||
// this constitutes another page of edges.
|
||||
query.first(input.first + 1);
|
||||
|
||||
// Get the nodes.
|
||||
const nodes = await query.exec().then(cursor => cursor.toArray());
|
||||
|
||||
// Convert the nodes to edges (which will include the extra edge we don't need
|
||||
// if there is more results).
|
||||
const edges = nodesToEdges(nodes, transformer);
|
||||
|
||||
// Get the pageInfo for the connection. We will use this to also determine if
|
||||
// we need to trim off the extra edge that we requested by comparing its
|
||||
// hasNextPage parameter.
|
||||
const pageInfo = getPageInfo(input, edges);
|
||||
if (pageInfo.hasNextPage) {
|
||||
// Because this means that we got one more than expected, we should trim off
|
||||
// the extra edge that was retrieved.
|
||||
edges.splice(input.first, 1);
|
||||
nodes.splice(input.first, 1);
|
||||
}
|
||||
|
||||
// Return the connection.
|
||||
return {
|
||||
edges,
|
||||
nodes,
|
||||
pageInfo,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { merge } from "lodash";
|
||||
import { isUndefined, merge, omitBy } from "lodash";
|
||||
|
||||
import {
|
||||
Collection,
|
||||
Cursor,
|
||||
@@ -38,7 +39,7 @@ export default class Query<T> {
|
||||
* @param filter the filter to merge into the existing query
|
||||
*/
|
||||
public where(filter: FilterQuery<T>): Query<T> {
|
||||
this.filter = merge({}, this.filter || {}, filter);
|
||||
this.filter = merge({}, this.filter || {}, omitBy(filter, isUndefined));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -76,6 +77,17 @@ export default class Query<T> {
|
||||
* exec will return a cursor to the query.
|
||||
*/
|
||||
public async exec(): Promise<Cursor<T>> {
|
||||
logger.trace(
|
||||
{
|
||||
collection: this.collection.collectionName,
|
||||
filter: this.filter,
|
||||
limit: this.limit,
|
||||
sort: this.sort,
|
||||
skip: this.skip,
|
||||
},
|
||||
"executing query"
|
||||
);
|
||||
|
||||
let cursor = await this.collection.find(this.filter);
|
||||
|
||||
if (this.limit) {
|
||||
|
||||
Reference in New Issue
Block a user