Skip to content

Commit

Permalink
examples: apollo & next.js (#48)
Browse files Browse the repository at this point in the history
* with nextjs example

* with apollo example

* updated readme

* Update index.js

Unused routes were removed

* removed unused routes

* ~> fixes
  • Loading branch information
jerolan authored and lukeed committed May 23, 2018
1 parent d96afc5 commit a7a38c4
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 0 deletions.
46 changes: 46 additions & 0 deletions examples/with-apollo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const polka = require('polka');
const { json } = require('body-parser');
const { makeExecutableSchema } = require('graphql-tools');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');

const { PORT=3000 } = process.env;

const tasks = [
{ id: 1, name: 'Go to Market', complete: false },
{ id: 2, name: 'Walk the dog', complete: true },
{ id: 3, name: 'Take a nap', complete: false }
];

const typeDefs = `
type Task {
id: Int!
name: String!
complete: Boolean!
}
type Query {
tasks: [Task]
task(id: Int!): Task
}
`;

const resolvers = {
Query: {
tasks: () => tasks,
task: (_, args) => tasks.find(o => o.id === args.id)
}
};

const schema = module.exports = makeExecutableSchema({ typeDefs, resolvers });

polka()
.use(json())
.post('/graphql', graphqlExpress(req => ({
schema
})))
.get('/graphiql', graphiqlExpress({
endpointURL: '/graphql'
}))
.listen(PORT).then(_ => {
console.log(`> Ready on localhost:${PORT}`)
});
12 changes: 12 additions & 0 deletions examples/with-apollo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"scripts": {
"start": "node index"
},
"dependencies": {
"apollo-server-express": "^1.3.6",
"body-parser": "^1.18.3",
"graphql": "^0.13.2",
"graphql-tools": "^3.0.2",
"polka": "^0.3.4"
}
}
37 changes: 37 additions & 0 deletions examples/with-apollo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Example: Apollo Graphql

Tiny example with [Apollo Graphql](https://www.apollographql.com/)

It uses the [`Apollo Server`](https://github.com/apollographql/apollo-server) a community-maintained open-source GraphQL server.

## Setup
```sh
$ npm install
$ npm start
```

## Usage
You can use it with any apollo client or with the [Graphiql](https://github.com/graphql/graphiql) in [localhost](http://localhost:3000/graphiql)

## Available queries
```
{
tasks {
id
name
complete
}
}
```

```
{
task (id: Int!) {
id
name
complete
}
}
```

![Screenshot](screenshot.png)
Binary file added examples/with-apollo/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions examples/with-nextjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.next
node_modules
package-lock.json
16 changes: 16 additions & 0 deletions examples/with-nextjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const next = require('next');
const polka = require('polka');

const { PORT=3000, NODE_ENV } = process.env;

const dev = NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
polka()
.get('*', handle)
.listen(PORT).then(() => {
console.log(`> Ready on http://localhost:${PORT}`);
});
});
13 changes: 13 additions & 0 deletions examples/with-nextjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scripts": {
"dev": "node index.js",
"build": "next build",
"start": "NODE_ENV=production node index.js"
},
"dependencies": {
"next": "latest",
"polka": "latest",
"react": "16.2.0",
"react-dom": "16.2.0"
}
}
11 changes: 11 additions & 0 deletions examples/with-nextjs/pages/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import Link from 'next/link';

export default () => (
<div>
<h1>About Page</h1>
<Link href='/'>
Link to Home
</Link>
</div>
)
11 changes: 11 additions & 0 deletions examples/with-nextjs/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';
import Link from 'next/link';

export default () => (
<div>
<h1>Home Page</h1>
<Link href='/about'>
Link to About Page
</Link>
</div>
)
18 changes: 18 additions & 0 deletions examples/with-nextjs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Example: Next.js

It uses [`Next.js`](https://github.com/zeit/next.js) a Framework for server-rendered or statically-exported React apps.

## Setup
```sh
$ npm install
$ npm run build
$ npm start
```
or
```sh
$ npm install
$ npm run dev
```

## Usage
Go to `localhost:3000` after starting the server and get the welcome message

0 comments on commit a7a38c4

Please sign in to comment.