Skip to content

Commit bf272ec

Browse files
committed
feat(setup): setup binding and tests
1 parent 158c821 commit bf272ec

File tree

4 files changed

+1515
-7
lines changed

4 files changed

+1515
-7
lines changed

Diff for: __tests__/binding.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expect } from "chai";
2+
import ExampleServiceBinding from "../src";
3+
4+
const exampleBinding = new ExampleServiceBinding();
5+
6+
describe("Example Binding", function() {
7+
it("should return an array of users", async function() {
8+
const users = await exampleBinding.query.users();
9+
expect(users.length).not.to.eql(0);
10+
});
11+
12+
it("should return a user after creating one", async function() {
13+
const user = await exampleBinding.mutation.createUser({ name: "Rowan" });
14+
expect(user.name).to.eql("Rowan");
15+
});
16+
17+
it("should find a user document for id: cjkpro9ugnyqb0b77mawk139e", async () => {
18+
const user = await exampleBinding.query.user({
19+
id: "cjkpro9ugnyqb0b77mawk139e"
20+
});
21+
expect(user.name).to.eql("Rowan");
22+
});
23+
24+
it("should update a user's name", async () => {
25+
let user = await exampleBinding.mutation.createUser({ name: "Rowan" });
26+
expect(user.name).to.eql("Rowan");
27+
user = await exampleBinding.mutation.updateUser({
28+
id: user.id,
29+
name: "Andy"
30+
});
31+
expect(user.name).to.eql("Andy");
32+
});
33+
34+
it("should delete a user", async () => {
35+
let user = await exampleBinding.mutation.createUser({ name: "Rowan" });
36+
expect(user.name).to.eql("Rowan");
37+
user = await exampleBinding.mutation.deleteUser({
38+
id: user.id
39+
});
40+
41+
user = await exampleBinding.query.user({
42+
id: user.id
43+
});
44+
45+
expect(user).to.eql(null);
46+
});
47+
//
48+
// it("should return users from query", async function() {
49+
// const users = await userBinding.query.users();
50+
// expect(users.length).to.eql(1);
51+
// });
52+
});

Diff for: package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
"author": "Abhi Aiyer <[email protected]>",
77
"license": "MIT",
88
"dependencies": {
9-
"graphql-binding": "^2.2.2"
9+
"apollo-link-http": "^1.5.4",
10+
"graphql": "^0.13.2",
11+
"graphql-binding": "^2.2.2",
12+
"graphql-tools": "^3.1.1",
13+
"node-fetch": "^2.2.0"
1014
},
1115
"scripts": {
12-
"test": "mocha ./__tests__ --compilers js:babel-register"
16+
"test": "mocha ./__tests__ --require babel-register --watch"
1317
},
1418
"devDependencies": {
1519
"babel-cli": "^6.26.0",

Diff for: src/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import fetch from "node-fetch";
2+
import { Binding } from "graphql-binding";
3+
import { HttpLink } from "apollo-link-http";
4+
import { makeRemoteExecutableSchema } from "graphql-tools";
5+
6+
const typeDefs = `
7+
type Query {
8+
user(id: ID!): User
9+
users: [User!]!
10+
}
11+
12+
type Mutation {
13+
createUser(name: String!): User!
14+
updateUser(id: ID!, name: String!): User
15+
deleteUser(id: ID!): User
16+
}
17+
18+
type Subscription {
19+
userCreated: User!
20+
}
21+
22+
type User {
23+
id: ID!
24+
name: String!
25+
}
26+
`;
27+
28+
export default class ExampleServiceBinding extends Binding {
29+
constructor() {
30+
// Create the `HttpLink` required for the remote executable schema
31+
const endpoint = `https://graphql-binding-example-service-kcbreqbsbh.now.sh`;
32+
const link = new HttpLink({ uri: endpoint, fetch });
33+
34+
// Create the remote schema
35+
const schema = makeRemoteExecutableSchema({ link, schema: typeDefs });
36+
37+
// Invoke the constructor of `Binding` with the remote schema
38+
super({
39+
schema: schema
40+
});
41+
}
42+
}

0 commit comments

Comments
 (0)