从突变返回结果

Returning results from mutations

我的直接问题是为什么不调用查询解析函数?

我怀疑突变解析函数(有效)的 return 值有问题。那么,return 值应该是什么样的?

一个更高层次的问题是:GraphQL 中是否有一种标准方法来注册新用户并处理已存在用户的情况?

下面的做法是将用户的所有数据都放在session数据中,只传回前端需要的数据。

/**
 * graphQL.js
 *
 * Created by jrootham on 18/04/16.
 *
 * Copyright © 2016 Jim Rootham
 */

import graphqlHTTP from "express-graphql";
import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLNonNull,
    GraphQLBoolean
} from 'graphql';
import {hash} from "bcrypt"
import {connect, User} from "../database/defineDB";

const GraphUser = new GraphQLObjectType({
    name: "GraphUser",
    description: "A user object",
    fields: () => {
        return {
            name: {
                type: GraphQLString,
                resolve: (_, __, session) => {
                    console.log("resolve name", session);
                    let name = "";
                    if (session.signedOn) {
                        return User.findById(session.userId).then (user => {
                            return user.name;
                        });
                    }

                    console.log("name", name);
                    return name;
                }
            },
            signedOn: {
                type: GraphQLBoolean,
                resolve: (_, __, session) => {
                    return session.signedOn;
                }
            },
            existed: {
                type: GraphQLBoolean,
                resolve: (_, __, session) => {
                    return session.existed;
                }
            }
        }
    }
});

const query = new GraphQLObjectType({
    name: 'Queries',
    fields: () => {
        return {
            graphUser: {
                type: GraphUser
            }
        }
    }
});

const mutation = new GraphQLObjectType({
    name: 'Mutations',
    description: "Modification actions",
    fields() {
        return {
            registerUser: {
                type: GraphUser,
                args: {
                    name: {
                        type: new GraphQLNonNull(GraphQLString)
                    },
                    password: {
                        type: new GraphQLNonNull(GraphQLString)
                    }
                },
                resolve(_, args, session) {
                    console.log("resolve", args);
                    User.findOne({where:{name:args.name}}).then(user => {
                        console.log("After find", user);
                        if (user === null) {
                            const getHash = new Promise(
                                resolve => {
                                    hash(args.password, 10, (err, hash) => {
                                        resolve(hash);
                                    });
                                }
                            );

                            const result = getHash.then(hash => {
                                connect.models.user.create({
                                    name: args.name,
                                    password: hash
                                }).then(user => {
                                    session.userId = user.id;
                                    session.signedOn = true;
                                    session.existed = false;

                                    console.log("session user", session.userId);
                                    return user;
                                });

                                console.log(result);
                                return result;
                            });
                        }
                        else {
                            session.userId = 0;
                            session.signedOn = false;
                            session.existed = true;
                            console.log("existed");
                            return GraphUser;
                        }
                    });
                }
            }
        }
    }
});

const schema = new GraphQLSchema({
    query: query,
    mutation: mutation
});

export const useGraphQL = app => {
    app.use('/graphql', graphqlHTTP(request =>({
        schema: schema,
        context: request.session,
        formatError: error => ({
            message: error.message,
            locations: error.locations,
            stack: error.stack
        }),
        graphiql:true
    })));
};

为什么不调用查询解析函数?

您的根查询字段 graphUser 没有得到解析。

const query = new GraphQLObjectType({
    name: 'Queries',
    fields: () => {
        return {
            graphUser: {
                type: GraphUser
                // TODO Add a resolve function here
            }
        }
    }
});

我怀疑突变解析函数(有效)的 return 值有问题。那么,return 值应该是什么样的?

如果你使用 Promise,你的突变的 resolve 函数应该 return 一个 Promise(它现在不是 return)并且应该使用 resolve(result)而不是 return result。如果用户已经存在,只需 return 现有用户 object 而不是 type GraphUser.

更高层次的问题是:GraphQL 中是否有标准的方法来注册新用户并处理已存在用户的情况?

GraphQL 本身没有处理用户注册的标准方法。你只需要一个用户注册的突变。在 mutation 的 resolve 函数中,检查用户是否已经存在。如果存在,突变可能 return 错误。否则,用户被注册并且新创建的用户对象被 returned.

创建的 Promise 需要 returned,每个 .then 函数也需要 return 它的参数。结果证明这是处理 Promises 的问题,而不是 GraphQL 问题。