|
15 | 15 | * You should have received a copy of the GNU Affero General Public License |
16 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | */ |
18 | | -import { |
19 | | - ApolloServerPluginLandingPageDisabled, |
20 | | - ApolloServerPluginLandingPageGraphQLPlayground, |
21 | | -} from 'apollo-server-core'; |
22 | | -import { |
23 | | - ApolloServer, |
24 | | - ApolloServerExpressConfig, |
25 | | - gql, |
26 | | -} from 'apollo-server-express'; |
| 18 | + |
| 19 | +// import { |
| 20 | +// ApolloServerPluginLandingPageDisabled, |
| 21 | +// ApolloServerPluginLandingPageGraphQLPlayground, |
| 22 | +// } from 'apollo-server-core'; |
| 23 | +// import { |
| 24 | +// ApolloServer, |
| 25 | +// ApolloServerExpressConfig, |
| 26 | +// gql, |
| 27 | +// } from 'apollo-server-express'; |
| 28 | +import { ApolloServer } from '@apollo/server'; |
| 29 | +import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground'; |
| 30 | +import { expressMiddleware } from '@apollo/server/express4'; |
| 31 | +import { DocumentNode } from 'graphql'; |
| 32 | +import gql from 'graphql-tag'; |
| 33 | + |
27 | 34 | import { readFileSync } from 'node:fs'; |
28 | 35 |
|
29 | 36 | import { GqlQueryable } from '../../types.js'; |
30 | 37 | import { resolvers } from './resolvers.js'; |
31 | 38 |
|
32 | 39 | const typeDefsUrl = new URL('./schema/types.graphql', import.meta.url); |
33 | | -const typeDefs = gql(readFileSync(typeDefsUrl, 'utf8')); |
| 40 | +const typeDefs: DocumentNode | undefined = gql( |
| 41 | + readFileSync(typeDefsUrl, 'utf8'), |
| 42 | +); |
34 | 43 |
|
35 | | -const apolloServer = ( |
36 | | - db: GqlQueryable, |
37 | | - opts: ApolloServerExpressConfig = {}, |
38 | | -) => { |
39 | | - return new ApolloServer({ |
| 44 | +interface ApolloServerContext { |
| 45 | + db: GqlQueryable; |
| 46 | +} |
| 47 | + |
| 48 | +export const makeApolloServerMiddleware = async ( |
| 49 | + context: ApolloServerContext, |
| 50 | +): Promise<any> => { |
| 51 | + const apolloServer = new ApolloServer<ApolloServerContext>({ |
40 | 52 | typeDefs, |
41 | 53 | resolvers, |
42 | | - debug: false, |
43 | | - plugins: [ |
44 | | - ApolloServerPluginLandingPageDisabled(), |
45 | | - ApolloServerPluginLandingPageGraphQLPlayground(), |
46 | | - ], |
47 | | - context: () => { |
48 | | - return { db }; |
| 54 | + plugins: [ApolloServerPluginLandingPageGraphQLPlayground()], |
| 55 | + introspection: true, |
| 56 | + persistedQueries: { |
| 57 | + ttl: 300, // 5 minutes |
49 | 58 | }, |
50 | | - ...opts, |
51 | 59 | }); |
52 | | -}; |
53 | 60 |
|
54 | | -export { apolloServer }; |
| 61 | + await apolloServer.start(); |
| 62 | + |
| 63 | + return expressMiddleware<ApolloServerContext>(apolloServer, { |
| 64 | + context: async (): Promise<ApolloServerContext> => { |
| 65 | + return { ...context }; |
| 66 | + }, |
| 67 | + }); |
| 68 | +}; |
0 commit comments