-
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
- Loading branch information
Showing
4 changed files
with
97 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,34 @@ | ||
import { Ioc } from '@adonisjs/fold'; | ||
|
||
import createApolloConfig from '../createApolloConfig'; | ||
|
||
const testIoC = new Ioc(); | ||
|
||
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), | ||
}, | ||
}); | ||
}); | ||
}); |
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); | ||
} |