|
1 |
| -# node-basic |
| 1 | +# hackernews-graphql-js |
2 | 2 |
|
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. |
4 | 4 |
|
5 |
| - |
| 5 | +## Usage |
6 | 6 |
|
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 |
25 | 8 |
|
26 | 9 | ```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 |
41 | 12 | ```
|
42 | 13 |
|
43 |
| -<details> |
44 |
| - |
45 |
| -<summary>Alternative: Clone repo</summary> |
| 14 | +### 2. Deploy the Prisma database service |
46 | 15 |
|
47 | 16 | ```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 |
63 | 18 | ```
|
64 | 19 |
|
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 | +``` |
98 | 40 |
|
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 | +``` |
100 | 57 |
|
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. |
111 | 59 |
|
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 |
113 | 61 |
|
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`: |
115 | 63 |
|
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 | +``` |
0 commit comments