-
Notifications
You must be signed in to change notification settings - Fork 559
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: minimal prisma example for js and ts
- Loading branch information
Divyendu Singh
committed
Jul 16, 2018
1 parent
9055382
commit 070b31b
Showing
15 changed files
with
846 additions
and
3 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
endpoint: https://eu1.prisma.sh/public-prisma-yoga-example-js/prisma/dev | ||
datamodel: datamodel.graphql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
### 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 | ||
} | ||
} | ||
``` | ||
|
||
### Codegen | ||
|
||
We are also leveraging `codegen` in this example. The `post-deploy` hook in `prisma.yml` calls the following two commands:- | ||
|
||
1. `graphql get-schema --project db` - gets the `prisma.graphql` i.e. schema SDL for a running Prisma service. | ||
2. `graphql codegen` - generates the binding for Prisma service based on the `codegen` extension in `.graphqlconfig.yaml` file. Note that this requires `prisma-binding` 2.x to be installed as a depencency. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type User { | ||
id: ID! @unique | ||
name: String! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const { GraphQLServer } = require('graphql-yoga') | ||
const { Prisma } = require('./prisma.ts') | ||
|
||
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({ | ||
endpoint: 'https://eu1.prisma.sh/public-prisma-yoga-example-ts/prisma/dev', | ||
debug: true, | ||
}), | ||
}), | ||
}) | ||
|
||
server.start(() => console.log('Server is running on http://localhost:4000')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "prisma", | ||
"version": "1.0.0", | ||
"description": "Minimal Prisma example with GraphQL Yoga", | ||
"main": "index.ts", | ||
"scripts": { | ||
"start": "ts-node index.ts", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "Prisma", | ||
"license": "MIT", | ||
"dependencies": { | ||
"graphql-yoga": "1.14.12", | ||
"prisma-binding": "2.1.0" | ||
}, | ||
"devDependencies": { | ||
"ts-node": "^7.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.