Skip to content

Commit

Permalink
Merge pull request #400 from divyenduz/docs_prisma_example
Browse files Browse the repository at this point in the history
docs: minimal prisma example
  • Loading branch information
Divyendu Singh committed Jul 16, 2018
2 parents 364be1e + 3e28f81 commit 0992cd4
Show file tree
Hide file tree
Showing 14 changed files with 1,192 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/prisma-js/README.md
@@ -0,0 +1,35 @@
### Introduction

This example demonstrates a minimal example of using Prisma with GraphQL Yoga.

A quick look at the `prisma.yml` file shows us that we are using a demo server to run this example

The `datamodel.grapqhl` file defines the Prisma service definition.

All the code related to GraphQL-Yoga is self contained in `index.js`.

### Operations

The `typeDefs` in `index.js` suggest that we can perform the following operations.

**Mutation:**

```graphql
mutation CreateUser {
createUser(name: "Prisma") {
id
name
}
}
```

**Query:**

```graphql
query Users {
users {
id
name
}
}
```
4 changes: 4 additions & 0 deletions examples/prisma-js/datamodel.graphql
@@ -0,0 +1,4 @@
type User {
id: ID! @unique
name: String!
}
42 changes: 42 additions & 0 deletions examples/prisma-js/index.js
@@ -0,0 +1,42 @@
const { GraphQLServer } = require('graphql-yoga')
const { Prisma } = require('prisma-binding')

const typeDefs = `
type User {
id: ID!
name: String
}
type Query {
users: [User!]!
}
type Mutation {
createUser(name: String): User
}
`

const resolvers = {
Query: {
users: (root, args, ctx, info) => ctx.prisma.query.users({}, info),
},
Mutation: {
createUser: (root, args, ctx, info) =>
ctx.prisma.mutation.createUser({ data: { name: args.name } }, info),
},
}

const server = new GraphQLServer({
typeDefs,
resolvers,
context: req => ({
...req,
prisma: new Prisma({
typeDefs: './prisma.graphql',
endpoint: 'https://eu1.prisma.sh/public-prisma-yoga-example-js/prisma/dev',
debug: true,
}),
}),
})

server.start(() => console.log('Server is running on http://localhost:4000'))
16 changes: 16 additions & 0 deletions examples/prisma-js/package.json
@@ -0,0 +1,16 @@
{
"name": "prisma",
"version": "1.0.0",
"description": "Minimal Prisma example with GraphQL Yoga",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Prisma",
"license": "MIT",
"dependencies": {
"graphql-yoga": "1.14.12",
"prisma-binding": "2.1.0"
}
}
251 changes: 251 additions & 0 deletions examples/prisma-js/prisma.graphql
@@ -0,0 +1,251 @@
# source: https://eu1.prisma.sh/divyenduz/prisma/dev
# timestamp: Mon Jul 16 2018 11:55:19 GMT+0530 (India Standard Time)

type AggregateUser {
count: Int!
}

type BatchPayload {
"""The number of nodes that have been affected by the Batch operation."""
count: Long!
}

"""
The `Long` scalar type represents non-fractional signed whole numeric values.
Long can represent values between -(2^63) and 2^63 - 1.
"""
scalar Long

type Mutation {
createUser(data: UserCreateInput!): User!
updateUser(data: UserUpdateInput!, where: UserWhereUniqueInput!): User
deleteUser(where: UserWhereUniqueInput!): User
upsertUser(where: UserWhereUniqueInput!, create: UserCreateInput!, update: UserUpdateInput!): User!
updateManyUsers(data: UserUpdateInput!, where: UserWhereInput): BatchPayload!
deleteManyUsers(where: UserWhereInput): BatchPayload!
}

enum MutationType {
CREATED
UPDATED
DELETED
}

"""An object with an ID"""
interface Node {
"""The id of the object."""
id: ID!
}

"""Information about pagination in a connection."""
type PageInfo {
"""When paginating forwards, are there more items?"""
hasNextPage: Boolean!

"""When paginating backwards, are there more items?"""
hasPreviousPage: Boolean!

"""When paginating backwards, the cursor to continue."""
startCursor: String

"""When paginating forwards, the cursor to continue."""
endCursor: String
}

type Query {
users(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): [User]!
user(where: UserWhereUniqueInput!): User
usersConnection(where: UserWhereInput, orderBy: UserOrderByInput, skip: Int, after: String, before: String, first: Int, last: Int): UserConnection!

"""Fetches an object given its ID"""
node(
"""The ID of an object"""
id: ID!
): Node
}

type Subscription {
user(where: UserSubscriptionWhereInput): UserSubscriptionPayload
}

type User implements Node {
id: ID!
name: String!
}

"""A connection to a list of items."""
type UserConnection {
"""Information to aid in pagination."""
pageInfo: PageInfo!

"""A list of edges."""
edges: [UserEdge]!
aggregate: AggregateUser!
}

input UserCreateInput {
name: String!
}

"""An edge in a connection."""
type UserEdge {
"""The item at the end of the edge."""
node: User!

"""A cursor for use in pagination."""
cursor: String!
}

enum UserOrderByInput {
id_ASC
id_DESC
name_ASC
name_DESC
updatedAt_ASC
updatedAt_DESC
createdAt_ASC
createdAt_DESC
}

type UserPreviousValues {
id: ID!
name: String!
}

type UserSubscriptionPayload {
mutation: MutationType!
node: User
updatedFields: [String!]
previousValues: UserPreviousValues
}

input UserSubscriptionWhereInput {
"""Logical AND on all given filters."""
AND: [UserSubscriptionWhereInput!]

"""Logical OR on all given filters."""
OR: [UserSubscriptionWhereInput!]

"""Logical NOT on all given filters combined by AND."""
NOT: [UserSubscriptionWhereInput!]

"""
The subscription event gets dispatched when it's listed in mutation_in
"""
mutation_in: [MutationType!]

"""
The subscription event gets only dispatched when one of the updated fields names is included in this list
"""
updatedFields_contains: String

"""
The subscription event gets only dispatched when all of the field names included in this list have been updated
"""
updatedFields_contains_every: [String!]

"""
The subscription event gets only dispatched when some of the field names included in this list have been updated
"""
updatedFields_contains_some: [String!]
node: UserWhereInput
}

input UserUpdateInput {
name: String
}

input UserWhereInput {
"""Logical AND on all given filters."""
AND: [UserWhereInput!]

"""Logical OR on all given filters."""
OR: [UserWhereInput!]

"""Logical NOT on all given filters combined by AND."""
NOT: [UserWhereInput!]
id: ID

"""All values that are not equal to given value."""
id_not: ID

"""All values that are contained in given list."""
id_in: [ID!]

"""All values that are not contained in given list."""
id_not_in: [ID!]

"""All values less than the given value."""
id_lt: ID

"""All values less than or equal the given value."""
id_lte: ID

"""All values greater than the given value."""
id_gt: ID

"""All values greater than or equal the given value."""
id_gte: ID

"""All values containing the given string."""
id_contains: ID

"""All values not containing the given string."""
id_not_contains: ID

"""All values starting with the given string."""
id_starts_with: ID

"""All values not starting with the given string."""
id_not_starts_with: ID

"""All values ending with the given string."""
id_ends_with: ID

"""All values not ending with the given string."""
id_not_ends_with: ID
name: String

"""All values that are not equal to given value."""
name_not: String

"""All values that are contained in given list."""
name_in: [String!]

"""All values that are not contained in given list."""
name_not_in: [String!]

"""All values less than the given value."""
name_lt: String

"""All values less than or equal the given value."""
name_lte: String

"""All values greater than the given value."""
name_gt: String

"""All values greater than or equal the given value."""
name_gte: String

"""All values containing the given string."""
name_contains: String

"""All values not containing the given string."""
name_not_contains: String

"""All values starting with the given string."""
name_starts_with: String

"""All values not starting with the given string."""
name_not_starts_with: String

"""All values ending with the given string."""
name_ends_with: String

"""All values not ending with the given string."""
name_not_ends_with: String
}

input UserWhereUniqueInput {
id: ID
}
2 changes: 2 additions & 0 deletions examples/prisma-js/prisma.yml
@@ -0,0 +1,2 @@
endpoint: https://eu1.prisma.sh/public-prisma-yoga-example-js/prisma/dev
datamodel: datamodel.graphql
12 changes: 12 additions & 0 deletions examples/prisma-ts/.graphqlconfig.yaml
@@ -0,0 +1,12 @@
projects:
db:
schemaPath: prisma.graphql
extensions:
endpoints:
default: 'https://eu1.prisma.sh/public-prisma-yoga-example-ts/prisma/dev'
codegen:
- generator: prisma-binding
language: typescript
input: prisma.graphql
output:
binding: prisma.ts

0 comments on commit 0992cd4

Please sign in to comment.