Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
feat: handle graphql list and id type (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicky Lenaers authored Mar 11, 2019
1 parent 7ef50aa commit 70f4dff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/pagination/types/filter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLInterfaceType, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
import { GraphQLID, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql';
import { mockServer } from 'graphql-tools';
import { FilterType } from './filter';

Expand All @@ -9,8 +9,10 @@ describe('FilterType', () => {
const TestType = new GraphQLObjectType({
name: 'TestType',
fields: () => ({
id: { type: new GraphQLNonNull(GraphQLID) },
foo: { type: GraphQLString },
bar: { type: new GraphQLNonNull(GraphQLString) }
bar: { type: new GraphQLNonNull(GraphQLString) },
baz: { type: new GraphQLList(GraphQLString) }
})
});

Expand All @@ -28,17 +30,26 @@ describe('FilterType', () => {
})
});

const response = { foo: 'Foo', bar: 'Bar' };
const response = { id: '1', foo: 'Foo', bar: 'Bar', baz: ['Baz'] };
const server = mockServer(schema, { TestType: () => response });

const res = await server.query(`
query testFilter($filters: [FilterTestType]) {
testFilter(filters: $filters) {
id,
foo,
bar
bar,
baz
}
}
`, { filters: [{ foo: { eq: 'Foo' }, bar: { eq: 'Bar' } }] });
`, {
filters: [{
id: { eq: '1' },
foo: { eq: 'Foo' },
bar: { ne: 'Foor' },
baz: { contains: { value: 'BAZ', options: 'i' } }
}]
});

expect(res).toEqual({ data: { testFilter: response } });
});
Expand Down
24 changes: 18 additions & 6 deletions src/pagination/types/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLInputFieldConfigMap, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLString, GraphQLType } from 'graphql';
import { GraphQLID, GraphQLInputFieldConfigMap, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLList, GraphQLNonNull, GraphQLObjectType, GraphQLOutputType, GraphQLString, GraphQLType } from 'graphql';

/** Filter Contains Type */
const FilterContainsType = new GraphQLInputObjectType({
Expand Down Expand Up @@ -45,12 +45,10 @@ export function FilterType(type: GraphQLObjectType | GraphQLInterfaceType): Grap

const [name, value] = field;
const filters: GraphQLInputFieldConfigMap = {};
const valueType = getValueType(value.type);

const valueType = value.type instanceof GraphQLNonNull
? value.type.ofType
: value.type;

switch (valueType.toString()) {
switch (valueType) {
case GraphQLID.name:
case GraphQLString.name: {
filters[name] = {
type: new GraphQLInputObjectType({
Expand All @@ -70,3 +68,17 @@ export function FilterType(type: GraphQLObjectType | GraphQLInterfaceType): Grap
)
}));
}

/**
* Retrieve the Value Type of a GraphQL Type.
*
* @param type GraphQL Type
* @returns GraphQL Type Name
*/
function getValueType(type: GraphQLOutputType): string {

if (type instanceof GraphQLNonNull) return type.ofType.toString();
if (type instanceof GraphQLList) return type.ofType.toString();

return type.toString();
}

0 comments on commit 70f4dff

Please sign in to comment.