Skip to content

Commit

Permalink
feat: sort id fields at the top
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Feb 26, 2024
1 parent 31fa4cf commit 4d013d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/graphql/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getGraphQLResourceSchema(graphqlSchema: ResolvedGraphQLSch
}

if (gqlType.kind === 'OBJECT') {
const fields: Record<string, ResourceSchemaField> = {}
const fields: Array<ResourceSchemaField> = []
let inline = true

for (const field of gqlType.fields) {
Expand Down Expand Up @@ -116,7 +116,7 @@ export async function getGraphQLResourceSchema(graphqlSchema: ResolvedGraphQLSch
deprecationReason: field.deprecationReason ?? undefined,
}

fields[resField.name] = resField
fields.push(resField)
}

const isRootType = rootTypes.includes(gqlType.name)
Expand All @@ -127,7 +127,15 @@ export async function getGraphQLResourceSchema(graphqlSchema: ResolvedGraphQLSch
}

// Sort fields
const sortedFields = Object.values(fields).sort((a, b) => a.name.localeCompare(b.name))
const sortedFields = fields.sort((a, b) => {
if (a.name === 'id' || a.name === '_id') {
return -1
}
if (b.name === 'id' || b.name === '_id') {
return 1
}
return a.name.localeCompare(b.name)
})
const sortedFieldsMap: Record<string, ResourceSchemaField> = {}
for (const field of sortedFields) {
sortedFieldsMap[field.name] = field
Expand Down
14 changes: 11 additions & 3 deletions packages/core/src/rest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function getTypesFromFile(mq: MoquerieInstance, files: string[]) {
}

for (const node of typeNodes) {
const fields: Record<string, ResourceSchemaField> = {}
const fields: Array<ResourceSchemaField> = []

let inline = true

Expand Down Expand Up @@ -119,13 +119,21 @@ export async function getTypesFromFile(mq: MoquerieInstance, files: string[]) {
deprecationReason,
}

fields[fieldName] = resField
fields.push(resField)
})

const tags = ['rest', 'object']

// Sort fields
const sortedFields = Object.values(fields).sort((a, b) => a.name.localeCompare(b.name))
const sortedFields = fields.sort((a, b) => {
if (a.name === 'id' || a.name === '_id') {
return -1
}
if (b.name === 'id' || b.name === '_id') {
return 1
}
return a.name.localeCompare(b.name)
})
const sortedFieldsMap: Record<string, ResourceSchemaField> = {}
for (const field of sortedFields) {
sortedFieldsMap[field.name] = field
Expand Down

0 comments on commit 4d013d5

Please sign in to comment.