Skip to content

Commit 83f8939

Browse files
committed
fix server
1 parent 8a99625 commit 83f8939

File tree

10 files changed

+3570
-297
lines changed

10 files changed

+3570
-297
lines changed

server/.gitignore

Lines changed: 0 additions & 8 deletions
This file was deleted.

server/.graphqlconfig.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
projects:
22
app:
3-
schemaPath: src/schema.graphql
3+
schemaPath: "src/schema.graphql"
44
extensions:
55
endpoints:
6-
default: http://localhost:4000
6+
default: "http://localhost:4000"
77
database:
8-
schemaPath: src/generated/graphcool.graphql
8+
schemaPath: "src/generated/prisma.graphql"
99
extensions:
10-
graphcool: ./database/graphcool.yml
10+
prisma: database/prisma.yml

server/README.md

Lines changed: 50 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,116 +1,66 @@
1-
# node-basic
1+
# hackernews-graphql-js
22

3-
🚀 Basic starter code for a scalable, production-ready GraphQL server for Node.js.
3+
This repository contains the final project for the [**GraphQL.js tutorial**](https://www.howtographql.com/graphql-js/0-introduction/) on [How to GraphQL](https://www.howtographql.com/). Note that it also serves as foundation for all frontend tutorials on the site.
44

5-
![](https://imgur.com/LG6r1q1.png)
5+
## Usage
66

7-
## Features
8-
9-
- **Scalable GraphQL Server:** `graphql-yoga` based on Apollo Server & Express
10-
- **GraphQL-native database:** Includes GraphQL database binding to Graphcool (running on MySQL)
11-
- Out-of-the-box support for [GraphQL Playground](https://github.com/graphcool/graphql-playground) & [Tracing](https://github.com/apollographql/apollo-tracing)
12-
- Simple data model – easy to adjust
13-
- Preconfigured [`graphql-config`](https://github.com/graphcool/graphql-config) setup
14-
15-
## Requirements
16-
17-
You need to have the following things installed:
18-
19-
* Node 8+
20-
* Graphcool CLI: `npm i -g graphcool@beta`
21-
* GraphQL CLI: `npm i -g graphql-cli`
22-
* GraphQL Playground desktop app (optional): [Download](https://github.com/graphcool/graphql-playground/releases)
23-
24-
## Getting started
7+
### 1. Clone repository
258

269
```sh
27-
# Bootstrap GraphQL server in directory `my-app`, based on `node-basic` boilerplate
28-
graphql create my-app --boilerplate node-basic
29-
30-
# Navigate to the new project
31-
cd my-app
32-
33-
# Deploy the Graphcool database
34-
graphcool deploy
35-
36-
# Start server (runs on http://localhost:4000)
37-
yarn start
38-
39-
# Open Playground to explore GraphQL API
40-
yarn playground
10+
git clone https://github.com/howtographql/graphql-js
11+
cd graphql-js
4112
```
4213

43-
<details>
44-
45-
<summary>Alternative: Clone repo</summary>
14+
### 2. Deploy the Prisma database service
4615

4716
```sh
48-
# Clone the repo and navigate into project directory
49-
git clone https://github.com/graphql-boilerplates/node-graphql-server.git
50-
cd node-graphql-server/basic
51-
52-
# Deploy the Graphcool database
53-
graphcool deploy
54-
55-
# Install node dependencies
56-
yarn install
57-
58-
# Start server (runs on http://localhost:4000)
59-
yarn start
60-
61-
# Open Playground to explore GraphQL API
62-
yarn playground
17+
prisma deploy
6318
```
6419

65-
</details>
66-
67-
## Docs
68-
69-
### Commands
70-
71-
* `yarn start` starts GraphQL server
72-
* `yarn playground` opens the GraphQL Playground
73-
* `yarn deploy` deploys GraphQL server to [`now`](https://now.sh)
74-
75-
### Project structure
76-
77-
#### `/` - configuration files
78-
79-
- [`.graphqlconfig.yml`](./.graphqlconfig.yml) GraphQL configuration file containing the endpoints and schema configuration. Used by the [`graphql-cli`](https://github.com/graphcool/graphql-cli) and the [GraphQL Playground](https://github.com/graphcool/graphql-playground). See [`graphql-config`](https://github.com/graphcool/graphql-config) for more information.
80-
- [`graphcool.yml`](./graphcool.yml) The configuration file for your database service ([documentation](https://www.graph.cool/docs/1.0/reference/graphcool.yml/overview-and-example-foatho8aip)).
81-
82-
#### `/database` - datamodel
83-
84-
- [`database/datamodel.graphql`](./database/datamodel.graphql) contains the data model that you define for your database service (written in [SDL](https://blog.graph.cool/graphql-sdl-schema-definition-language-6755bcb9ce51)).
85-
86-
#### `/src` - application server
87-
88-
- [`src/schema.graphql`](src/schema.graphql) defines your **application schema**. It contains the GraphQL API that you want to expose to your client applications.
89-
- [`src/index.js`](src/index.js) is the entry point of your server, putting everything together and starting the `GraphQLServer` from [`graphql-yoga`](https://github.com/graphcool/graphql-yoga).
90-
91-
#### `/src/generated` - generated files
92-
93-
- [`src/generated/schema.graphql`](src/generated/schema.graphql) defines your **database schema**. It contains the GraphQL API exposed by the Graphcool Database. This file is automatically generated every time `graphcool deploy` is executed, according to the datamodel in `database/datamodel.graphql`.
94-
95-
### Common questions
96-
97-
#### I'm getting a 'Schema could not be fetched.' error after deploying, what gives?
20+
When prompted where (i.e. to which _cluster_) you want to deploy your service, choose any of the public clusters, e.g. `public-us1` or `public-eu1`. (If you have Docker installed, you can also deploy locally.)
21+
22+
### 3. Set the Prisma service endpoint
23+
24+
From the output of the previous command, copy the `HTTP` endpoint and paste it into `src/index.js` where it's used to instantiate the `Prisma` binding. You need to replace the current placeholder `__PRISMA_ENDPOINT`:
25+
26+
```js
27+
const server = new GraphQLServer({
28+
typeDefs: './src/schema.graphql',
29+
resolvers,
30+
context: req => ({
31+
...req,
32+
db: new Prisma({
33+
typeDefs: 'src/generated/prisma.graphql',
34+
endpoint: "__PRISMA_ENDPOINT__",
35+
secret: 'mysecret123',
36+
}),
37+
}),
38+
})
39+
```
9840

99-
Access to the Graphcool API is secured by a secret. This also applies to the introspection query. Using the latest version of GraphQL Playground, the `Authorization` header should automatically be setup with a proper JWT signing the secret. If that's not the case, you can follow these steps to access your API:
41+
For example:
42+
43+
```js
44+
const server = new GraphQLServer({
45+
typeDefs: './src/schema.graphql',
46+
resolvers,
47+
context: req => ({
48+
...req,
49+
db: new Prisma({
50+
typeDefs: 'src/generated/prisma.graphql',
51+
endpoint: "https://eu1.prisma.sh/public-hillcloak-flier-942261/hackernews-graphql-js/dev",
52+
secret: 'mysecret123',
53+
}),
54+
}),
55+
})
56+
```
10057

101-
1. Visit http://jwtbuilder.jamiekurtz.com/
102-
1. Replace the `Key` at the bottom of the page with your `secret` from the [`graphcool.yml`](./graphcool.yml#L5)
103-
1. Click `Create signed JWT` and copy the obtained token
104-
1. Now, to access the schema, use the `Authorization: Bearer <token>` header, or in the GraphQL Playground set it as JSON:
105-
```json
106-
{
107-
"Authorization": "Bearer <token>"
108-
}
109-
```
110-
1. Reload the schema in the Playground (the _refresh_-button is located right next to the URL of the server)
58+
Note that the part `public-hillcloak-flier-952361` of the URL is unique to your service.
11159

112-
> Note: Currently, no content of the signed JWT is verified by the database! This will be implemented [according to this proposal](https://github.com/graphcool/framework/issues/1365) at a later stage.
60+
### 4. Start the server & open Playground
11361

114-
## Contributing
62+
To interact with the API in a GraphQL Playground, all you need to do is execute the `dev` script defined in `package.json`:
11563

116-
Your feedback is **very helpful**, please share your opinion and thoughts! If you have any questions, join the [`#graphql-boilerplate`](https://graphcool.slack.com/messages/graphql-boilerplate) channel on our [Slack](https://graphcool.slack.com/).
64+
```sh
65+
yarn dev
66+
```

server/database/graphcool.yml renamed to server/database/prisma.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# the name for the service (will be part of the service's HTTP endpoint)
2-
service: hackernews-node
2+
service: hackernews-graphql-js
33

44
# the cluster and stage the service is deployed to
55
stage: dev
@@ -10,3 +10,5 @@ secret: mysecret123
1010

1111
# the file path pointing to your data model
1212
datamodel: datamodel.graphql
13+
14+
cluster: public-hillcloak-flier-952261/prisma-eu1

server/package.json

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
{
2-
"name": "hackernews-node",
2+
"name": "hackernews-graphql-js",
33
"scripts": {
4-
"start": "NODE_ENV=dev node src/index.js",
4+
"start": "node src/index.js",
5+
"dev": "npm-run-all --parallel start playground",
56
"playground": "graphql playground"
67
},
78
"dependencies": {
89
"bcryptjs": "^2.4.3",
9-
"graphcool-binding": "1.3.3",
10-
"graphql-yoga": "1.1.4",
11-
"jsonwebtoken": "^8.1.0"
10+
"graphql-yoga": "1.2.0",
11+
"prisma-binding": "1.3.8"
12+
},
13+
"devDependencies": {
14+
"graphql-cli": "2.12.4",
15+
"npm-run-all": "4.1.2",
16+
"prisma-cli": "1.0.0"
1217
}
1318
}

server/src/generated/graphcool.graphql renamed to server/src/generated/prisma.graphql

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# THIS FILE HAS BEEN AUTO-GENERATED BY THE "GRAPHCOOL DEPLOY"
1+
# THIS FILE HAS BEEN AUTO-GENERATED BY "GRAPHCOOL DEPLOY"
22
# DO NOT EDIT THIS FILE DIRECTLY
33

44
#
@@ -34,6 +34,18 @@ type Vote implements Node {
3434
# Other Types
3535
#
3636

37+
type AggregateLink {
38+
count: Int!
39+
}
40+
41+
type AggregateUser {
42+
count: Int!
43+
}
44+
45+
type AggregateVote {
46+
count: Int!
47+
}
48+
3749
type BatchPayload {
3850
count: Long!
3951
}
@@ -43,6 +55,7 @@ scalar DateTime
4355
type LinkConnection {
4456
pageInfo: PageInfo!
4557
edges: [LinkEdge]!
58+
aggregate: AggregateLink!
4659
}
4760

4861
input LinkCreateInput {
@@ -300,6 +313,7 @@ type Subscription {
300313
type UserConnection {
301314
pageInfo: PageInfo!
302315
edges: [UserEdge]!
316+
aggregate: AggregateUser!
303317
}
304318

305319
input UserCreateInput {
@@ -515,6 +529,7 @@ input UserWhereUniqueInput {
515529
type VoteConnection {
516530
pageInfo: PageInfo!
517531
edges: [VoteEdge]!
532+
aggregate: AggregateVote!
518533
}
519534

520535
input VoteCreateInput {

server/src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { GraphQLServer } = require('graphql-yoga')
2-
const { Graphcool } = require('graphcool-binding')
2+
const { Prisma } = require('prisma-binding')
33
const Query = require('./resolvers/Query')
44
const Mutation = require('./resolvers/Mutation')
55
const Subscription = require('./resolvers/Subscription')
@@ -17,9 +17,9 @@ const server = new GraphQLServer({
1717
resolvers,
1818
context: req => ({
1919
...req,
20-
db: new Graphcool({
21-
typeDefs: 'src/generated/graphcool.graphql',
22-
endpoint: 'http://localhost:60000/hackernews-node-02/dev',
20+
db: new Prisma({
21+
typeDefs: 'src/generated/prisma.graphql',
22+
endpoint: "https://eu1.prisma.sh/public-hillcloak-flier-952261/hackernews-graphql-js/dev",
2323
secret: 'mysecret123',
2424
}),
2525
}),

server/src/resolvers/Query.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
async function feed(parent, args, ctx, info) {
2-
const { filter, first, skip, orderBy } = args // destructure input arguments
2+
const { filter, first, skip } = args // destructure input arguments
33
const where = filter
44
? { OR: [{ url_contains: filter }, { description_contains: filter }] }
55
: {}
66

77
const allLinks = await ctx.db.query.links({})
88
const count = allLinks.length
99

10-
const queriedLinkes = await ctx.db.query.links({ first, skip, orderBy, where })
10+
const queriedLinkes = await ctx.db.query.links({ first, skip, where })
1111

1212
return {
1313
linkIds: queriedLinkes.map(link => link.id),

server/src/schema.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# import Link, Vote, LinkSubscriptionPayload, VoteSubscriptionPayload from "./generated/graphcool.graphql"
1+
# import Link, Vote from "./generated/prisma.graphql"
22

33
type Query {
44
feed(filter: String, skip: Int, first: Int, orderBy: LinkOrderByInput): Feed!
@@ -30,4 +30,4 @@ type User {
3030
type Subscription {
3131
newLink: LinkSubscriptionPayload
3232
newVote: VoteSubscriptionPayload
33-
}
33+
}

0 commit comments

Comments
 (0)