Skip to content

Commit

Permalink
Add example application which uses apollo-server-hapi. (apollograph…
Browse files Browse the repository at this point in the history
…ql#2)

Much in the same way as this repository has an `apollo-server-express`
example, this brings an example which uses hapi.
  • Loading branch information
abernix authored Feb 25, 2019
1 parent d7725ef commit 09e34a1
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 0 deletions.
39 changes: 39 additions & 0 deletions apps/basic-typescript-hapi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz
*.swp

pids
logs
results
tmp

# Build
public/css/main.css

# Coverage reports
coverage

# API keys and secrets
.env

# Dependency directory
node_modules
bower_components

# Editors
.idea
*.iml

# OS metadata
.DS_Store
Thumbs.db

# Ignore built ts files
dist/**/*

1 change: 1 addition & 0 deletions apps/basic-typescript-hapi/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
12 changes: 12 additions & 0 deletions apps/basic-typescript-hapi/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

{
"language": "node_js",
"node_js": "8",
"services": [
"mongodb"
],
"script": [
"npm run build",
"npm run test"
]
}
5 changes: 5 additions & 0 deletions apps/basic-typescript-hapi/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"apollographql.vscode-apollo"
]
}
15 changes: 15 additions & 0 deletions apps/basic-typescript-hapi/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible Node.js debug attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"protocol": "inspector"
}
]
}
15 changes: 15 additions & 0 deletions apps/basic-typescript-hapi/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Place your settings in this file to overwrite default and user settings.
{
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/dist": true,
"**/coverge": true
},
"typescript.referencesCodeLens.enabled": true,
"cSpell.words": [
"csrf",
"definitelytyped",
"promisified"
]
}
15 changes: 15 additions & 0 deletions apps/basic-typescript-hapi/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "build",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
18 changes: 18 additions & 0 deletions apps/basic-typescript-hapi/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
globals: {
'ts-jest': {
tsConfigFile: 'tsconfig.json'
}
},
moduleFileExtensions: [
'ts',
'js'
],
transform: {
'^.+\\.(ts|tsx)$': './node_modules/ts-jest/preprocessor.js'
},
testMatch: [
'**/test/**/*.test.(ts|js)'
],
testEnvironment: 'node'
};
41 changes: 41 additions & 0 deletions apps/basic-typescript-hapi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "basic-typescript-hapi",
"version": "0.1.0",
"private": true,
"description": "Apollo Server TypeScript",
"author": "Meteor Development Group, Inc.",
"license": "MIT",
"scripts": {
"start": "npm run build && npm run watch-debug",
"build": "npm run build-ts",
"serve": "node dist/server.js",
"watch-node": "nodemon dist/server.js",
"watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run watch-node\"",
"test": "jest --forceExit --coverage --verbose",
"watch-test": "npm run test -- --watchAll",
"build-ts": "tsc",
"watch-ts": "tsc -w",
"debug": "npm run build && npm run watch-debug",
"serve-debug": "nodemon --inspect ./dist/server.js -w ../../packages/\\*/\\*\\*/ --ignore 'src/' -w .",
"watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"npm run watch-ts\" \"npm run serve-debug\""
},
"peerDependencies": {
"apollo-server-hapi": "*",
"graphql": "*"
},
"dependencies": {
"hapi": "17.x"
},
"devDependencies": {
"@types/hapi": "17.x",
"@types/graphql": "^14.0.3",
"@types/jest": "^22.1.3",
"@types/node": "^9.4.6",
"concurrently": "^3.5.1",
"jest": "^22.0.4",
"nodemon": "^1.18.8",
"ts-jest": "^22.0.4",
"ts-node": "^5.0.0",
"typescript": "^3.0.0"
}
}
71 changes: 71 additions & 0 deletions apps/basic-typescript-hapi/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ApolloServer, gql } from 'apollo-server-hapi';
import Hapi from 'hapi';

// This is a (sample) collection of books we'll be able to query
// the GraphQL server for. A more complete example might fetch
// from an existing data source like a REST API or database.
const books = [
{
title: "Harry Potter and the Chamber of Secrets",
author: "J.K. Rowling"
},
{
title: "Jurassic Park",
author: "Michael Crichton"
}
];

// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Book" type can be used in other type declarations.
type Book {
title: String
author: String
}
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
}
`;

// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
}
};

const server = new ApolloServer({
typeDefs,
resolvers,
});

const app = new Hapi.Server({
host: 'localhost',
port: 4000,
});

server.applyMiddleware({
app,
});

// Start the server
const start = async function() {
try {
await app.start();
}
catch (err) {
console.log(err);
process.exit(1);
}

console.log(`🚀 Server ready at ${app.info.uri}${server.graphqlPath}`);
};

start();
22 changes: 22 additions & 0 deletions apps/basic-typescript-hapi/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es6",
"noImplicitAny": true,
"lib": ["es2017", "esnext.asynciterable"],
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
}
},
"include": [
"src/**/*"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"apollo-gateway": "file:packages/apollo-platform-commercial/packages/apollo-gateway",
"apollo-graphql": "file:packages/apollo-tooling/packages/apollo-graphql",
"apollo-server": "file:packages/apollo-server/packages/apollo-server",
"apollo-server-hapi": "file:packages/apollo-server/packages/apollo-server-hapi",
"concurrently": "^4.1.0",
"meta": "^1.2.16",
"meta-npm": "^1.0.2",
Expand Down

0 comments on commit 09e34a1

Please sign in to comment.