Skip to content

Commit

Permalink
[WIP] Add LT, LTE, GT, GTE options to GraphQL filters for Int/Float (#…
Browse files Browse the repository at this point in the history
…5676)

Add LT, LTE, GT, GTE options to GraphQL filters for Int/Float
  • Loading branch information
crgeary authored and pieh committed Jun 4, 2018
1 parent 778f5fa commit 31d8b0e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function isIntInput(type) {
expect(type.getFields()).toEqual({
eq: { name: `eq`, type: GraphQLInt },
ne: { name: `ne`, type: GraphQLInt },
lt: { name: `lt`, type: GraphQLInt },
lte: { name: `lte`, type: GraphQLInt },
gt: { name: `gt`, type: GraphQLInt },
gte: { name: `gte`, type: GraphQLInt },
})
}

Expand Down Expand Up @@ -89,6 +93,10 @@ describe(`GraphQL Input args from fields, test-only`, () => {
expect(float.getFields()).toEqual({
eq: { name: `eq`, type: GraphQLFloat },
ne: { name: `ne`, type: GraphQLFloat },
lt: { name: `lt`, type: GraphQLFloat },
lte: { name: `lte`, type: GraphQLFloat },
gt: { name: `gt`, type: GraphQLFloat },
gte: { name: `gte`, type: GraphQLFloat },
})

const string = inferredFields.scal_string.type
Expand Down Expand Up @@ -293,6 +301,10 @@ describe(`GraphQL Input args from fields, test-only`, () => {
expect(list.getFields()).toEqual({
eq: { name: `eq`, type: GraphQLFloat },
ne: { name: `ne`, type: GraphQLFloat },
gt: { name: `gt`, type: GraphQLFloat },
gte: { name: `gte`, type: GraphQLFloat },
lt: { name: `lt`, type: GraphQLFloat },
lte: { name: `lte`, type: GraphQLFloat },
in: { name: `in`, type: new GraphQLList(GraphQLFloat) },
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,78 @@ describe(`GraphQL Input args`, () => {
expect(result.data.allNode.edges[0].node.hair).toEqual(1)
})

it(`handles lt operator`, async () => {
let result = await queryResult(
nodes,
`
{
allNode(filter: {hair: { lt: 2 }}) {
edges { node { hair }}
}
}
`
)

expect(result.errors).not.toBeDefined()
expect(result.data.allNode.edges.length).toEqual(2)
expect(result.data.allNode.edges[0].node.hair).toEqual(1)
expect(result.data.allNode.edges[1].node.hair).toEqual(0)
})

it(`handles lte operator`, async () => {
let result = await queryResult(
nodes,
`
{
allNode(filter: {hair: { lte: 1 }}) {
edges { node { hair }}
}
}
`
)

expect(result.errors).not.toBeDefined()
expect(result.data.allNode.edges.length).toEqual(2)
expect(result.data.allNode.edges[0].node.hair).toEqual(1)
expect(result.data.allNode.edges[1].node.hair).toEqual(0)
})

it(`handles gt operator`, async () => {
let result = await queryResult(
nodes,
`
{
allNode(filter: {hair: { gt: 0 }}) {
edges { node { hair }}
}
}
`
)

expect(result.errors).not.toBeDefined()
expect(result.data.allNode.edges.length).toEqual(2)
expect(result.data.allNode.edges[0].node.hair).toEqual(1)
expect(result.data.allNode.edges[1].node.hair).toEqual(2)
})

it(`handles gte operator`, async () => {
let result = await queryResult(
nodes,
`
{
allNode(filter: {hair: { gte: 1 }}) {
edges { node { hair }}
}
}
`
)

expect(result.errors).not.toBeDefined()
expect(result.data.allNode.edges.length).toEqual(2)
expect(result.data.allNode.edges[0].node.hair).toEqual(1)
expect(result.data.allNode.edges[1].node.hair).toEqual(2)
})

it(`handles the regex operator`, async () => {
let result = await queryResult(
nodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,18 @@ const scalarFilterMap = {
Int: {
eq: { type: GraphQLInt },
ne: { type: GraphQLInt },
gt: { type: GraphQLInt },
gte: { type: GraphQLInt },
lt: { type: GraphQLInt },
lte: { type: GraphQLInt },
},
Float: {
eq: { type: GraphQLFloat },
ne: { type: GraphQLFloat },
gt: { type: GraphQLFloat },
gte: { type: GraphQLFloat },
lt: { type: GraphQLFloat },
lte: { type: GraphQLFloat },
},
ID: {
eq: { type: GraphQLID },
Expand Down
8 changes: 8 additions & 0 deletions packages/gatsby/src/schema/infer-graphql-input-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ function typeFields(type): GraphQLInputFieldConfigMap {
return {
eq: { type: GraphQLInt },
ne: { type: GraphQLInt },
gt: { type: GraphQLInt },
gte: { type: GraphQLInt },
lt: { type: GraphQLInt },
lte: { type: GraphQLInt },
}
case `float`:
return {
eq: { type: GraphQLFloat },
ne: { type: GraphQLFloat },
gt: { type: GraphQLFloat },
gte: { type: GraphQLFloat },
lt: { type: GraphQLFloat },
lte: { type: GraphQLFloat },
}
}
return {}
Expand Down

0 comments on commit 31d8b0e

Please sign in to comment.