我有以下 MongoDB 架构。
Item {
_id: ObjectId,
translations: [{
language: String
name: String
}]
}
Run Code Online (Sandbox Code Playgroud)
所以我的 Item 实例可能看起来像这样。
{
_id: ObjectId("5ba3bf09d3121aba3ba2f488"),
translations: [
{
language: "en"
name: "a Car"
},
{
language: "de",
name: "der Wagen"
}]
}
Run Code Online (Sandbox Code Playgroud)
我希望能够通过这种方式使用 Graphql 使用特定语言查询我的数据。
{
item(where: {language: "en"}) {
name
}
}
Run Code Online (Sandbox Code Playgroud)
所以它会产生像这样形状的漂亮输出。
{
name: "a Car"
}
Run Code Online (Sandbox Code Playgroud)
请您告诉我一些好的做法或设置 Graphql 解析器映射的好方法吗?
我正在使用阿波罗服务器。
非常感谢!
我正在开发一个使用 graphql ajd 的 Express 后端,我不知道是否需要使用 express-graphql 或 appolo-server-graphql 库。感谢您的帮助!
开始摆弄 GraphQL,但我遇到了这个错误。不确定这是架构定义还是查询中的问题。
const express_graphql = require('express-graphql')
const { buildSchema } = require('graphql')
const users = require('../users/translator')
const schema = buildSchema(`
type User {
id: ID
email: String
role: String
}
type Query {
user(id: ID!): User
users: [User]
token(email: String!, password: String!): String!
}
type Mutation {
signup(email: String!, password: String!, role: String!): ID
}`
)
const resolvers = {
users: users.getAll,
user: users.getById,
token: users.login,
signup: users.create,
}
module.exports = app => {
// GraphQL route
app.use('/graphql', express_graphql({
schema, …Run Code Online (Sandbox Code Playgroud) 我知道我的数据是否会像
{
id: 123
address: xyz
name: hello
}
Run Code Online (Sandbox Code Playgroud)
我想在查询期间使用别名我可以这样做
query {
identification: id
address
full_name : name
}
Run Code Online (Sandbox Code Playgroud)
所以我的数据将如下所示:
{
identification: 123
address: xyz
full_name: hello
}
Run Code Online (Sandbox Code Playgroud)
但我希望即将发布的数据如下所示:
{
id : 123
info: {
address: xyz
name: hello
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在 GraphQl Query 中实现这一点,我怎样才能给出别名info
执行命令时出现以下错误:npm start
Cannot find module 'loadash'
at Function.Module._resolveFilename (module.js:538:15)
at Function.Module._load (module.js:468:25)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (<Path...>/data/dbConnector.js:3:1)
at Module._compile (module.js:643:30)
at loader (<Path...>\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (<Path...>\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (<Path...>/data/resolvers.js:2:1)
at Module._compile (module.js:643:30)
at loader (<Path...>\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (<Path...>\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Module.require (module.js:587:17)
at require (internal/module.js:11:18)
[nodemon] app …Run Code Online (Sandbox Code Playgroud)