Skip to content

Commit

Permalink
support older graphql js version (#1992)
Browse files Browse the repository at this point in the history
* support older graphql js version

* add utils

* inline more fns
  • Loading branch information
saihaj authored Nov 2, 2022
1 parent 7144c70 commit bf69a56
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-pens-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

inline functions to support multiple version of graphql-js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,109 @@ import {
isObjectType,
typeFromAST,
ValidationContext,
ObjectFieldNode,
ValueNode,
} from 'graphql'
import { inspect } from 'graphql/jsutils/inspect.js'
import type { Maybe } from 'graphql/jsutils/Maybe.js'
import type { ObjMap } from 'graphql/jsutils/ObjMap.js'
import { sortValueNode } from 'graphql/utilities/sortValueNode.js'
import { inspect, Maybe } from '@graphql-tools/utils'

interface ObjMap<T> {
[key: string]: T
}

/**
* Returns a number indicating whether a reference string comes before, or after,
* or is the same as the given string in natural sort order.
*
* See: https://en.wikipedia.org/wiki/Natural_sort_order
*
*/
export function naturalCompare(aStr: string, bStr: string): number {
let aIndex = 0
let bIndex = 0

while (aIndex < aStr.length && bIndex < bStr.length) {
let aChar = aStr.charCodeAt(aIndex)
let bChar = bStr.charCodeAt(bIndex)

if (isDigit(aChar) && isDigit(bChar)) {
let aNum = 0
do {
++aIndex
aNum = aNum * 10 + aChar - DIGIT_0
aChar = aStr.charCodeAt(aIndex)
} while (isDigit(aChar) && aNum > 0)

let bNum = 0
do {
++bIndex
bNum = bNum * 10 + bChar - DIGIT_0
bChar = bStr.charCodeAt(bIndex)
} while (isDigit(bChar) && bNum > 0)

if (aNum < bNum) {
return -1
}

if (aNum > bNum) {
return 1
}
} else {
if (aChar < bChar) {
return -1
}
if (aChar > bChar) {
return 1
}
++aIndex
++bIndex
}
}

return aStr.length - bStr.length
}

const DIGIT_0 = 48
const DIGIT_9 = 57

function isDigit(code: number): boolean {
return !isNaN(code) && DIGIT_0 <= code && code <= DIGIT_9
}

function sortValueNode(valueNode: ValueNode): ValueNode {
switch (valueNode.kind) {
case Kind.OBJECT:
return {
...valueNode,
fields: sortFields(valueNode.fields),
}
case Kind.LIST:
return {
...valueNode,
values: valueNode.values.map(sortValueNode),
}
case Kind.INT:
case Kind.FLOAT:
case Kind.STRING:
case Kind.BOOLEAN:
case Kind.NULL:
case Kind.ENUM:
case Kind.VARIABLE:
return valueNode
}
}

function sortFields(
fields: ReadonlyArray<ObjectFieldNode>,
): Array<ObjectFieldNode> {
return fields
.map((fieldNode) => ({
...fieldNode,
value: sortValueNode(fieldNode.value),
}))
.sort((fieldA, fieldB) =>
naturalCompare(fieldA.name.value, fieldB.name.value),
)
}

function reasonMessage(reason: ConflictReasonMessage): string {
if (Array.isArray(reason)) {
Expand Down

0 comments on commit bf69a56

Please sign in to comment.