-
-
Notifications
You must be signed in to change notification settings - Fork 349
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 basic GraphQL wrapper function
- Loading branch information
Showing
18 changed files
with
322 additions
and
247 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,61 @@ | ||
// POC that graphQL endpoints can be tested! | ||
/* tslint:disable:no-unused-expression object-literal-sort-keys max-classes-per-file no-empty */ | ||
|
||
import * as chai from "chai"; | ||
import * as chaiAsPromised from "chai-as-promised"; | ||
import * as sinon from "sinon"; | ||
import { like, term } from "../../src/dsl/matchers"; | ||
import { query } from "./consumer"; | ||
import { Pact, GraphQLInteraction } from "../../src/pact"; | ||
|
||
const path = require("path"); | ||
const expect = chai.expect; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
describe("GraphQL example", () => { | ||
const provider = new Pact({ | ||
port: 4000, | ||
log: path.resolve(process.cwd(), "logs", "mockserver-integration.log"), | ||
dir: path.resolve(process.cwd(), "pacts"), | ||
consumer: "GraphQLConsumer", | ||
provider: "GraphQLProvider", | ||
}); | ||
|
||
before(() => provider.setup()); | ||
after(() => provider.finalize()); | ||
|
||
describe("query hello on /graphql", () => { | ||
before(() => { | ||
const graphqlQuery = new GraphQLInteraction() | ||
.uponReceiving("a hello request") | ||
.withQuery(`{ hello }`) | ||
.withRequest({ | ||
path: "/graphql", | ||
method: "POST", | ||
}) | ||
.withVariables({ | ||
foo: "bar", | ||
}) | ||
.willRespondWith({ | ||
status: 200, | ||
headers: { | ||
"Content-Type": "application/json; charset=utf-8", | ||
}, | ||
body: { | ||
data: { | ||
hello: like("Hello world!"), | ||
}, | ||
}, | ||
}); | ||
return provider.addInteraction(graphqlQuery); | ||
}); | ||
|
||
it("returns the correct response", (done) => { | ||
expect(query()).to.eventually.deep.eq({ hello: "Hello world!" }).notify(done); | ||
}); | ||
|
||
// verify with Pact, and reset expectations | ||
afterEach(() => provider.verify()); | ||
}); | ||
}); |
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,30 @@ | ||
import { ApolloClient, HttpLink } from "apollo-boost"; | ||
import { InMemoryCache } from "apollo-cache-inmemory"; | ||
import gql from "graphql-tag"; | ||
import { createHttpLink } from "apollo-link-http"; | ||
|
||
const client = new ApolloClient({ | ||
cache: new InMemoryCache(), | ||
link: createHttpLink({ | ||
fetch: require("node-fetch"), | ||
headers: { | ||
foo: "bar", | ||
}, | ||
uri: "http://localhost:4000/graphql", | ||
}), | ||
}); | ||
|
||
export function query(): any { | ||
return client | ||
.query({ | ||
query: gql` | ||
{ | ||
hello | ||
} | ||
`, | ||
variables: { | ||
foo: "bar", | ||
}, | ||
}) | ||
.then((result: any) => result.data); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,37 @@ | ||
import { Verifier } from "../../src/pact"; | ||
import * as chai from "chai"; | ||
import * as chaiAsPromised from "chai-as-promised"; | ||
|
||
import { VerifierOptions } from "@pact-foundation/pact-node"; | ||
import app from "./provider"; | ||
|
||
const expect = chai.expect; | ||
const path = require("path"); | ||
chai.use(chaiAsPromised); | ||
|
||
const server = app.listen(4000, () => console.log("Now browse to localhost:4000/graphql")); | ||
|
||
// Verify that the provider meets all consumer expectations | ||
describe("Pact Verification", () => { | ||
it("should validate the expectations of Matching Service", () => { // lexical binding required here | ||
const opts = { | ||
provider: "GraphQLProvider", | ||
providerBaseUrl: "http://localhost:4000/graphql", | ||
// Local pacts | ||
// pactUrls: [path.resolve(process.cwd(), "./pacts/graphqlconsumer-graphqlprovider.json")], | ||
pactBrokerUrl: "https://test.pact.dius.com.au/", | ||
pactBrokerUsername: "dXfltyFMgNOFZAxr8io9wJ37iUpY42M", | ||
pactBrokerPassword: "O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1", | ||
publishVerificationResult: true, | ||
providerVersion: "1.0.0", | ||
tags: ["prod"], | ||
}; | ||
|
||
return new Verifier().verifyProvider(opts) | ||
.then((output) => { | ||
console.log("Pact Verification Complete!"); | ||
console.log(output); | ||
server.close(); | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
const express = require("express"); | ||
const graphqlHTTP = require("express-graphql"); | ||
const { buildSchema } = require("graphql"); | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
hello: String | ||
} | ||
`); | ||
|
||
const root = { | ||
hello: () => "Hello world!", | ||
}; | ||
|
||
const app = express(); | ||
export default app; | ||
|
||
app.use("/graphql", graphqlHTTP({ | ||
graphiql: true, | ||
rootValue: root, | ||
schema, | ||
})); | ||
|
||
export function start(): any { | ||
app.listen(4000, () => console.log("Now browse to localhost:4000/graphql")); | ||
} |
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,23 @@ | ||
const pact = require('@pact-foundation/pact-node') | ||
const path = require('path') | ||
const opts = { | ||
pactFilesOrDirs: [path.resolve(__dirname, 'pacts/graphqlconsumer-graphqlprovider.json')], | ||
pactBroker: 'https://test.pact.dius.com.au', | ||
pactBrokerUsername: 'dXfltyFMgNOFZAxr8io9wJ37iUpY42M', | ||
pactBrokerPassword: 'O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1', | ||
tags: ['prod', 'test'], | ||
consumerVersion: '1.0.' + ((process.env.TRAVIS_BUILD_NUMBER) ? process.env.TRAVIS_BUILD_NUMBER : Math.floor(new Date() / 1000)) | ||
} | ||
|
||
pact.publishPacts(opts) | ||
.then(() => { | ||
console.log('Pact contract publishing complete!') | ||
console.log('') | ||
console.log('Head over to https://test.pact.dius.com.au/ and login with') | ||
console.log('=> Username: dXfltyFMgNOFZAxr8io9wJ37iUpY42M') | ||
console.log('=> Password: O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1') | ||
console.log('to see your published contracts.') | ||
}) | ||
.catch(e => { | ||
console.log('Pact contract publishing failed: ', e) | ||
}) |
Oops, something went wrong.