-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): add more tests for middleware and context resolver
Signed-off-by: Raymond Feng <[email protected]>
- Loading branch information
1 parent
82594eb
commit 8db9e08
Showing
4 changed files
with
151 additions
and
19 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
examples/graphql/src/__tests__/acceptance/graphql-context.acceptance.ts
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,62 @@ | ||
// Copyright IBM Corp. 2020. All Rights Reserved. | ||
// Node module: @loopback/example-graphql | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {createBindingFromClass} from '@loopback/core'; | ||
import {GraphQLBindings, GraphQLServer} from '@loopback/graphql'; | ||
import {expect, supertest} from '@loopback/testlab'; | ||
import {RecipesDataSource} from '../../datasources'; | ||
import {RecipeResolver} from '../../graphql-resolvers/recipe-resolver'; | ||
import {RecipeRepository} from '../../repositories'; | ||
import {sampleRecipes} from '../../sample-recipes'; | ||
import {RecipeService} from '../../services/recipe.service'; | ||
import {exampleQuery} from './graphql-tests'; | ||
|
||
describe('GraphQL context', () => { | ||
let server: GraphQLServer; | ||
let repo: RecipeRepository; | ||
|
||
before(givenServer); | ||
after(stopServer); | ||
|
||
it('invokes middleware', async () => { | ||
await supertest(server.httpServer?.url) | ||
.post('/graphql') | ||
.set('content-type', 'application/json') | ||
.accept('application/json') | ||
.send({operationName: 'GetRecipe1', variables: {}, query: exampleQuery}) | ||
.expect(200); | ||
}); | ||
|
||
async function givenServer() { | ||
server = new GraphQLServer({host: '127.0.0.1', port: 0}); | ||
server.resolver(RecipeResolver); | ||
|
||
// Customize the GraphQL context with additional information for test verification | ||
server.bind(GraphQLBindings.GRAPHQL_CONTEXT_RESOLVER).to(ctx => { | ||
return {...ctx, meta: 'loopback'}; | ||
}); | ||
|
||
// Register a GraphQL middleware to verify context resolution | ||
server.middleware((resolverData, next) => { | ||
expect(resolverData.context).to.containEql({meta: 'loopback'}); | ||
return next(); | ||
}); | ||
|
||
server.bind('recipes').to([...sampleRecipes]); | ||
const repoBinding = createBindingFromClass(RecipeRepository); | ||
server.add(repoBinding); | ||
server.add(createBindingFromClass(RecipesDataSource)); | ||
server.add(createBindingFromClass(RecipeService)); | ||
await server.start(); | ||
repo = await server.get<RecipeRepository>(repoBinding.key); | ||
await repo.start(); | ||
} | ||
|
||
async function stopServer() { | ||
if (!server) return; | ||
await server.stop(); | ||
repo.stop(); | ||
} | ||
}); |
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