Graphql-js: Order fields in the "type Query" alphabetically

Created on 13 Jul 2019  路  4Comments  路  Source: graphql/graphql-js

Is it possible to sort the fields in the type Query alphabetically? At the moment they are not sorted and then they are also unsorted in the Prisma Playground Docs.

This is my current generated type Query from the schema:

type Query {
  area(id: String!): AreaModel!
  areas(filter: AreasFilterInput): [AreaModel!]!
  room(id: String!): RoomModel!
  rooms(orderBy: RoomsOrderByInput = ASC): [RoomModel!]!
  session(id: String!): SessionModel!
  sessions: [SessionModel!]!
  topic(id: String!): TopicModel!
  topics: [TopicModel!]!
  timeframe(id: String!): Timeframe!`
  timeframes(withoutGaps: Boolean = true): [Timeframe!]!
  partner(id: String!): PartnerModel!
  partners: [PartnerModel!]!
  booth(id: String!): BoothModel!
  booths: [BoothModel!]!
  speaker(id: String!): Speaker!
  speakers: [Speaker!]!
}

And the Docs in Prisma Playground looks like this:

image

I would like to have the type Query to be sorted like this:

type Query {
  area(id: String!): AreaModel!
  areas(filter: AreasFilterInput): [AreaModel!]!
  booth(id: String!): BoothModel!
  booths: [BoothModel!]!
  partner(id: String!): PartnerModel!
  partners: [PartnerModel!]!
  room(id: String!): RoomModel!
  rooms(orderBy: RoomsOrderByInput = ASC): [RoomModel!]!
  session(id: String!): SessionModel!
  sessions: [SessionModel!]!
  speaker(id: String!): Speaker!
  speakers: [Speaker!]!
  timeframe(id: String!): Timeframe!
  timeframes(withoutGaps: Boolean = true): [Timeframe!]!
  topic(id: String!): TopicModel!
  topics: [TopicModel!]!
}
question

Most helpful comment

@websolutions-hamburg Please use lexicographicSortSchema on your schema to get sorted schema.
https://github.com/graphql/graphql-js/blob/49d86bbc810d1203aa3f7d93252e51f257d9460f/src/utilities/lexicographicSortSchema.js#L29-L32

All 4 comments

@websolutions-hamburg Please use lexicographicSortSchema on your schema to get sorted schema.
https://github.com/graphql/graphql-js/blob/49d86bbc810d1203aa3f7d93252e51f257d9460f/src/utilities/lexicographicSortSchema.js#L29-L32

@IvanGoncharov I was wondering if I can get some example code on how to use this function?

const { lexicographicSortSchema } = require('graphql')
const fs = require('fs')
const schema = fs.readFileSync('src/schema.graphql')

console.log(lexicographicSortSchema(schema))

Throws an error for me:


/Users/pelhage/my-project/node_modules/graphql/utilities/lexicographicSortSchema.js:39
    types: sortTypes((0, _objectValues.default)(schema.getTypeMap())),
                                                       ^

TypeError: schema.getTypeMap is not a function
    at lexicographicSortSchema (/Users/pelhage/my-project/node_modules/graphql/utilities/lexicographicSortSchema.js:39:56)
    at Object.<anonymous> (/Users/pelhage/my-project/rules/types_are_capitalized.js:5:13)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)

How do I convert this file into a schema that can be parsed properly by lexicographicSortSchema? Thanks!

I spent some time searching through more documentation and think i figured it out

const { lexicographicSortSchema,  buildSchema } = require('graphql')
const fs = require('fs')
const schema = fs.readFileSync('src/schema.graphql', { encoding: 'utf-8'})
const schemaV2 = buildSchema(schema)
console.log(lexicographicSortSchema(schemaV2))

@pelhage Yes, correct.
Also here is a good article on a topic: https://blog.apollographql.com/three-ways-to-represent-your-graphql-schema-a41f4175100d

Was this page helpful?
0 / 5 - 0 ratings