Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort Field Type Function #249

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions src/buildSortFromArg.ts

This file was deleted.

156 changes: 17 additions & 139 deletions src/connectionDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,156 +1,34 @@
import {
GraphQLBoolean,
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldResolver,
GraphQLInt,
GraphQLInterfaceType,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLString,
Thunk,
} from 'graphql';

export const forwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
after: {
type: GraphQLString,
},
first: {
type: GraphQLInt,
},
};

export const backwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
before: {
type: GraphQLString,
},
last: {
type: GraphQLInt,
},
};

export const connectionArgs: GraphQLFieldConfigArgumentMap = {
...forwardConnectionArgs,
...backwardConnectionArgs,
};

export type GraphQLConnectionDefinitions = {
edgeType: GraphQLObjectType;
connectionType: GraphQLObjectType;
};

export const PageInfoType = new GraphQLObjectType({
name: 'PageInfo',
description: 'Information about pagination in a connection.',
fields: () => ({
hasNextPage: {
type: GraphQLNonNull(GraphQLBoolean),
description: 'When paginating forwards, are there more items?',
},
hasPreviousPage: {
type: GraphQLNonNull(GraphQLBoolean),
description: 'When paginating backwards, are there more items?',
},
startCursor: {
type: GraphQLString,
description: 'When paginating backwards, the cursor to continue.',
},
endCursor: {
type: GraphQLString,
description: 'When paginating forwards, the cursor to continue.',
},
}),
});

const connectionProps = {
count: {
type: GraphQLInt,
description: 'Number of items in this connection.',
},
totalCount: {
type: GraphQLInt,
resolve: (connection) => connection.count,
description: `A count of the total number of objects in this connection, ignoring pagination.
This allows a client to fetch the first five objects by passing "5" as the
argument to "first", then fetch the total count so it could display "5 of 83",
for example.`,
},
startCursorOffset: {
type: GraphQLNonNull(GraphQLInt),
description: 'Offset from start.',
},
endCursorOffset: {
type: GraphQLNonNull(GraphQLInt),
description: 'Offset till end.',
},
pageInfo: {
type: GraphQLNonNull(PageInfoType),
description: 'Information to aid in pagination.',
},
};

export const ConnectionInterface = new GraphQLInterfaceType({
name: `Connection`,
description: 'A connection to a list of items.',
fields: () => ({
...connectionProps,
}),
});

function resolveMaybeThunk<T>(thingOrThunk: Thunk<T>): T {
return typeof thingOrThunk === 'function' ? (thingOrThunk as () => T)() : thingOrThunk;
}
import { connectionDefinitions as connectionDefinitionsRelay } from 'graphql-relay';

interface ConnectionConfig {
name?: string | null;
description?: string | null;
name?: string;
nodeType: GraphQLObjectType;
edgeDescription?: string | null;
resolveNode?: GraphQLFieldResolver<any, any> | null;
resolveCursor?: GraphQLFieldResolver<any, any> | null;
edgeFields?: Thunk<GraphQLFieldConfigMap<any, any>> | null;
connectionFields?: Thunk<GraphQLFieldConfigMap<any, any>> | null;
resolveNode?: GraphQLFieldResolver<any, any>;
resolveCursor?: GraphQLFieldResolver<any, any>;
edgeFields?: Thunk<GraphQLFieldConfigMap<any, any>>;
connectionFields?: Thunk<GraphQLFieldConfigMap<any, any>>;
}

export const connectionDefinitions = (config: ConnectionConfig): GraphQLConnectionDefinitions => {
const { nodeType, resolveCursor, resolveNode } = config;

const name = config.name || nodeType.name;
const edgeFields = config.edgeFields || {};
const connectionFields = config.connectionFields || {};

const edgeType = new GraphQLObjectType({
name: `${name}Edge`,
description: config.edgeDescription || 'An edge in a connection.',
fields: () => ({
node: {
type: nodeType,
resolve: resolveNode,
description: 'The item at the end of the edge.',
},
cursor: {
type: GraphQLNonNull(GraphQLString),
resolve: resolveCursor,
description: 'A cursor for use in pagination.',
export const connectionDefinitions = (config: ConnectionConfig) => {
return connectionDefinitionsRelay({
...config,
connectionFields: {
startCursorOffset: {
type: GraphQLNonNull(GraphQLInt),
description: 'Offset from start.',
},
...(resolveMaybeThunk(edgeFields) as any),
}),
});

const connectionType = new GraphQLObjectType({
name: `${name}Connection`,
description: config.description || 'A connection to a list of items.',
fields: () => ({
...connectionProps,
edges: {
type: GraphQLNonNull(GraphQLList(edgeType)),
description: 'A list of edges.',
endCursorOffset: {
type: GraphQLNonNull(GraphQLInt),
description: 'Offset till end.',
},
...(resolveMaybeThunk(connectionFields) as any),
}),
interfaces: [ConnectionInterface],
},
});

return { edgeType, connectionType };
};
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ it('should return correct sort', async () => {
},
];

expect(buildSortFromArg(sortArg)).toMatchInlineSnapshot(`
Object {
"user": -1,
}
`);
expect(buildSortFromArg(sortArg)).toEqual({
user: -1,
});
});
18 changes: 18 additions & 0 deletions src/sorting/buildSortFromArg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type SortValues = 1 | -1 | number;

export interface GraphqlSortArg<SortFieldT extends string> {
field: SortFieldT;
direction: SortValues;
}

export default function buildSortFromArg<DocumentField extends string>(
orderByArg: GraphqlSortArg<DocumentField>[],
): Record<string, SortValues> {
return orderByArg.reduce(
(acc, item) => ({
...acc,
[item.field]: item.direction,
}),
{},
);
}
24 changes: 24 additions & 0 deletions src/sorting/createSortFieldType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GraphQLInputObjectType, GraphQLNonNull, GraphQLEnumType, GraphQLEnumValueConfigMap } from 'graphql';

import { DirectionEnumType } from './DirectionEnumType';

export const createSortFieldType = (name: string, values: GraphQLEnumValueConfigMap) => {
const SortFieldEnumType = new GraphQLEnumType({
name: `${name}SortFieldEnum`,
values: values,
});

const SortInputType = new GraphQLInputObjectType({
name: `${name}Sort`,
fields: () => ({
field: {
type: GraphQLNonNull(SortFieldEnumType),
},
direction: {
type: GraphQLNonNull(DirectionEnumType),
},
}),
});

return { SortFieldEnumType, SortInputType };
};
8 changes: 0 additions & 8 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,4 @@ export interface GraphQLArgFilter {
filter: GraphQLFilter;
}

// this should be 1 | -1, but it's going to be harder to use from client if that was the case.
export type SortDirection = number;

export interface GraphqlSortArg<SortFieldT extends string> {
field: SortFieldT;
direction: SortDirection;
}

export type LoaderFn<Context extends object> = (ctx: Context, id: DataLoaderKey) => any;