Skip to content

Commit

Permalink
chore(gatsby): Convert pagination to TypeScript (#23717)
Browse files Browse the repository at this point in the history
* Convert pagination schema type to typescript

* Update getPagination import in schema file

* PR suggested improvements
  • Loading branch information
alisson-suzigan authored May 6, 2020
1 parent c5b8aa5 commit f35545e
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 102 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const {
processFieldExtensions,
internalExtensionNames,
} = require(`./extensions`)
const { getPagination } = require(`./types/pagination`)
import { getPagination } from "./types/pagination"
const { getSortInput, SORTABLE_ENUM } = require(`./types/sort`)
const { getFilterInput, SEARCHABLE_ENUM } = require(`./types/filter`)
const { isGatsbyType, GatsbyGraphQLTypeKind } = require(`./types/type-builders`)
Expand Down
101 changes: 0 additions & 101 deletions packages/gatsby/src/schema/types/pagination.js

This file was deleted.

131 changes: 131 additions & 0 deletions packages/gatsby/src/schema/types/pagination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import {
SchemaComposer,
ObjectTypeComposer,
InputTypeComposer,
InterfaceTypeComposer,
ComposeFieldConfigMap,
} from "graphql-compose"
import { getFieldsEnum } from "./sort"
import { addDerivedType } from "./derived-types"
import { distinct, group } from "../resolvers"

export const getPageInfo = <TContext = any>({
schemaComposer,
}: {
schemaComposer: SchemaComposer<TContext>
}): ObjectTypeComposer =>
schemaComposer.getOrCreateOTC(`PageInfo`, tc => {
tc.addFields({
currentPage: `Int!`,
hasPreviousPage: `Boolean!`,
hasNextPage: `Boolean!`,
itemCount: `Int!`,
pageCount: `Int!`,
perPage: `Int`,
})
})

export const getEdge = <TContext = any>({
schemaComposer,
typeComposer,
}: {
schemaComposer: SchemaComposer<TContext>
typeComposer: ObjectTypeComposer | InterfaceTypeComposer
}): ObjectTypeComposer => {
const typeName = `${typeComposer.getTypeName()}Edge`
addDerivedType({ typeComposer, derivedTypeName: typeName })
return schemaComposer.getOrCreateOTC(typeName, tc => {
tc.addFields({
next: typeComposer,
node: typeComposer.getTypeNonNull(),
previous: typeComposer,
})
})
}

const createPagination = <TSource = any, TContext = any>({
schemaComposer,
typeComposer,
fields,
typeName,
}: {
schemaComposer: SchemaComposer<TContext>
typeComposer: ObjectTypeComposer | InterfaceTypeComposer
fields: ComposeFieldConfigMap<TSource, TContext>
typeName: string
}): ObjectTypeComposer => {
const paginationTypeComposer: ObjectTypeComposer = schemaComposer.getOrCreateOTC(
typeName,
tc => {
tc.addFields({
totalCount: `Int!`,
edges: [getEdge({ schemaComposer, typeComposer }).getTypeNonNull()],
nodes: [typeComposer.getTypeNonNull()],
pageInfo: getPageInfo({ schemaComposer }).getTypeNonNull(),
...fields,
})
}
)
paginationTypeComposer.makeFieldNonNull(`edges`)
paginationTypeComposer.makeFieldNonNull(`nodes`)
addDerivedType({ typeComposer, derivedTypeName: typeName })
return paginationTypeComposer
}

export const getGroup = <TContext = any>({
schemaComposer,
typeComposer,
}: {
schemaComposer: SchemaComposer<TContext>
typeComposer: ObjectTypeComposer | InterfaceTypeComposer
}): ObjectTypeComposer => {
const typeName = `${typeComposer.getTypeName()}GroupConnection`
const fields = {
field: `String!`,
fieldValue: `String`,
}
return createPagination({ schemaComposer, typeComposer, fields, typeName })
}

export const getPagination = <TContext = any>({
schemaComposer,
typeComposer,
}: {
schemaComposer: SchemaComposer<TContext>
typeComposer: ObjectTypeComposer | InterfaceTypeComposer
}): ObjectTypeComposer => {
const inputTypeComposer: InputTypeComposer = typeComposer.getInputTypeComposer()
const typeName = `${typeComposer.getTypeName()}Connection`
const fieldsEnumTC = getFieldsEnum({
schemaComposer,
typeComposer,
inputTypeComposer,
})
const fields = {
distinct: {
type: [`String!`],
args: {
field: fieldsEnumTC.getTypeNonNull(),
},
resolve: distinct,
},
group: {
type: [getGroup({ schemaComposer, typeComposer }).getTypeNonNull()],
args: {
skip: `Int`,
limit: `Int`,
field: fieldsEnumTC.getTypeNonNull(),
},
resolve: group,
},
}
const paginationTypeComposer: ObjectTypeComposer = createPagination({
schemaComposer,
typeComposer,
fields,
typeName,
})
paginationTypeComposer.makeFieldNonNull(`distinct`)
paginationTypeComposer.makeFieldNonNull(`group`)
return paginationTypeComposer
}

0 comments on commit f35545e

Please sign in to comment.