Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added examples #48

Merged
merged 7 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/with-apollo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const polka = require('polka')
const bodyParser = require('body-parser')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's import it as const { json } = require('body-parser') then update the usage below too

const { graphqlExpress, graphiqlExpress } = require('apollo-server-express')
const { makeExecutableSchema } = require('graphql-tools')

const { PORT = 3000 } = process.env
const app = polka()

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
})

app.use(bodyParser.json())

app.post('/graphql', graphqlExpress(req => ({
schema
})))

app.get('/graphiql', graphiqlExpress({
endpointURL: '/graphql'
}))

app.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
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
17 changes: 17 additions & 0 deletions examples/with-nextjs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const polka = require('polka')
const next = require('next')

const port = parseInt(process.env.PORT, 10) || 3000
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
const server = polka()

server.get('*', (req, res) => handle(req, res))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can actually be server.get('*', handle) since the signatures match.


server
.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": "0.2.3",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's bump this to latest as well 👍

"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>Home 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>
)
17 changes: 17 additions & 0 deletions examples/with-nextjs/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 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 start
```
or
```sh
$ npm install
$ npm run dev
```

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