Skip to content

Commit

Permalink
chore: improve docs for node 18
Browse files Browse the repository at this point in the history
  • Loading branch information
lynxtaa committed Apr 20, 2024
1 parent 10a8611 commit 8f2c0a8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ client
#### NodeJS 20

```js
const { createReadStream, statSync, openAsBlob } = require('node:fs')
const { openAsBlob } = require('node:fs')
const { AwesomeGraphQLClient } = require('awesome-graphql-client')

const client = new AwesomeGraphQLClient({
Expand Down Expand Up @@ -106,12 +106,14 @@ client
```js
const { createReadStream, statSync } = require('node:fs')
const { Readable } = require('node:stream')
const { AwesomeGraphQLClient, isFileUpload } = require('awesome-graphql-client')
const { AwesomeGraphQLClient } = require('awesome-graphql-client')

class StreamableFile {
class StreamableFile extends Blob {
constructor(filePath) {
const { mtime, size } = statSync(filePath)

super([])

this.name = path.parse(filePath).base
this.lastModified = mtime.getTime()
this.#filePath = filePath
Expand All @@ -133,7 +135,6 @@ class StreamableFile {

const client = new AwesomeGraphQLClient({
endpoint: 'http://localhost:8080/graphql',
isFileUpload: value => isFileUpload(value) || value instanceof StreamableFile,
})

// Also query can be an output from graphql-tag (see examples below)
Expand Down
12 changes: 7 additions & 5 deletions packages/awesome-graphql-client/test/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import { createReadStream, statSync, readFileSync } from 'node:fs'
import path from 'node:path'
import { Readable } from 'node:stream'
import { ReadableStream } from 'node:stream/web'

import { FileUpload, GraphQLUpload } from 'graphql-upload'

import { AwesomeGraphQLClient, isFileUpload } from '../src/index'
import { AwesomeGraphQLClient } from '../src/index'
import { gql } from '../src/util/gql'

import { createServer, TestServer } from './jest/gqlServer'
Expand Down Expand Up @@ -92,17 +91,21 @@ maybeDescribe(nodeMajorVersion < 20)('node < 20', () => {
)

// https://github.com/nodejs/undici/issues/2202#issuecomment-1664134203
class StreamableFile {
class StreamableFile extends Blob {
#filePath: string
name: string
lastModified: number
type: string

constructor(filePath: string) {
const { mtime, size } = statSync(filePath)

super([])

this.name = path.parse(filePath).base
this.lastModified = mtime.getTime()
this.#filePath = filePath
this.type = ''

Object.defineProperty(this, 'size', {
value: size,
Expand All @@ -115,13 +118,12 @@ maybeDescribe(nodeMajorVersion < 20)('node < 20', () => {
}

stream(): ReadableStream<any> {
return Readable.toWeb(createReadStream(this.#filePath))
return Readable.toWeb(createReadStream(this.#filePath)) as ReadableStream
}
}

const client = new AwesomeGraphQLClient({
endpoint: server.endpoint,
isFileUpload: value => isFileUpload(value) || value instanceof StreamableFile,
})

const query = gql`
Expand Down

0 comments on commit 8f2c0a8

Please sign in to comment.