Use Prisma APIPrisma Bindings
Code Generation
Overview
Prisma binding comes with a code generator CLI.
Install
Install with npm
:
npm install -g prisma-binding
Install with yarn
:
yarn global add prisma-binding
Usage
Usage: prisma-binding -i [input] -g [generator] -b [outputBinding]
Options:
--help Show help [boolean]
--version Show version number [boolean]
--input, -i Path to schema.graphql, schema.js or schema.ts file
[string] [required]
--language, -l Type of the generator. Available generators: typescript,
javascript, flow [string] [required]
--outputBinding, -b Output binding. Example: binding.ts [string] [required]
--outputTypedefs, -t Output type defs. Example: typeDefs.graphql [string]
Usage with GraphQL Config
The prisma-binding
CLI integrates with GraphQL Config. This means instead of passing arguments to the command, you can write a .graphqlconfig.yml
file which will be read by the CLI.
For example, consider the following .graphqlconfig.yml
:
projects:
myapp:
schemaPath: src/generated/prisma.graphql
extensions:
prisma: prisma/prisma.yml
codegen:
- generator: prisma-binding
language: typescript
output:
binding: src/generated/prisma.ts
Invoking simply graphql codegen
in a directory where the above .graphqlconfig
is available is equivalent to invoking the following terminal command:
prisma-binding --language typescript --input src/generated/prisma.graphql --outputBinding src/generated/prisma.ts
Example
Assume your Prisma API is deployed based on the following service configuration:
prisma.yml
datamodel: datamodel.graphql
endpoint: http://localhost:4466
datamodel.graphql
type User {
id: ID! @unique
name: String!
}
The Prisma GraphQL schema for the service is stored in prisma.graphql
.
You can now invoke the prisma-binding
CLI:
prisma-binding --language typescript --input src/generated/prisma.graphql --outputBinding prisma.ts
This will generated the following code:
import { GraphQLResolveInfo, GraphQLSchema } from 'graphql'
import { IResolvers } from 'graphql-tools/dist/Interfaces'
import { Options } from 'graphql-binding'
import { makePrismaBindingClass, BasePrismaOptions } from 'prisma-binding'
export interface Query {
users: <T = User[]>(args: { where?: UserWhereInput, orderBy?: UserOrderByInput, skip?: Int, after?: String, before?: String, first?: Int, last?: Int }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
user: <T = User | null>(args: { where: UserWhereUniqueInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
usersConnection: <T = UserConnection>(args: { where?: UserWhereInput, orderBy?: UserOrderByInput, skip?: Int, after?: String, before?: String, first?: Int, last?: Int }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
node: <T = Node | null>(args: { id: ID_Output }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T>
}
export interface Mutation {
createUser: <T = User>(args: { data: UserCreateInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
updateUser: <T = User | null>(args: { data: UserUpdateInput, where: UserWhereUniqueInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
deleteUser: <T = User | null>(args: { where: UserWhereUniqueInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
upsertUser: <T = User>(args: { where: UserWhereUniqueInput, create: UserCreateInput, update: UserUpdateInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
updateManyUsers: <T = BatchPayload>(args: { data: UserUpdateInput, where?: UserWhereInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T> ,
deleteManyUsers: <T = BatchPayload>(args: { where?: UserWhereInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<T>
}
export interface Subscription {
user: <T = UserSubscriptionPayload | null>(args: { where?: UserSubscriptionWhereInput }, info?: GraphQLResolveInfo | string, options?: Options) => Promise<AsyncIterator<T>>
}
export interface Exists {
User: (where?: UserWhereInput) => Promise<boolean>
}
export interface Prisma {
query: Query
mutation: Mutation
subscription: Subscription
exists: Exists
request: <T = any>(query: string, variables?: {[key: string]: any}) => Promise<T>
delegate(operation: 'query' | 'mutation', fieldName: string, args: {
[key: string]: any;
}, infoOrQuery?: GraphQLResolveInfo | string, options?: Options): Promise<any>;
delegateSubscription(fieldName: string, args?: {
[key: string]: any;
}, infoOrQuery?: GraphQLResolveInfo | string, options?: Options): Promise<AsyncIterator<any>>;
getAbstractResolvers(filterSchema?: GraphQLSchema | string): IResolvers;
}
export interface BindingConstructor<T> {
new(options: BasePrismaOptions): T
}
/**
* Type Defs
*/
const typeDefs = `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
}
`
export const Prisma = makePrismaBindingClass<BindingConstructor<Prisma>>({typeDefs})
/**
* Types
*/
export type UserOrderByInput = 'id_ASC' |
'id_DESC' |
'name_ASC' |
'name_DESC' |
'updatedAt_ASC' |
'updatedAt_DESC' |
'createdAt_ASC' |
'createdAt_DESC'
export type MutationType = 'CREATED' |
'UPDATED' |
'DELETED'
export interface UserCreateInput {
name: String
}
export interface UserWhereUniqueInput {
id?: ID_Input
}
export interface UserUpdateInput {
name?: String
}
export interface UserSubscriptionWhereInput {
AND?: UserSubscriptionWhereInput[] | UserSubscriptionWhereInput
OR?: UserSubscriptionWhereInput[] | UserSubscriptionWhereInput
NOT?: UserSubscriptionWhereInput[] | UserSubscriptionWhereInput
mutation_in?: MutationType[] | MutationType
updatedFields_contains?: String
updatedFields_contains_every?: String[] | String
updatedFields_contains_some?: String[] | String
node?: UserWhereInput
}
export interface UserWhereInput {
AND?: UserWhereInput[] | UserWhereInput
OR?: UserWhereInput[] | UserWhereInput
NOT?: UserWhereInput[] | UserWhereInput
id?: ID_Input
id_not?: ID_Input
id_in?: ID_Input[] | ID_Input
id_not_in?: ID_Input[] | ID_Input
id_lt?: ID_Input
id_lte?: ID_Input
id_gt?: ID_Input
id_gte?: ID_Input
id_contains?: ID_Input
id_not_contains?: ID_Input
id_starts_with?: ID_Input
id_not_starts_with?: ID_Input
id_ends_with?: ID_Input
id_not_ends_with?: ID_Input
name?: String
name_not?: String
name_in?: String[] | String
name_not_in?: String[] | String
name_lt?: String
name_lte?: String
name_gt?: String
name_gte?: String
name_contains?: String
name_not_contains?: String
name_starts_with?: String
name_not_starts_with?: String
name_ends_with?: String
name_not_ends_with?: String
}
/*
* An object with an ID
*/
export interface Node {
id: ID_Output
}
export interface UserPreviousValues {
id: ID_Output
name: String
}
export interface BatchPayload {
count: Long
}
/*
* Information about pagination in a connection.
*/
export interface PageInfo {
hasNextPage: Boolean
hasPreviousPage: Boolean
startCursor?: String
endCursor?: String
}
export interface UserSubscriptionPayload {
mutation: MutationType
node?: User
updatedFields?: String[]
previousValues?: UserPreviousValues
}
export interface User extends Node {
id: ID_Output
name: String
}
/*
* A connection to a list of items.
*/
export interface UserConnection {
pageInfo: PageInfo
edges: UserEdge[]
aggregate: AggregateUser
}
export interface AggregateUser {
count: Int
}
/*
* An edge in a connection.
*/
export interface UserEdge {
node: User
cursor: String
}
/*
The `Boolean` scalar type represents `true` or `false`.
*/
export type Boolean = boolean
/*
The `String` scalar type represents textual data, represented as UTF-8 character sequences. The String type is most often used by GraphQL to represent free-form human-readable text.
*/
export type String = string
/*
The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
*/
export type Int = number
/*
The `Long` scalar type represents non-fractional signed whole numeric values.
Long can represent values between -(2^63) and 2^63 - 1.
*/
export type Long = string
/*
The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.
*/
export type ID_Input = string | number
export type ID_Output = string