-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide another way of creating the formatError handler which e…
…nables importing IoC dependencies (#56) defines and exports an interface to be implemented by the error formatter
- Loading branch information
Showing
4 changed files
with
138 additions
and
9 deletions.
There are no files selected for viewing
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
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
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,76 @@ | ||
import { Ioc } from '@adonisjs/fold'; | ||
|
||
import { ApolloExceptionFormatter } from '@ioc:Zakodium/Apollo/Server'; | ||
|
||
import createApolloConfig from '../createApolloConfig'; | ||
|
||
const testIoC = new Ioc(); | ||
|
||
class Formatter implements ApolloExceptionFormatter { | ||
formatError() { | ||
return { | ||
message: 'error', | ||
}; | ||
} | ||
} | ||
testIoC.singleton('App/Exceptions/GraphqlHandler', () => new Formatter()); | ||
|
||
describe('apollo config', () => { | ||
it('create apollo configuration, formatError is callback', () => { | ||
const config = createApolloConfig( | ||
{ | ||
schemas: 'app/Schemas', | ||
resolvers: 'app/Resolvers', | ||
path: '/graphql', | ||
apolloServer: { | ||
introspection: true, | ||
formatError: () => ({ message: 'error' }), | ||
}, | ||
}, | ||
{ | ||
ioc: testIoC, | ||
fallbackUrl: 'http://localhost:3333', | ||
}, | ||
); | ||
expect(config).toEqual({ | ||
schemas: 'app/Schemas', | ||
resolvers: 'app/Resolvers', | ||
path: '/graphql', | ||
apolloServer: { | ||
introspection: true, | ||
formatError: expect.any(Function), | ||
}, | ||
}); | ||
}); | ||
|
||
it('create apollo configuration, formatError is an IoC dependency', () => { | ||
const config = createApolloConfig( | ||
{ | ||
schemas: 'app/Schemas', | ||
resolvers: 'app/Resolvers', | ||
path: '/graphql', | ||
apolloServer: { | ||
introspection: true, | ||
formatError: 'App/Exceptions/GraphqlHandler', | ||
}, | ||
}, | ||
{ | ||
ioc: testIoC, | ||
fallbackUrl: 'http://localhost:3333', | ||
}, | ||
); | ||
expect(config).toEqual({ | ||
schemas: 'app/Schemas', | ||
resolvers: 'app/Resolvers', | ||
path: '/graphql', | ||
apolloServer: { | ||
introspection: true, | ||
formatError: expect.any(Function), | ||
}, | ||
}); | ||
// @ts-expect-error We don't care about the arguments here. | ||
expect(config.apolloServer?.formatError()).toEqual({ | ||
message: 'error', | ||
}); | ||
}); | ||
}); |
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,35 @@ | ||
import { IocContract } from '@adonisjs/fold'; | ||
import { ApolloServerOptions, BaseContext } from '@apollo/server'; | ||
|
||
import type { | ||
ApolloConfig, | ||
ApolloUserConfig, | ||
} from '@ioc:Zakodium/Apollo/Server'; | ||
|
||
export default function createApolloConfig<ContextType extends BaseContext>( | ||
config: ApolloUserConfig<ContextType>, | ||
options: { | ||
ioc: IocContract; | ||
fallbackUrl?: string; | ||
}, | ||
): ApolloConfig<ContextType> { | ||
return { | ||
...config, | ||
apolloServer: { | ||
...config.apolloServer, | ||
formatError: | ||
typeof config.apolloServer?.formatError === 'string' | ||
? makeFormatter(options.ioc, config.apolloServer.formatError) | ||
: config.apolloServer.formatError, | ||
}, | ||
}; | ||
} | ||
|
||
function makeFormatter( | ||
ioc: IocContract, | ||
formatterPath: string, | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): ApolloServerOptions<any>['formatError'] { | ||
const formatter = ioc.make(formatterPath); | ||
return formatter.formatError.bind(formatter); | ||
} |