-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Martinez Pupo
committed
Feb 26, 2020
1 parent
c579a69
commit 3c1e400
Showing
13 changed files
with
1,616 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,6 @@ typings/ | |
config/ | ||
resolvers/ | ||
types/ | ||
test/ | ||
|
||
node_modules | ||
|
||
|
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,72 @@ | ||
const { createTestClient } = require('apollo-server-testing') | ||
const gql = require('graphql-tag') | ||
const gateway = require('./../index') | ||
const { reservations } = require('./mocks/data/mocks') | ||
require('./mocks/gql-gateway-requests') | ||
|
||
describe('agreggation layer', () => { | ||
let Query = null | ||
|
||
const endpointsList = [ | ||
{ name: 'reservation_service', url: 'http://localhost/reservation-api/swagger.json' }, | ||
{ name: 'client_service', url: 'http://localhost/client-api/swagger.json' } | ||
] | ||
|
||
const localSchema = ` | ||
extend type Client { | ||
reservations : Reservations | ||
} | ||
` | ||
|
||
const GET_CLIENTS = gql` | ||
query get_clients { | ||
get_clients { | ||
clientId, | ||
firstName, | ||
middleName, | ||
reservations { | ||
data { | ||
reservationId, | ||
location | ||
}, | ||
totalCount | ||
} | ||
} | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Client: { | ||
reservations: { | ||
fragment: '... on Client {clientId}', | ||
async resolve (client, args, context, info) { | ||
const schema = await context.resolveSchema('reservation_service') | ||
|
||
return info.mergeInfo.delegateToSchema({ | ||
schema, | ||
operation: 'query', | ||
fieldName: 'get_reservations_client_clientId', | ||
args: { clientId: client.clientId }, | ||
context, | ||
info | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
beforeAll(async () => { | ||
const server = await gateway({ localSchema, resolvers, endpointsList }) | ||
server.context = server.context({ req: { headers: { authorization: 'Bearer XXXXX' } } }) | ||
const { query } = createTestClient(server) | ||
|
||
Query = query | ||
}) | ||
|
||
it('load clients with aggregations', async () => { | ||
const { data } = await Query({ query: GET_CLIENTS }) | ||
|
||
expect(data.get_clients.length).toBe(2) | ||
expect(data.get_clients[0].reservations.data).toEqual(reservations) | ||
}) | ||
}) |
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,27 @@ | ||
const gateway = require('../index') | ||
const apolloServer = require('apollo-server') | ||
|
||
describe('Server construct', () => { | ||
it('error on empty swagger endpoints list', async () => { | ||
try { | ||
await gateway({}) | ||
} catch (err) { | ||
expect(err.message).toBe('The parameter endpointsList is required') | ||
} | ||
}) | ||
|
||
it('error loading endpoints', async () => { | ||
try { | ||
const endpointsList = [{ name: 'petstore_service', url: 'https://no-existent-endpoint/swagger.json' }] | ||
await gateway({ endpointsList }) | ||
} catch (err) { | ||
expect(err.message).toBe('Error: getaddrinfo ENOTFOUND no-existent-endpoint no-existent-endpoint:443') | ||
} | ||
}) | ||
|
||
it('load of PetStore schema', async () => { | ||
const endpointsList = [{ name: 'petstore_service', url: 'https://petstore.swagger.io/v2/swagger.json' }] | ||
const server = await gateway({ endpointsList }) | ||
expect(server instanceof apolloServer.ApolloServer) | ||
}) | ||
}) |
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,15 @@ | ||
module.exports = { | ||
clients: [{ | ||
clientId: 1, | ||
firstName: 'Ivan', | ||
middleName: 'Martinez' | ||
}, { | ||
clientId: 2, | ||
firstName: 'George', | ||
middleName: 'Schmith' | ||
}], | ||
reservations: [{ | ||
reservationId: 12321, | ||
location: 'My remote location' | ||
}] | ||
} |
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,111 @@ | ||
{ | ||
"swagger": "2.0", | ||
"info": { | ||
"description": "Sample Client Api", | ||
"version": "1.0.0", | ||
"title": "Clients API" | ||
}, | ||
"host" : "localhost", | ||
"basePath": "/client-api", | ||
"consumes": [ | ||
"application/json" | ||
], | ||
"produces": [ | ||
"application/json" | ||
], | ||
"tags": [ | ||
{ | ||
"name": "client", | ||
"description": "Client" | ||
} | ||
], | ||
"schemes": [ | ||
"http" | ||
], | ||
"definitions": { | ||
"Client": { | ||
"type": "object", | ||
"properties": { | ||
"clientId": { | ||
"type": "string", | ||
"readOnly": true | ||
}, | ||
"firstName": { | ||
"type": "string" | ||
}, | ||
"middleName": { | ||
"type": "string" | ||
}, | ||
"lastName": { | ||
"type": "string" | ||
}, | ||
"active": { | ||
"type": "boolean" | ||
}, | ||
"salutation": { | ||
"type": "string" | ||
}, | ||
"address": { | ||
"type": "string" | ||
}, | ||
"email": { | ||
"type": "string" | ||
} | ||
} | ||
} | ||
}, | ||
"paths": { | ||
"/clients": { | ||
"get": { | ||
"tags": [ | ||
"client" | ||
], | ||
"summary": "Get Clients", | ||
"description": "Get all clients", | ||
"responses": { | ||
"200": { | ||
"description": "successful operation", | ||
"schema": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/Client" | ||
} | ||
} | ||
}, | ||
"400": { | ||
"description": "Bad Request" | ||
} | ||
} | ||
} | ||
}, | ||
"/clients/{clientId}": { | ||
"get": { | ||
"tags": [ | ||
"client" | ||
], | ||
"summary": "Find a client by ID", | ||
"description": "", | ||
"parameters": [ | ||
{ | ||
"name": "clientId", | ||
"in": "path", | ||
"description": "ID of the client to be fetched", | ||
"required": true, | ||
"type": "string" | ||
} | ||
], | ||
"responses": { | ||
"200": { | ||
"description": "successful operation", | ||
"schema": { | ||
"$ref": "#/definitions/Client" | ||
} | ||
}, | ||
"404": { | ||
"description": "Not Found" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.