Skip to content

Commit

Permalink
Allow URL patterns in graphqlEndpoint (#2266)
Browse files Browse the repository at this point in the history
* Allow URL patterns in `graphqlEndpoint`

* chore(dependencies): updated changesets for modified dependencies

* chore(dependencies): updated changesets for modified dependencies

* Do not break existing users

* Bump

* chore(dependencies): updated changesets for modified dependencies

* Relax

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ardatan and github-actions[bot] committed Jan 2, 2023
1 parent 2ca0f33 commit 3e5f688
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 60 deletions.
5 changes: 5 additions & 0 deletions .changeset/@graphql-yoga_apollo-link-2266-dependencies.md
@@ -0,0 +1,5 @@
---
'@graphql-yoga/apollo-link': patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/executor-http@0.0.8` 鈫楋笌](https://www.npmjs.com/package/@graphql-tools/executor-http/v/0.0.8) (from `0.0.7`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-yoga_graphiql-2266-dependencies.md
@@ -0,0 +1,5 @@
---
'@graphql-yoga/graphiql': patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/url-loader@7.16.29` 鈫楋笌](https://www.npmjs.com/package/@graphql-tools/url-loader/v/7.16.29) (from `7.16.28`, in `dependencies`)
@@ -0,0 +1,5 @@
---
'@graphql-yoga/plugin-apollo-inline-trace': patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.6.1` 鈫楋笌](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.6.1) (from `^0.5.1`, in `peerDependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-yoga_plugin-apq-2266-dependencies.md
@@ -0,0 +1,5 @@
---
'@graphql-yoga/plugin-apq': patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@^0.6.1` 鈫楋笌](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.6.1) (from `0.5.4`, in `dependencies`)
5 changes: 5 additions & 0 deletions .changeset/@graphql-yoga_urql-exchange-2266-dependencies.md
@@ -0,0 +1,5 @@
---
'@graphql-yoga/urql-exchange': patch
---
dependencies updates:
- Updated dependency [`@graphql-tools/executor-http@0.0.8` 鈫楋笌](https://www.npmjs.com/package/@graphql-tools/executor-http/v/0.0.8) (from `0.0.7`, in `dependencies`)
8 changes: 8 additions & 0 deletions .changeset/calm-jars-draw.md
@@ -0,0 +1,8 @@
---
'graphql-yoga': minor
'@graphql-yoga/plugin-apollo-inline-trace': patch
'@graphql-yoga/plugin-defer-stream': patch
'@graphql-yoga/plugin-apq': patch
---

Accept URL patterns like `/:path` and `*` in `graphqlEndpoint`
6 changes: 6 additions & 0 deletions .changeset/graphql-yoga-2266-dependencies.md
@@ -0,0 +1,6 @@
---
'graphql-yoga': patch
---
dependencies updates:
- Updated dependency [`@whatwg-node/fetch@0.6.1` 鈫楋笌](https://www.npmjs.com/package/@whatwg-node/fetch/v/0.6.1) (from `0.5.4`, in `dependencies`)
- Updated dependency [`@whatwg-node/server@0.5.3` 鈫楋笌](https://www.npmjs.com/package/@whatwg-node/server/v/0.5.3) (from `0.5.1`, in `dependencies`)
2 changes: 1 addition & 1 deletion examples/bun/package.json
Expand Up @@ -12,6 +12,6 @@
"graphql": "16.6.0"
},
"devDependencies": {
"@whatwg-node/fetch": "0.5.4"
"@whatwg-node/fetch": "0.6.1"
}
}
2 changes: 1 addition & 1 deletion examples/error-handling/package.json
Expand Up @@ -8,7 +8,7 @@
},
"dependencies": {
"graphql-yoga": "3.2.1",
"@whatwg-node/fetch": "0.5.4",
"@whatwg-node/fetch": "0.6.1",
"graphql": "^16.1.0"
},
"devDependencies": {
Expand Down
37 changes: 37 additions & 0 deletions packages/graphql-yoga/__tests__/requests.spec.ts
Expand Up @@ -5,6 +5,7 @@ describe('requests', () => {
typeDefs: /* GraphQL */ `
type Query {
ping: String
requestUrl: String
}
type Mutation {
echo(str: String): String
Expand All @@ -13,6 +14,7 @@ describe('requests', () => {
resolvers: {
Query: {
ping: () => 'pong',
requestUrl: (_, __, ctx) => ctx.request.url,
},
Mutation: {
echo(root, args) {
Expand All @@ -35,6 +37,41 @@ describe('requests', () => {
expect(response.status).toBe(404)
})

it('should support path patterns', async () => {
const yoga = createYoga({
schema,
logging: false,
graphqlEndpoint: '/:version/:path',
})
const response = await yoga.fetch('http://yoga/v1/mypath', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ requestUrl }' }),
})

expect(response.status).toBe(200)
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.requestUrl).toBe('http://yoga/v1/mypath')
})

it('allows you to bypass endpoint check with wildcard', async () => {
const yoga = createYoga({
schema,
logging: false,
graphqlEndpoint: '*',
})
const response = await yoga.fetch('http://yoga/random', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ query: '{ ping }' }),
})

expect(response.status).toBe(200)
const body = await response.json()
expect(body.errors).toBeUndefined()
expect(body.data.ping).toBe('pong')
})
it('should send basic query', async () => {
const response = await yoga.fetch('http://yoga/test-graphql', {
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions packages/graphql-yoga/package.json
Expand Up @@ -56,8 +56,8 @@
"@graphql-tools/schema": "^9.0.0",
"@graphql-tools/utils": "^9.0.1",
"@graphql-yoga/subscription": "^3.0.0",
"@whatwg-node/fetch": "0.5.4",
"@whatwg-node/server": "0.5.1",
"@whatwg-node/fetch": "0.6.1",
"@whatwg-node/server": "0.5.3",
"dset": "^3.1.1",
"tslib": "^2.3.1"
},
Expand Down
15 changes: 8 additions & 7 deletions packages/graphql-yoga/src/plugins/useGraphiQL.ts
@@ -1,5 +1,4 @@
import { PromiseOrValue } from '@envelop/core'

import graphiqlHTML from '../graphiql-html.js'
import { YogaLogger } from '../logger.js'
import { Plugin } from './types.js'
Expand Down Expand Up @@ -103,13 +102,15 @@ export function useGraphiQL<TServerContext extends Record<string, any>>(
}

const renderer = config?.render ?? renderGraphiQL

let urlPattern: URLPattern
return {
async onRequest({ request, serverContext, fetchAPI, endResponse, url }) {
if (
shouldRenderGraphiQL(request) &&
config.graphqlEndpoint === url.pathname
) {
async onRequest({ request, serverContext, fetchAPI, endResponse }) {
if (!urlPattern) {
urlPattern = new fetchAPI.URLPattern({
pathname: config.graphqlEndpoint,
})
}
if (shouldRenderGraphiQL(request) && urlPattern.test(request.url)) {
logger.debug(`Rendering GraphiQL`)
const graphiqlOptions = await graphiqlOptionsFactory(
request,
Expand Down
9 changes: 6 additions & 3 deletions packages/graphql-yoga/src/plugins/useHealthCheck.ts
Expand Up @@ -12,10 +12,13 @@ export function useHealthCheck({
logger = console,
endpoint = '/health',
}: HealthCheckPluginOptions = {}): Plugin {
let urlPattern: URLPattern
return {
onRequest({ endResponse, fetchAPI, url }) {
const { pathname: requestPath } = url
if (requestPath === endpoint) {
onRequest({ request, endResponse, fetchAPI }) {
if (!urlPattern) {
urlPattern = new fetchAPI.URLPattern({ pathname: endpoint })
}
if (urlPattern.test(request.url)) {
logger.debug('Responding Health Check')
const response = new fetchAPI.Response(null, {
status: 200,
Expand Down
9 changes: 6 additions & 3 deletions packages/graphql-yoga/src/plugins/useReadinessCheck.ts
Expand Up @@ -34,10 +34,13 @@ export function useReadinessCheck({
endpoint = '/ready',
check,
}: ReadinessCheckPluginOptions): Plugin {
let urlPattern: URLPattern
return {
async onRequest({ request, endResponse, fetchAPI, url }) {
const { pathname: requestPath } = url
if (requestPath === endpoint) {
async onRequest({ request, endResponse, fetchAPI }) {
if (!urlPattern) {
urlPattern = new fetchAPI.URLPattern({ pathname: endpoint })
}
if (urlPattern.test(request.url)) {
let response: Response
try {
const readyOrResponse = await check({ request, fetchAPI })
Expand Down
9 changes: 6 additions & 3 deletions packages/graphql-yoga/src/plugins/useUnhandledRoute.ts
Expand Up @@ -5,10 +5,13 @@ export function useUnhandledRoute(args: {
graphqlEndpoint: string
showLandingPage: boolean
}): Plugin {
let urlPattern: URLPattern
return {
onRequest({ request, fetchAPI, endResponse, url }) {
const { pathname: requestPath } = url
if (requestPath !== args.graphqlEndpoint) {
if (!urlPattern) {
urlPattern = new fetchAPI.URLPattern({ pathname: args.graphqlEndpoint })
}
if (!urlPattern.test(url)) {
if (
args.showLandingPage === true &&
request.method === 'GET' &&
Expand All @@ -18,7 +21,7 @@ export function useUnhandledRoute(args: {
new fetchAPI.Response(
landingPageBody
.replace(/__GRAPHIQL_LINK__/g, args.graphqlEndpoint)
.replace(/__REQUEST_PATH__/g, requestPath),
.replace(/__REQUEST_PATH__/g, url.pathname),
{
status: 200,
statusText: 'OK',
Expand Down
11 changes: 5 additions & 6 deletions packages/graphql-yoga/src/server.ts
Expand Up @@ -145,7 +145,7 @@ export type YogaServerOptions<TServerContext, TUserContext> = {

parserCache?: boolean | ParserCacheOptions
validationCache?: boolean | ValidationCache
fetchAPI?: FetchAPI
fetchAPI?: Partial<FetchAPI>
/**
* GraphQL Multipart Request spec support
*
Expand Down Expand Up @@ -207,11 +207,10 @@ export class YogaServer<

constructor(options?: YogaServerOptions<TServerContext, TUserContext>) {
this.id = options?.id ?? 'yoga'
this.fetchAPI =
options?.fetchAPI ??
createFetch({
useNodeFetch: true,
})
const defaultFetchAPI = createFetch({
useNodeFetch: true,
})
this.fetchAPI = Object.assign(defaultFetchAPI, options?.fetchAPI ?? {})

const logger = options?.logging != null ? options.logging : true
this.logger =
Expand Down
8 changes: 4 additions & 4 deletions packages/plugins/apollo-inline-trace/package.json
Expand Up @@ -42,7 +42,7 @@
},
"peerDependencies": {
"@graphql-tools/utils": "^9.0.1",
"@whatwg-node/fetch": "^0.5.1",
"@whatwg-node/fetch": "^0.6.1",
"graphql-yoga": "^3.2.1",
"graphql": "^15.2.0 || ^16.0.0"
},
Expand All @@ -51,8 +51,8 @@
"apollo-reporting-protobuf": "^3.3.2"
},
"devDependencies": {
"@envelop/on-resolve": "^2.0.2",
"@whatwg-node/fetch": "^0.5.1",
"graphql-yoga": "^3.2.1"
"@envelop/on-resolve": "2.0.4",
"@whatwg-node/fetch": "0.6.1",
"graphql-yoga": "3.2.1"
}
}
4 changes: 2 additions & 2 deletions packages/plugins/apq/package.json
Expand Up @@ -40,11 +40,11 @@
"access": "public"
},
"dependencies": {
"@whatwg-node/fetch": "0.5.4",
"@whatwg-node/fetch": "^0.6.1",
"tiny-lru": "^10.0.0"
},
"devDependencies": {
"graphql-yoga": "^3.2.1"
"graphql-yoga": "3.2.1"
},
"peerDependencies": {
"@graphql-tools/utils": "^9.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/defer-stream/package.json
Expand Up @@ -44,7 +44,7 @@
"graphql-yoga": "^3.2.1"
},
"devDependencies": {
"@whatwg-node/fetch": "0.5.4"
"@whatwg-node/fetch": "0.6.1"
},
"type": "module",
"dependencies": {
Expand Down

0 comments on commit 3e5f688

Please sign in to comment.