Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GraphQL Yoga #4712

Merged
merged 15 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
"ora",
"tempy",
"terminal-link",
"@types/node-fetch",
"chalk",
"pascalcase",
"node-fetch",
"@redwoodjs/api",
"@redwoodjs/api-server",
"@redwoodjs/auth",
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/cookbook/using-a-third-party-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,18 @@ We'll enter our query at the top left and the variables (zip) at the lower left.

![image](https://user-images.githubusercontent.com/300/79395014-9cd9d980-7f2d-11ea-83b1-45aaa8506706.png)

Okay lets pull the real data from OpenWeather now. We'll use a package `node-fetch` that mimics the Fetch API in the browser:
Okay lets pull the real data from OpenWeather now. We'll use a package `cross-undici-fetch` that mimics the Fetch API in the browser:

```terminal
yarn workspace api add node-fetch@2
yarn workspace api add cross-undici-fetch
```

And import that into the service and make the fetch. Note that `fetch` returns a Promise so we're going to convert our service to `async`/`await` to simplify things:

```javascript
// api/src/services/weather/weather.js

import fetch from 'node-fetch'
import { fetch } from 'cross-undici-fetch'

export const getWeather = async ({ zip }) => {
const response = await fetch(
Expand Down Expand Up @@ -515,7 +515,7 @@ Okay, let's look for that `cod` and if it's `404` then we know the zip isn't fou
```javascript {4, 12-14}
// api/src/services/weather/weather.js

import fetch from 'node-fetch'
import { fetch } from 'cross-undici-fetch'
import { UserInputError } from '@redwoodjs/graphql-server'

export const getWeather = async ({ zip }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
"dependencies": {
"@babel/runtime-corejs3": "7.16.7",
"@prisma/client": "3.11.0",
"cross-undici-fetch": "0.1.25",
"crypto-js": "4.1.1",
"humanize-string": "2.1.0",
"jsonwebtoken": "8.5.1",
"jwks-rsa": "2.0.5",
"md5": "2.3.0",
"node-fetch": "2.6.7",
"pascalcase": "1.0.0",
"pino": "7.9.1",
"title-case": "3.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/__tests__/normalizeRequest.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIGatewayProxyEvent } from 'aws-lambda'
import { Headers } from 'node-fetch'
import { Headers } from 'cross-undici-fetch'

import { normalizeRequest } from '../transforms'

Expand Down
9 changes: 4 additions & 5 deletions packages/api/src/cors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Request } from 'graphql-helix'
import { Headers } from 'node-fetch'
import { Headers } from 'cross-undici-fetch'

import type { Request } from './transforms'

export type CorsConfig = {
origin?: boolean | string | string[]
Expand Down Expand Up @@ -62,9 +63,7 @@ export function createCorsContext(cors: CorsConfig | undefined) {
return request.method === 'OPTIONS'
},
getRequestHeaders(request: Request): CorsHeaders {
const eventHeaders = new Headers(
request.headers as Record<string, string>
)
const eventHeaders = new Headers(request.headers as HeadersInit)
const requestCorsHeaders = new Headers(corsHeaders)

if (cors && cors.origin) {
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/transforms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIGatewayProxyEvent } from 'aws-lambda'
import { Headers } from 'node-fetch'
import { Headers } from 'cross-undici-fetch'

// This is the same interface used by graphql-helix
// But not importing here to avoid adding a dependency
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/authClients/__tests__/dbAuth.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dbAuth } from '../dbAuth'

global.RWJS_API_DBAUTH_URL = '/.redwood/functions'

jest.mock('node-fetch', () => {
jest.mock('cross-undici-fetch', () => {
return
})

Expand Down
1 change: 0 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
"@babel/cli": "7.16.7",
"@babel/core": "7.16.7",
"@types/listr": "0.14.4",
"@types/node-fetch": "2.5.12",
"jest": "27.5.1",
"typescript": "4.6.2"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
"@babel/runtime-corejs3": "7.16.7",
"@vscode/ripgrep": "1.14.2",
"core-js": "3.21.1",
"cross-undici-fetch": "0.1.25",
"deepmerge": "4.2.2",
"fast-glob": "3.2.11",
"findup-sync": "5.0.0",
"jest": "27.5.1",
"jscodeshift": "0.13.1",
"node-fetch": "2.6.7",
"prettier": "2.5.1",
"tasuku": "1.0.2",
"toml": "3.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
import path from 'path'

import { fetch } from 'cross-undici-fetch'
import fg from 'fast-glob'
import fetch from 'node-fetch'

import getRWPaths from '../../../lib/getRWPaths'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'

import fetch from 'node-fetch'
import { fetch } from 'cross-undici-fetch'

import getRootPackageJson from '../../../lib/getRootPackageJSON'
import getRWPaths from '../../../lib/getRWPaths'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs'
import path from 'path'

import { fetch } from 'cross-undici-fetch'
import fg from 'fast-glob'
import fetch from 'node-fetch'

import getRWPaths from '../../../lib/getRWPaths'

Expand Down
2 changes: 1 addition & 1 deletion packages/codemods/src/lib/fetchFileFromTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'node-fetch'
import { fetch } from 'cross-undici-fetch'

/**
* @param tag should be something like 'v0.42.1'
Expand Down
6 changes: 2 additions & 4 deletions packages/graphql-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"test:watch": "yarn test --watch"
},
"dependencies": {
"@envelop/core": "2.1.0",
"@envelop/depth-limit": "1.3.0",
"@envelop/disable-introspection": "3.1.0",
"@envelop/filter-operation-type": "3.1.0",
Expand All @@ -31,17 +30,16 @@
"@graphql-tools/merge": "8.2.4",
"@graphql-tools/schema": "8.3.3",
"@graphql-tools/utils": "8.6.3",
"@graphql-yoga/common": "0.1.0-beta.4",
"@prisma/client": "3.11.0",
"@redwoodjs/api": "0.49.1",
"core-js": "3.21.1",
"cross-undici-fetch": "0.1.25",
"graphql": "16.3.0",
"graphql-helix": "1.12.0",
"graphql-playground-html": "1.6.30",
"graphql-scalars": "1.15.0",
"graphql-tag": "2.12.6",
"lodash.merge": "4.6.2",
"lodash.omitby": "4.6.0",
"node-fetch": "2.6.7",
"uuid": "8.3.2"
},
"devDependencies": {
Expand Down
98 changes: 0 additions & 98 deletions packages/graphql-server/src/cors.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/graphql-server/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// based on ApolloError https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-errors/src/index.ts
import { EnvelopError } from '@envelop/core'
import { GraphQLYogaError } from '@graphql-yoga/common'

export class RedwoodGraphQLError extends EnvelopError {
export class RedwoodGraphQLError extends GraphQLYogaError {
constructor(message: string, extensions?: Record<string, any>) {
super(message, extensions)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react-hooks/rules-of-hooks */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { envelop, Plugin } from '@envelop/core'
import { envelop, Plugin } from '@graphql-yoga/common'

import { context, getAsyncStoreInstance } from '../../index'
import { useRedwoodGlobalContextSetter } from '../../plugins/useRedwoodGlobalContextSetter'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { APIGatewayProxyEvent } from 'aws-lambda'
import { Headers } from 'node-fetch'
import { Headers } from 'cross-undici-fetch'

import { normalizeRequest } from '@redwoodjs/api'

Expand Down Expand Up @@ -62,32 +62,38 @@ test('Normalizes an aws event with base64', () => {
true
)

expect(normalizeRequest(corsEventB64)).toEqual({
const normalizedRequest = normalizeRequest(corsEventB64)
const expectedRequest = {
headers: new Headers(corsEventB64.headers),
method: 'POST',
query: null,
body: {
bazinga: 'hello_world',
},
}

expect(normalizedRequest.method).toEqual(expectedRequest.method)
expect(normalizedRequest.query).toEqual(expectedRequest.query)
expect(normalizedRequest.body).toEqual(expectedRequest.body)
expectedRequest.headers.forEach((value, key) => {
expect(normalizedRequest.headers.get(key)).toEqual(value)
})
})

test('Handles CORS requests with and without b64 encoded', () => {
const corsEventB64 = createMockedEvent('OPTIONS', undefined, true)

expect(normalizeRequest(corsEventB64)).toEqual({
headers: new Headers(corsEventB64.headers),
method: 'OPTIONS',
query: null,
body: undefined,
})

const corsEventWithoutB64 = createMockedEvent('OPTIONS', undefined, false)

expect(normalizeRequest(corsEventWithoutB64)).toEqual({
const normalizedRequest = normalizeRequest(corsEventB64)
const expectedRequest = {
headers: new Headers(corsEventB64.headers),
method: 'OPTIONS',
query: null,
body: undefined,
}
expect(normalizedRequest.method).toEqual(expectedRequest.method)
expect(normalizedRequest.query).toEqual(expectedRequest.query)
expect(normalizedRequest.body).toEqual(expectedRequest.body)
expectedRequest.headers.forEach((value, key) => {
expect(normalizedRequest.headers.get(key)).toEqual(value)
})
})
Loading