Skip to content

Commit

Permalink
[core] GraphQL: Don't include sorting for reference fields
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and saasen committed Feb 27, 2020
1 parent 26264f6 commit 65309f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ function pluralizeTypeName(name) {
return words.join('')
}

function generateTypeQueries(types) {
function generateTypeQueries(types, filters, sortings) {
const queries = []
const queryable = types.filter(
type => type.type === 'Object' && type.interfaces && type.interfaces.includes('Document')
)

const isSortable = type => sortings.some(sorting => sorting.name === `${type.name}Sorting`)

// Single ID-based result lookup queries
queryable.forEach(type => {
queries.push({
Expand Down Expand Up @@ -54,7 +56,7 @@ function generateTypeQueries(types) {
type: `${type.name}Filter`,
isFieldFilter: true
},
{
isSortable(type) && {
name: 'sort',
type: {
kind: 'List',
Expand All @@ -77,7 +79,7 @@ function generateTypeQueries(types) {
description: 'Offset at which to start returning documents from',
isFieldFilter: false
}
]
].filter(Boolean)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,41 @@ function generateTypeSortings(types) {
type => type.type === 'Object' && type.interfaces && type.interfaces.includes('Document')
)

const hasFields = type => type.fields.length > 0

const objectTypeSortings = createObjectTypeSortings(objectTypes)
const documentTypeSortings = createDocumentTypeSortings(documentTypes)
const allSortings = [].concat(objectTypeSortings, documentTypeSortings).filter(hasFields)

return objectTypeSortings.concat(documentTypeSortings).concat(builtInSortingEnum)
return allSortings.concat(builtInSortingEnum)
}

function createObjectTypeSortings(objectTypes) {
return objectTypes.map(objectType => {
return {
name: `${objectType.name}Sorting`,
kind: 'InputObject',
fields: objectType.fields
.filter(field => field.type !== 'JSON' && field.kind !== 'List')
.map(field => ({
fieldName: field.fieldName,
type: builtInTypes.includes(field.type) ? builtInSortingEnum.name : `${field.type}Sorting`
}))
}
})
return objectTypes.map(objectType => ({
name: `${objectType.name}Sorting`,
kind: 'InputObject',
fields: objectType.fields
.filter(field => field.type !== 'JSON' && field.kind !== 'List')
.filter(field => !field.isReference)
.map(field => ({
fieldName: field.fieldName,
type: builtInTypes.includes(field.type) ? builtInSortingEnum.name : `${field.type}Sorting`
}))
}))
}

function createDocumentTypeSortings(documentTypes) {
return documentTypes.map(documentType => {
const fields = documentType.fields
return documentTypes.map(documentType => ({
name: `${documentType.name}Sorting`,
kind: 'InputObject',
fields: documentType.fields
.filter(field => field.type !== 'JSON' && field.kind !== 'List')
.filter(field => !field.isReference)
.map(field => ({
fieldName: field.fieldName,
type: builtInTypes.includes(field.type) ? builtInSortingEnum.name : `${field.type}Sorting`
}))
return {
name: `${documentType.name}Sorting`,
kind: 'InputObject',
fields
}
})
}))
}

module.exports = generateTypeSortings

0 comments on commit 65309f9

Please sign in to comment.