|
1 | | -import {createSchema, createYoga} from 'graphql-yoga' |
2 | 1 | import {GraphQLScalarType, Kind} from 'graphql' |
3 | 2 | import { |
4 | 3 | DateTimeISOResolver, |
5 | 4 | GraphQLVoid, |
6 | 5 | JSONObjectResolver, |
7 | 6 | JSONResolver |
8 | 7 | } from 'graphql-scalars' |
| 8 | +import {createSchema, createYoga} from 'graphql-yoga' |
9 | 9 |
|
10 | | -import {useSentry} from '../envelop/use-sentry' |
11 | | -import {Context} from '../../context' |
12 | | -import {resolversToGraphQLResolvers} from '../../define-pylon' |
13 | | -import {PylonConfig} from '../..' |
| 10 | +import {useDisableIntrospection} from '@envelop/disable-introspection' |
14 | 11 | import {readFileSync} from 'fs' |
15 | 12 | import path from 'path' |
| 13 | +import {PylonConfig} from '../..' |
| 14 | +import {Context, getContext} from '../../context' |
| 15 | +import {resolversToGraphQLResolvers} from '../../define-pylon' |
| 16 | +import {useSentry} from '../envelop/use-sentry' |
| 17 | +import {useViewer} from '../envelop/use-viewer' |
16 | 18 |
|
17 | 19 | interface PylonHandlerOptions { |
18 | 20 | graphql: { |
@@ -102,18 +104,52 @@ export const handler = (options: PylonHandlerOptions) => { |
102 | 104 | } |
103 | 105 | }) |
104 | 106 |
|
| 107 | + const resolveGraphiql = (config?: PylonConfig) => { |
| 108 | + const graphiqlOptions = { |
| 109 | + shouldPersistHeaders: true, |
| 110 | + title: 'Pylon Playground', |
| 111 | + defaultQuery: `# Welcome to the Pylon Playground!` |
| 112 | + } |
| 113 | + |
| 114 | + if (typeof config?.graphiql === 'undefined') { |
| 115 | + return graphiqlOptions |
| 116 | + } |
| 117 | + |
| 118 | + if (typeof config.graphiql === 'boolean') { |
| 119 | + return config.graphiql ? graphiqlOptions : false |
| 120 | + } |
| 121 | + |
| 122 | + if (typeof config.graphiql === 'function') { |
| 123 | + const c = getContext() |
| 124 | + return config.graphiql(c) ? graphiqlOptions : false |
| 125 | + } |
| 126 | + |
| 127 | + return false // fallback safeguard |
| 128 | + } |
| 129 | + |
105 | 130 | const yoga = createYoga({ |
106 | 131 | landingPage: false, |
107 | | - graphiql: req => { |
108 | | - return { |
109 | | - shouldPersistHeaders: true, |
110 | | - title: 'Pylon Playground', |
111 | | - defaultQuery: `# Welcome to the Pylon Playground!` |
112 | | - } |
113 | | - }, |
114 | 132 | graphqlEndpoint: '/graphql', |
115 | 133 | ...config, |
116 | | - plugins: [useSentry(), ...(config?.plugins || [])], |
| 134 | + graphiql: async () => resolveGraphiql(config), |
| 135 | + plugins: [ |
| 136 | + useSentry(), |
| 137 | + useDisableIntrospection({ |
| 138 | + disableIf: () => { |
| 139 | + const disable = resolveGraphiql(config) === false |
| 140 | + |
| 141 | + return disable |
| 142 | + } |
| 143 | + }), |
| 144 | + useViewer({ |
| 145 | + disableIf: () => { |
| 146 | + const disable = resolveGraphiql(config) === false |
| 147 | + |
| 148 | + return disable |
| 149 | + } |
| 150 | + }), |
| 151 | + ...(config?.plugins || []) |
| 152 | + ], |
117 | 153 | schema |
118 | 154 | }) |
119 | 155 |
|
|
0 commit comments