Skip to content

Commit d8c23c7

Browse files
committed
refactor and rename internal functions
1 parent 6fee48a commit d8c23c7

File tree

3 files changed

+190
-215
lines changed

3 files changed

+190
-215
lines changed

README.md

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
# graphql-middleware-apollo-upload-server
1+
# graphql-middleware-typed-arguments
22

3-
[![CircleCI](https://circleci.com/gh/homeroom-live/graphql-middleware-apollo-upload-server.svg?style=shield)](https://circleci.com/gh/homeroom-live/graphql-middleware-apollo-upload-server)
4-
[![npm version](https://badge.fury.io/js/graphql-middleware-apollo-upload-server.svg)](https://badge.fury.io/js/graphql-middleware-apollo-upload-server)
5-
6-
GraphQL Middleware Apollo Upload Server manages uploads for you so you don't have to care about them.
7-
8-
> ❗️ Requires [Apollo Upload Server](https://github.com/jaydenseric/apollo-upload-server).
3+
GraphQL Middleware Typed Arguments lets you define a processor function to massage, validate, authorize, or whatever your query, mutation, or field arguments of a certain type. The middleware was originally developed as GraphQL Middleware Apollo Upload Server, which wonderfully separated streaming uploaded media to its ultimate destination from other concerns.
94

105
## Install
116

127
```bash
13-
yarn add graphql-middleware-apollo-upload-server
8+
npm install graphql-middleware-typed-arguments
149
```
1510

1611
## Overview
1712

18-
`graphql-middleware-apollo-upload-server` handles file upload for you by searching for all `Upload` types first, and handling the files if they are included in arguments. Everything else is in your hands!
13+
`graphql-middleware-typed-arguments` lets you wrap a function around field arguments of any type. The classic example is GraphQLUpload type in conjunction with Apollo Upload Server. Now, also, you can attach an inspection function to any type of query argument.
1914

2015
## Features
2116

@@ -25,31 +20,34 @@ yarn add graphql-middleware-apollo-upload-server
2520

2621
## Demo
2722

23+
The example is taken directly from graphql-middleware-apollo-upload-server which was basis for this generalization. There are two changes from the original: the GraphQLUpload type object is imported, but the type name "Upload" could be used as well. (In fact, type names are simpler way to get at a type defined in SDL.) The second change is providing the type (object or string) to the middleware factory function provided by this package: `processTypeArgs`.
24+
2825
```ts
2926
import { GraphQLServer } from 'graphql-yoga'
27+
import { GraphQLUpload } from 'apollo-upload-server'
3028
import { S3 } from 'aws-sdk'
31-
import { upload } from 'graphql-middleware-apollo-upload-server'
29+
import { processTypeArgs } from 'graphql-middleware-typed-arguments'
3230

3331
const client = new S3({
3432
accessKeyId: __S3_KEY__,
3533
secretAccessKey: __S3_SECRET__,
3634
params: { Bucket: __S3_BUCKET__ },
3735
})
3836

39-
const uploadToS3 = async file => {
37+
const uploadToS3 = file => {
4038
const { stream, filename, mimetype, encoding } = file
4139

42-
const response = await client
40+
const response = client
4341
.upload({
4442
Key: filename,
4543
ACL: 'public-read',
4644
Body: file.stream,
4745
})
4846
.promise()
49-
50-
return {
51-
name: filename,
52-
url: response.Location
47+
.then( response => ({
48+
name: filename,
49+
url: response.Location
50+
}))
5351
}
5452
}
5553

@@ -99,7 +97,7 @@ const resolvers = {
9997
const server = new GraphQLServer({
10098
typeDefs,
10199
resolvers,
102-
middlewares: [upload({ uploadHandler: uploadToS3 })],
100+
middlewares: [processTypeArgs({ type: GraphQLUpload, transform: uploadToS3 })],
103101
context: req => ({
104102
...req,
105103
db: new Prisma({
@@ -116,20 +114,32 @@ server.listen(() => {
116114
## API
117115

118116
```ts
119-
export interface IFile {
117+
interface IConfig<V, T> {
118+
type: GraphQLType | string
119+
transform: (value: V, root: any, args: {}, context: any, info: GraphQLResolveInfo) => Promise<T>
120+
}
121+
122+
export const processTypeArgs<V, T> = (
123+
config: IConfig<V, T>
124+
): IMiddleware
125+
126+
// input and output types are up to you, just provide the transform function
127+
interface IFile {
120128
stream: string
121129
filename: string
122130
mimetype: string
123131
encoding: string
124132
}
125133

126-
interface IConfig<output> {
127-
uploadHandler: (file: IFile) => Promise<output>
134+
interface IRefS3 {
135+
name: string
136+
url: string
128137
}
129138

130-
export const upload = <output>(
131-
config: IConfig<output>,
132-
): IMiddleware
139+
const middlewareFunction = processTypeArgs({
140+
type: 'Upload',
141+
transform: uploadToS3
142+
})
133143
```
134144

135145
## License

0 commit comments

Comments
 (0)