Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js GraphQL使用GraphQL工具获取用户_Node.js_Graphql - Fatal编程技术网

Node.js GraphQL使用GraphQL工具获取用户

Node.js GraphQL使用GraphQL工具获取用户,node.js,graphql,Node.js,Graphql,我有一个使用ExpressJS的GraphQL服务器。这是使用GraphQL工具的模式: import UserModel from '../models/User' export const Schema = ` type User { id: Int! firstName: String lastName: String } type Query { users: [User] } schema { query: Query } ` ex

我有一个使用ExpressJS的GraphQL服务器。这是使用GraphQL工具的模式:

import UserModel from '../models/User'

export const Schema =  `

type User {
    id: Int!
    firstName: String
    lastName: String
}

type Query {
    users: [User]
}

schema {
    query: Query
}

`

export const resolveFunctions = {
    Query: {
        users() {
            return UserModel.findAll()
        }
    }
}
没有GraphQL工具,我可以获得用户和某个用户的完整列表,但现在当我使用GraphQL工具时,我不知道如何通过id获得某个用户

除了官方文档之外,我没有在GraphQL工具上找到任何在线资源,所以我希望这能帮助其他正在寻找答案的人


谢谢

您应该添加一个查询:

user(id: String!): User
和处理程序:

user(root, args, ctx) {
  return UserModel.findOne({id: args.id});
}
(您可能需要调整参数类型和调用语义,我只是概述了所需的步骤)


如果你想看到一个有效的例子,你可以看看。我最近添加了一个非常类似的查询。

也许你应该看看。它是建立在
graphql工具
之上的抽象,帮助您更轻松地开发graphql服务。下面是带有
graphqly
的示例代码:

import graphly from "graphqly";

const gBuilder = graphly.createBuilder();

// define types, inputs ... (in any order)
gBuilder.type("Products").implements("List").def(`
    products: [Product]!
`);

gBuilder.type("Product").def(`
    id: ID!
    name: String!
    link: String
    price: Int
`);

gBuilder
.query(`
    products(limit: Int = 20, offset: Int = 0, filter: ProductFilter): Products
`)
.resolve((root, args, context) => {
    // your resolver here
});