-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Refactor logic to enable/allow disable/not allow GraphiQL and In…
…trospection Configuration in Dev vs Prod (#9104) There was a regression in the logic enabling/allowing GraphiQL and GraphQL introspection was being checked when creating GraphQL Yoga. This PR reworks the logic, extracts it, and adds tests to prevent the logic regression from happening in the future. --------- Co-authored-by: Josh GM Walker <56300765+Josh-Walker-GM@users.noreply.github.com>
- Loading branch information
1 parent
4b734d3
commit 99bfa9e
Showing
7 changed files
with
387 additions
and
26 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
packages/graphql-server/src/__tests__/__snapshots__/graphiql.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`configureGraphiQLPlayground when in development should configure the GraphiQL Playground when allowGraphiQL is not provided 1`] = ` | ||
{ | ||
"defaultQuery": "query Redwood { | ||
redwood { | ||
version | ||
} | ||
}", | ||
"headerEditorEnabled": true, | ||
"headers": undefined, | ||
"title": "Redwood GraphQL Playground", | ||
} | ||
`; | ||
|
||
exports[`configureGraphiQLPlayground when in development should configure the GraphiQL Playground when allowGraphiQL is null 1`] = ` | ||
{ | ||
"defaultQuery": "query Redwood { | ||
redwood { | ||
version | ||
} | ||
}", | ||
"headerEditorEnabled": true, | ||
"headers": undefined, | ||
"title": "Redwood GraphQL Playground", | ||
} | ||
`; | ||
|
||
exports[`configureGraphiQLPlayground when in development should configure the GraphiQL Playground when allowGraphiQL is true 1`] = ` | ||
{ | ||
"defaultQuery": "query Redwood { | ||
redwood { | ||
version | ||
} | ||
}", | ||
"headerEditorEnabled": true, | ||
"headers": undefined, | ||
"title": "Redwood GraphQL Playground", | ||
} | ||
`; | ||
|
||
exports[`configureGraphiQLPlayground when in development should configure the GraphiQL Playground when allowGraphiQL is undefined 1`] = ` | ||
{ | ||
"defaultQuery": "query Redwood { | ||
redwood { | ||
version | ||
} | ||
}", | ||
"headerEditorEnabled": true, | ||
"headers": undefined, | ||
"title": "Redwood GraphQL Playground", | ||
} | ||
`; | ||
|
||
exports[`configureGraphiQLPlayground when in development should configure the GraphiQL Playground when no config is provided 1`] = ` | ||
{ | ||
"defaultQuery": "query Redwood { | ||
redwood { | ||
version | ||
} | ||
}", | ||
"headerEditorEnabled": true, | ||
"headers": "{"x-auth-comment": "See documentation: https://redwoodjs.com/docs/cli-commands#setup-graphiql-headers on how to auto generate auth headers"}", | ||
"title": "Redwood GraphQL Playground", | ||
} | ||
`; | ||
|
||
exports[`configureGraphiQLPlayground when not in development environment should configure the GraphiQL Playground when allowGraphiQL is true 1`] = ` | ||
{ | ||
"defaultQuery": "query Redwood { | ||
redwood { | ||
version | ||
} | ||
}", | ||
"headerEditorEnabled": true, | ||
"headers": undefined, | ||
"title": "Redwood GraphQL Playground", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { configureGraphiQLPlayground } from '../graphiql' | ||
|
||
describe('configureGraphiQLPlayground', () => { | ||
describe('when not in development environment', () => { | ||
const curNodeEnv = process.env.NODE_ENV | ||
|
||
beforeAll(() => { | ||
process.env.NODE_ENV = 'not-development' | ||
}) | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = curNodeEnv | ||
expect(process.env.NODE_ENV).toBe('test') | ||
}) | ||
|
||
it('should return false when no config is provided', () => { | ||
const result = configureGraphiQLPlayground({}) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when allowGraphiQL is true', () => { | ||
const result = configureGraphiQLPlayground({ | ||
allowGraphiQL: true, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).not.toBe(false) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
it('should return false when allowGraphiQL is false', () => { | ||
const result = configureGraphiQLPlayground({ | ||
allowGraphiQL: false, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
it('should return false when allowGraphiQL is not provided', () => { | ||
const result = configureGraphiQLPlayground({ | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
it('should return false when allowGraphiQL is undefined', () => { | ||
const result = configureGraphiQLPlayground({ | ||
allowGraphiQL: undefined, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
it('should return false when allowGraphiQL is null', () => { | ||
const result = configureGraphiQLPlayground({ | ||
// @ts-expect-error - We don't explicitly allow null, but we will cover it in the tests anyway | ||
allowGraphiQL: null, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('when in development', () => { | ||
const curNodeEnv = process.env.NODE_ENV | ||
|
||
beforeAll(() => { | ||
process.env.NODE_ENV = 'development' | ||
}) | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = curNodeEnv | ||
expect(process.env.NODE_ENV).toBe('test') | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when no config is provided', () => { | ||
const result = configureGraphiQLPlayground({}) | ||
|
||
expect(result).not.toBe(false) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when allowGraphiQL is true', () => { | ||
const result = configureGraphiQLPlayground({ | ||
allowGraphiQL: true, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).not.toBe(false) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when allowGraphiQL is false', () => { | ||
const result = configureGraphiQLPlayground({ | ||
allowGraphiQL: false, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).toBe(false) | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when allowGraphiQL is not provided', () => { | ||
const result = configureGraphiQLPlayground({ | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).not.toBe(false) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when allowGraphiQL is undefined', () => { | ||
const result = configureGraphiQLPlayground({ | ||
allowGraphiQL: undefined, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).not.toBe(false) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
|
||
it('should configure the GraphiQL Playground when allowGraphiQL is null', () => { | ||
const result = configureGraphiQLPlayground({ | ||
// @ts-expect-error - We don't explicitly allow null, but we will cover it in the tests anyway | ||
allowGraphiQL: null, | ||
generateGraphiQLHeader: jest.fn(), | ||
}) | ||
|
||
expect(result).not.toBe(false) | ||
expect(result).toMatchSnapshot() | ||
}) | ||
}) | ||
}) |
107 changes: 107 additions & 0 deletions
107
packages/graphql-server/src/__tests__/introspection.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { configureGraphQLIntrospection } from '../introspection' | ||
|
||
describe('configureGraphQLIntrospection', () => { | ||
describe('when not in development environment', () => { | ||
const curNodeEnv = process.env.NODE_ENV | ||
|
||
beforeAll(() => { | ||
process.env.NODE_ENV = 'not-development' | ||
}) | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = curNodeEnv | ||
expect(process.env.NODE_ENV).toBe('test') | ||
}) | ||
|
||
it('should not disable GraphQL Introspection when allowIntrospection is true', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
allowIntrospection: true, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(false) | ||
}) | ||
|
||
it('should disable GraphQL Introspection when allowIntrospection is false', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
allowIntrospection: false, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(true) | ||
}) | ||
|
||
it('should disable GraphQL Introspection when allowIntrospection is not provided', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({}) | ||
|
||
expect(disableIntrospection).toBe(true) | ||
}) | ||
|
||
it('should disable GraphQL Introspection when allowIntrospection is undefined', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
allowIntrospection: undefined, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(true) | ||
}) | ||
|
||
it('should disable GraphQL Introspection when allowIntrospection is null', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
// @ts-expect-error - We don't explicitly allow null, but we will cover it in the tests anyway | ||
allowIntrospection: null, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('when in development', () => { | ||
const curNodeEnv = process.env.NODE_ENV | ||
|
||
beforeAll(() => { | ||
process.env.NODE_ENV = 'development' | ||
}) | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = curNodeEnv | ||
expect(process.env.NODE_ENV).toBe('test') | ||
}) | ||
|
||
it('should not disable GraphQL Introspection when allowIntrospection is true', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
allowIntrospection: true, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(false) | ||
}) | ||
|
||
it('should disable GraphQL Introspection when allowIntrospection is false', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
allowIntrospection: false, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(true) | ||
}) | ||
|
||
it('should not disable GraphQL Introspection when allowIntrospection is not provided', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({}) | ||
|
||
expect(disableIntrospection).toBe(false) | ||
}) | ||
|
||
it('should not disable GraphQL Introspection when allowIntrospection is undefined', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
allowIntrospection: undefined, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(false) | ||
}) | ||
|
||
it('should not disable GraphQL Introspection when allowIntrospection is null', () => { | ||
const { disableIntrospection } = configureGraphQLIntrospection({ | ||
// @ts-expect-error - We don't explicitly allow null, but we will cover it in the tests anyway | ||
allowIntrospection: null, | ||
}) | ||
|
||
expect(disableIntrospection).toBe(false) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.