在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):prisma-labs/prisma-binding开源软件地址(OpenSource Url):https://github.com/prisma-labs/prisma-binding开源编程语言(OpenSource Language):TypeScript 86.7%开源软件介绍(OpenSource Introduction):prisma-binding
|
Key | Required | Type | Default | Note |
---|---|---|---|---|
typeDefs |
Yes | string |
- | Type definition string or file path to the schema definition of your Prisma service (typically a file called database.graphql or prisma.graphql ) |
endpoint |
Yes | string |
- | The endpoint of your Prisma service |
secret |
Yes | string |
- | The secret of your Prisma service |
fragmentReplacements |
No | FragmentReplacements |
null |
A list of GraphQL fragment definitions, specifying fields that are required for the resolver to function correctly |
debug |
No | boolean |
false |
Log all queries/mutations to the console |
query
and mutation
query
and mutation
are public properties on your Prisma
instance. They both are of type Query
and expose a number of auto-generated delegate resolver functions that are named after the fields on the Query
and Mutation
types in your Prisma database schema.
Each of these delegate resolvers in essence provides a convenience API for sending queries/mutations to your Prisma service, so you don't have to spell out the full query/mutation from scratch and worry about sending it over HTTP. This is all handled by the delegate resolver function under the hood.
Delegate resolver have the following interface:
(args: any, info: GraphQLResolveInfo | string): Promise<T>
The input arguments are used as follows:
args
: An object carrying potential arguments for the query/mutationinfo
: An object representing the selection set of the query/mutation, either expressed directly as a string or in the form of GraphQLResolveInfo
(you can find more info about the GraphQLResolveInfo
type here)The generic type T
corresponds to the type of the respective field.
exists
exists
also is a public property on your Prisma
instance. Similar to query
and mutation
, it also exposes a number of auto-generated functions. However, it exposes only a single function per type. This function is named according to the root field that allows the retrieval of a single node of that type (e.g. User
for a type called User
). It takes a where
object as an input argument and returns a boolean
value indicating whether the condition expressed with where
is met.
This function enables you to easily check whether a node of a specific type exists in your Prisma database.
request
The request
method lets you send GraphQL queries/mutations to your Prisma service. The functionality is identical to the auto-generated delegate resolves, but the API is more verbose as you need to spell out the full query/mutation. request
uses graphql-request
under the hood.
Here is an example of how it can be used:
const query = `
query ($userId: ID!){
user(id: $userId) {
id
name
}
}
`
const variables = { userId: 'abc' }
prisma.request(query, variables)
.then(result => console.log(result))
// sample result:
// {"data": { "user": { "id": "abc", "name": "Sarah" } } }
forwardTo
If you just want to forward a query to the exact same underlying prisma query, you can use forwardTo
:
const {forwardTo} = require('prisma-binding')
const resolvers = {
Query: {
posts: forwardTo('db')
}
}
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
context: req => ({
...req,
db: new Prisma({
typeDefs: 'src/generated/prisma.graphql',
endpoint: '...',
secret: 'mysecret123',
}),
debug: true,
}),
})
server.start(
() => console.log(`Server is running on http://localhost:4000`),
)
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论