An open API service indexing awesome lists of open source software.

https://github.com/deadcoder0904/jokes-server-error

Jokes App made using Prisma2 which throws weird error ☠️
https://github.com/deadcoder0904/jokes-server-error

Last synced: about 1 year ago
JSON representation

Jokes App made using Prisma2 which throws weird error ☠️

Awesome Lists containing this project

README

          

# GraphQL Server Example

This example shows how to implement a **GraphQL server with JavaScript (Node.js)** based on [Photon.js](https://photonjs.prisma.io/) & [graphql-yoga](https://github.com/prisma/graphql-yoga).

## How to use

### 1. Download example & install dependencies

Clone the `prisma2` branch of this repository:

```
git clone --single-branch --branch prisma2 git@github.com:prisma/prisma-examples.git
```

Install Node dependencies:

```
cd prisma-examples/javascript/graphql
npm install
```

### 2. Install the Prisma 2 CLI

To run the example, you need the [Prisma 2 CLI](https://github.com/prisma/prisma2/blob/master/docs/prisma-2-cli.md):

```
npm install -g prisma2
```

### 3. Set up database

For this example, you'll use a simple [SQLite database](https://www.sqlite.org/index.html). To set up your database, run:

```
prisma2 lift save --name 'init'
prisma2 lift up
```

You can now use the [SQLite Browser](https://sqlitebrowser.org/) to view and edit your data in the `./prisma/dev.db` file that was created when you ran `prisma2 lift up`.

Alternative: Connect to your own database

Prisma supports MySQL and PostgreSQL at the moment. If you would like to connect to your own database, you can do so by specifying a different data source in the [Prisma schema file](prisma/schema.prisma).

For a MySQL provider:
```
datasource mysql {
provider = "mysql"
url = "mysql://johndoe:secret42@localhost:3306/mydatabase"
}
```

*OR*

For a PostgreSQL provider:
```
datasource postgresql {
provider = "postgresql"
url = "postgresql://johndoe:secret42@localhost:5432/mydatabase?schema=public"
}
```

> Note: In the above example connection strings, `johndoe` would be the username to your database, `secret42` the password, `mydatabase` the name of your database, and `public` the [PostgreSQL schema](https://www.postgresql.org/docs/9.1/ddl-schemas.html).

Then to migrate your database, run:

```sh
prisma2 lift save --name 'init'
prisma2 lift up
```

### 4. Generate Photon (type-safe database client)

Run the following command to generate [Photon.js](https://photonjs.prisma.io/):

```
prisma2 generate
```

Now you can seed your database using the `seed` script from `package.json`:

```
npm run seed
```

### 5. Start the GraphQL server

Launch your GraphQL server with this command:

```
npm run start
```

Navigate to [http://localhost:4000](http://localhost:4000) in your browser to explore the API of your GraphQL server in a [GraphQL Playground](https://github.com/prisma/graphql-playground).

### 6. Using the GraphQL API

The schema that specifies the API operations of your GraphQL server is defined in [`./src/schema.graphql`](./src/schema.graphql). Below are a number of operations that you can send to the API using the GraphQL Playground.

Feel free to adjust any operation by adding or removing fields. The GraphQL Playground helps you with its auto-completion and query validation features.

#### Retrieve all published posts and their authors

```graphql
query {
feed {
id
title
content
published
author {
id
name
email
}
}
}
```

See more API operations

#### Create a new user

```graphql
mutation {
signupUser(
data: {
name: "Sarah"
email: "sarah@prisma.io"
}
) {
id
}
}
```

#### Create a new draft

```graphql
mutation {
createDraft(
title: "Join the Prisma Slack"
content: "https://slack.prisma.io"
authorEmail: "alice@prisma.io"
) {
id
published
}
}
```

#### Publish an existing draft

```graphql
mutation {
publish(id: "__POST_ID__") {
id
published
}
}
```

> **Note**: You need to replace the `__POST_ID__`-placeholder with an actual `id` from a `Post` item. You can find one e.g. using the `filterPosts`-query.

#### Search for posts with a specific title or content

```graphql
{
filterPosts(searchString: "graphql") {
id
title
content
published
author {
id
name
email
}
}
}
```

#### Retrieve a single post

```graphql
{
post(id: "__POST_ID__") {
id
title
content
published
author {
id
name
email
}
}
}
```

> **Note**: You need to replace the `__POST_ID__`-placeholder with an actual `id` from a `Post` item. You can find one e.g. using the `filterPosts`-query.

#### Delete a post

```graphql
mutation {
deleteOnePost(where: {id: "__POST_ID__"})
{
id
}
}
```

> **Note**: You need to replace the `__POST_ID__`-placeholder with an actual `id` from a `Post` item. You can find one e.g. using the `filterPosts`-query.

## Next steps

- Read the [Prisma 2 announcement](https://www.prisma.io/blog/announcing-prisma-2-zq1s745db8i5/)
- Check out the [Prisma 2 docs](https://github.com/prisma/prisma2)
- Share your feedback in the [`prisma2-preview`](https://prisma.slack.com/messages/CKQTGR6T0/) channel on the Prisma Slack