@dialexa/pleco-graphql
v0.1.3
Published
# Pleco GraphQL
Downloads
1
Readme
Pleco GraphQL
Table of Contents
Overview
This library provides GraphQL types that can be used to form your own filter types
GraphQL Types
All the GraphQL types can be found in src/graphql/index.ts
. For each GraphQLScalarType
(excluding Boolean
),
which includes ID
, Int
, String
, and Float
, we provide a GraphQL type
allowing users to provide comparison operations like in
, lt
, gt
, eq
, as well as the nesting
abilities with AND
and OR
.
Provided Exports
| operation | FilterQuery_Boolean | FilterQuery_ID | FilterQuery_String | FilterQuery_Int | FilterQuery_Float | |-----------|--------------------- |-----------------|---------------------|------------------|--------------------| | AND | ✔ | ✔ | ✔ | ✔ | ✔ | | OR | ✔ | ✔ | ✔ | ✔ | ✔ | | in | ✔ | ✔ | ✔ | ✔ | ✔ | | nin | ✔ | ✔ | ✔ | ✔ | ✔ | | eq | ✔ | ✔ | ✔ | ✔ | ✔ | | ne | ✔ | ✔ | ✔ | ✔ | ✔ | | gt | | | ✔ | ✔ | ✔ | | lt | | | ✔ | ✔ | ✔ | | gte | | | ✔ | ✔ | ✔ | | lte | | | ✔ | ✔ | ✔ | | contains | | | ✔ | | |
In addition to the 4 FilterQuery_*
types, pleco-graphql also provides
SortDirection
: an enum ofASC
andDESC
LimitOffsetPage
: an input object with limit and offsetgraphQLTypes
which is a string containing the definitions of all the types in SDL.
All these exports were written using what they will appear as in SDL. The javascript objects are:
| SDL Name | Javascript Export | |----------------------|---------------------------| | FilterQuery_Boolean | GraphQLFilterQueryBoolean | | FilterQuery_ID | GraphQLFilterQueryID | | FilterQuery_String | GraphQLFilterQueryString | | FilterQuery_Int | GraphQLFilterQueryInt | | FilterQuery_Float | GraphQLFilterQueryFloat | | SortDirection | GraphQLSortDirection | | LimitOffsetPage | GraphQLLimitOffsetPage |
Notes
null
can be passed toeq
andne
and will do awhereNull
andwhereNotNull
, respectivelycontains
is case insensitive
Usage
Take the following GraphQL schema
type Vehicle {
make: String
model: String
}
input VehicleFilterInput {
AND: [VehicleFilterInput]
OR: [VehicleFilterInput]
"fields on the vehicle table"
make: FilterQuery_String
model: FilterQuery_String
"fields not directly on the vehicle table"
numberOfUsers: FilterQuery_Int
highwayMPG: FilterQuery_Int
cityMPG: FilterQuery_Int
userSurveyRating: FilterQuery_Float
}
input VehicleSortInput {
numberOfUsers: SortDirection
userSurveyRating: SortDirection
}
type Query {
vehicles(filter: VehicleFilterInput, sort: VehicleSortInput): [Vehicle]
}
Using this input, a user could construct the following filter query:
query GetVehicles ($filter: VehicleFilterInput, $sort: VehicleSortInput) {
vehicles (filter: $filter, sort: $sort) {
make
model
}
}
with variables
{
"filter": {
"AND": [
{ "make": { "eq": "nissan" } },
{ "model": { "in": ["altima", "sentra"] } },
{ "numberOfUsers": { "AND": [{ "gt": 1000 }, { "lt": 1999 }] } },
{
"OR": [
{ "highwayMPG": { "gt": 30 } },
{ "cityMPG": { "gte": 20 } }
]
},
{ "userSurveyRating": { "gte": 80.5 } }
]
},
"sort": {
"userSurveyRating": "ASC"
}
}
This will specify that the user wants all vehicles whose make is "nissan", with model "altima" or "sentry", who has between 1000-1999 users (exclusive), whose user survey ratings is greater than or equal to 80.5 and whose MPG satisifies either highway strictly greater than 30mpg or city greater than or equal to 20mpg, and sorted by userSurveyRating ascending.
Known Limitations
- Implicit eq and in are not currently supported because GraphQL does not support union input types yet