如何将 GraphQL 查询从 Node.js 发送到 Prisma
How to send a GraphQL Query from Node.js to Prisma
我刚刚学习了如何基于 the HowToGraphQL tutorial 使用 graphql-yoga
和 prisma-binding
创建 GraphlQL 服务器。
问题: 到目前为止,查询数据库的唯一方法是使用由 运行 命令 graphql playground
启动的 Prisma Playground 网页。
是否可以从 Node.js 脚本执行相同的查询?我遇到了 Apollo 客户端,但它似乎是为了从 React、Vue、Angular.
等前端层使用
这绝对有可能,最后 Prisma API 只是普通的 HTTP,您将查询放入 POST
请求的 body .
因此,您也可以在 Node 脚本中使用 fetch
或 prisma-binding
。
这可能也有帮助,因为它解释了如何使用 fetch
查询 API:https://github.com/nikolasburk/gse/tree/master/3-Use-Prisma-GraphQL-API-from-Code
这是使用 fetch
的样子:
const fetch = require('node-fetch')
const endpoint = '__YOUR_PRISMA_ENDPOINT__'
const query = `
query {
users {
id
name
posts {
id
title
}
}
}
`
fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: query })
})
.then(response => response.json())
.then(result => console.log(JSON.stringify(result)))
如果您想在 fetch
周围使用轻量级包装器,从而避免编写样板文件,请务必查看 graphql-request
。
下面是您如何使用 Prisma 绑定:
const { Prisma } = require('prisma-binding')
const prisma = new Prisma({
typeDefs: 'prisma.graphql',
endpoint: '__YOUR_PRISMA_ENDPOINT__'
})
// send `users` query
prisma.query.users({}, `{ id name }`)
.then(users => console.log(users))
.then(() =>
// send `createUser` mutation
prisma.mutation.createUser(
{
data: { name: `Sarah` },
},
`{ id name }`,
),
)
.then(newUser => {
console.log(newUser)
return newUser
})
.then(newUser =>
// send `user` query
prisma.query.user(
{
where: { id: newUser.id },
},
`{ name }`,
),
)
.then(user => console.log(user))
由于您正在使用 Prisma 并希望从 NodeJS 脚本查询它,我认为您可能忽略了从 Prisma 定义生成客户端的选项。
它负责根据您的数据模型处理 create/read/update/delete/upsert 方法。
此外,您不必担心保持模型和 queries/mutations 同步,因为它是使用 Prisma CLI (prisma generate) 生成的。
我发现与使用原始 GrahQL 查询相比,它可以节省大量编码时间,我节省了更复杂的查询 queries/mutations。
查看他们的official documentation了解更多详情。
此外,请注意,在 prisma-binding 资源库中推荐使用 Prisma 客户端,除非:
Unless you explicitly want to use schema delegation
我不能告诉你太多。
在阅读您的问题之前,我不知道 prisma-binding
包。
编辑:
这是另一个 link 将它们放在一起的观点
我刚刚学习了如何基于 the HowToGraphQL tutorial 使用 graphql-yoga
和 prisma-binding
创建 GraphlQL 服务器。
问题: 到目前为止,查询数据库的唯一方法是使用由 运行 命令 graphql playground
启动的 Prisma Playground 网页。
是否可以从 Node.js 脚本执行相同的查询?我遇到了 Apollo 客户端,但它似乎是为了从 React、Vue、Angular.
等前端层使用这绝对有可能,最后 Prisma API 只是普通的 HTTP,您将查询放入 POST
请求的 body .
因此,您也可以在 Node 脚本中使用 fetch
或 prisma-binding
。
这可能也有帮助,因为它解释了如何使用 fetch
查询 API:https://github.com/nikolasburk/gse/tree/master/3-Use-Prisma-GraphQL-API-from-Code
这是使用 fetch
的样子:
const fetch = require('node-fetch')
const endpoint = '__YOUR_PRISMA_ENDPOINT__'
const query = `
query {
users {
id
name
posts {
id
title
}
}
}
`
fetch(endpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query: query })
})
.then(response => response.json())
.then(result => console.log(JSON.stringify(result)))
如果您想在 fetch
周围使用轻量级包装器,从而避免编写样板文件,请务必查看 graphql-request
。
下面是您如何使用 Prisma 绑定:
const { Prisma } = require('prisma-binding')
const prisma = new Prisma({
typeDefs: 'prisma.graphql',
endpoint: '__YOUR_PRISMA_ENDPOINT__'
})
// send `users` query
prisma.query.users({}, `{ id name }`)
.then(users => console.log(users))
.then(() =>
// send `createUser` mutation
prisma.mutation.createUser(
{
data: { name: `Sarah` },
},
`{ id name }`,
),
)
.then(newUser => {
console.log(newUser)
return newUser
})
.then(newUser =>
// send `user` query
prisma.query.user(
{
where: { id: newUser.id },
},
`{ name }`,
),
)
.then(user => console.log(user))
由于您正在使用 Prisma 并希望从 NodeJS 脚本查询它,我认为您可能忽略了从 Prisma 定义生成客户端的选项。
它负责根据您的数据模型处理 create/read/update/delete/upsert 方法。 此外,您不必担心保持模型和 queries/mutations 同步,因为它是使用 Prisma CLI (prisma generate) 生成的。
我发现与使用原始 GrahQL 查询相比,它可以节省大量编码时间,我节省了更复杂的查询 queries/mutations。
查看他们的official documentation了解更多详情。
此外,请注意,在 prisma-binding 资源库中推荐使用 Prisma 客户端,除非:
Unless you explicitly want to use schema delegation
我不能告诉你太多。
在阅读您的问题之前,我不知道 prisma-binding
包。
编辑:
这是另一个 link 将它们放在一起的观点