Node.js 返回null的GraphQL

Node.js 返回null的GraphQL,node.js,graphql,graphiql,Node.js,Graphql,Graphiql,我正在尝试使用GraphQL创建一个简单的模块化Node.js应用程序,以稍微了解它。我实现了两种类型,并尝试在GraphiQL中使用它们,但没有取得多大成功。下面是一个最小的非工作示例。MDC.js: //MDC.js module.exports.typeDef = ` type MDC { getAll: String getWithInitials(initials: String): [String] } `; module.expor

我正在尝试使用GraphQL创建一个简单的模块化Node.js应用程序,以稍微了解它。我实现了两种类型,并尝试在GraphiQL中使用它们,但没有取得多大成功。下面是一个最小的非工作示例。MDC.js:

//MDC.js
module.exports.typeDef = `
    type MDC {
        getAll: String
        getWithInitials(initials: String): [String]
    }
`;

module.exports.resolvers = {
    MDC: {
        getAll: function() {
            return "get all called";
        },
        getWithInitials: function(initials) {
            return "get with initials called with initials = " + initials;
        }
    }
};
Schemas.js:

//schemas.js
const MDC = require('./mdc').typeDef; 
const MDCResolvers = require('./mdc').resolvers; 

const Query = `
  type Query {
    mdc: MDC
    hello: String
  }
`;


const resolvers = {
    Query: { 
        mdc: function() {
            return "mdc called (not sure if this is correct)"
        }
    },
    MDC: {
        getAll: MDCResolvers.MDC.getAll,
        getInitials: MDCResolvers.MDC.getWithInitials,
    },
    hello: function() {
        return "ciao"
    }
};

module.exports.typeDefs = [ MDC, Query ];
module.exports.resolvers = resolvers;
Server.js:

//server.js
const express        = require('express');
const app            = express();
var graphqlHTTP      = require('express-graphql');
var { buildSchema }  = require('graphql');

const port = 8000;
const schemas = require('./app/schemas/schemas');

require('./app/routes')(app, {});

var fullSchemas = "";
for(var i = 0; i < schemas.typeDefs.length; i ++){
  fullSchemas +=  "," + schemas.typeDefs[i];
}

console.log(schemas.resolvers) //I can see the functions are defined

var schema = buildSchema(fullSchemas);

var root = schemas.resolvers;


app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));

app.listen(port, () => {
  console.log('We are live on ' + port);
});
结果为空:

{
  "data": {
    "mdc": null
  }
}
我真的不明白怎么了。我认为这可能与类型查询的定义有关,主要是因为我不理解它的用途,但可能是我在其他地方出了问题


谢谢

我认为,这是正确的:

// schemas.js
const resolvers = {
    Query: {
        mdc: function() {
            return "mdc called (not sure if this is correct)"
        }
    },
    mdc: {
        getAll: MDCResolvers.MDC.getAll,
        getInitials: MDCResolvers.MDC.getWithInitials,
    },
    hello: function() {
        return "ciao"
    }
};
当你打电话的时候

{
    mdc {
        getAll
    } 
}
答案是

{
    "data": {
        "mdc": {
            "getAll": "get all called"
        }
    }
 }
{
    "data": {
        "mdc": {
            "getAll": "get all called"
        }
    }
 }