import { ApolloServer } from 'apollo-server-express' import { GraphQLResponse } from 'apollo-server-core' type TypedGraphQLResponse = GraphQLResponse & { data?: TResult | null } export const createQueryFn = (server: ApolloServer) => { type QueryOpts = { query: Parameters[0]['query'] // eslint-disable-next-line @typescript-eslint/ban-types } & (TVars extends undefined ? {} : { variables: TVars }) return async (opts: QueryOpts) => { const result = await server.executeOperation(opts) return result as TypedGraphQLResponse } } export const createMutationFn = (server: ApolloServer) => { type MutationOpts = { mutation: Parameters[0]['query'] variables: TVars } return async (opts: MutationOpts) => { const result = await server.executeOperation({ ...opts, query: opts.mutation }) return result as TypedGraphQLResponse } } export type QueryFn = ReturnType export type MutationFn = ReturnType