-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] GraphQL: Dynamically generate GraphQL type sortings
- Loading branch information
Showing
3 changed files
with
105 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
packages/@sanity/core/src/actions/graphql/generateTypeSortings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const builtInTypes = ['ID', 'String', 'Url', 'Float', 'Integer', 'Boolean', 'Datetime', 'Date'] | ||
|
||
const builtInSortingEnum = { | ||
name: 'SortOrder', | ||
kind: 'Enum', | ||
values: [ | ||
{ | ||
name: 'ASC', | ||
description: 'Sorts on the value in ascending order.', | ||
value: 1 | ||
}, | ||
{ | ||
name: 'DESC', | ||
description: 'Sorts on the value in descending order.', | ||
value: 2 | ||
} | ||
] | ||
} | ||
|
||
function generateTypeSortings(types) { | ||
const objectTypes = types.filter( | ||
type => | ||
type.type === 'Object' && | ||
!['Block', 'Span'].includes(type.name) && // TODO: What do we do with blocks? | ||
!type.interfaces && | ||
!builtInTypes.includes(type.name) | ||
) | ||
const documentTypes = types.filter( | ||
type => type.type === 'Object' && type.interfaces && type.interfaces.includes('Document') | ||
) | ||
|
||
const objectTypeSortings = createObjectTypeSortings(objectTypes) | ||
const documentTypeSortings = createDocumentTypeSortings(documentTypes) | ||
|
||
return objectTypeSortings.concat(documentTypeSortings).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` | ||
})) | ||
} | ||
}) | ||
} | ||
|
||
function createDocumentTypeSortings(documentTypes) { | ||
return documentTypes.map(documentType => { | ||
const fields = documentType.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 { | ||
name: `${documentType.name}Sorting`, | ||
kind: 'InputObject', | ||
fields | ||
} | ||
}) | ||
} | ||
|
||
module.exports = generateTypeSortings |