简体   繁体   中英

GraphQL - Nested Methods

I am very new to GraphQL and wonder if I can type individual methods for type members

Code

var express = require('express');
var graphqlHTTP = require('express-graphql');

var { buildSchema } = require('graphql');

var schema = buildSchema(`
    type Query {
        beitraege: Beitrag
    }

    type Beitrag {
        beitr_name: String
        erst_name: String
    }
`);

var root = {

    // This is the part that does not work
    Beitrag: () => {

        beitr_name: () => {
            return "Foo";
        };

        erst_name: () =>  {
            return "Bar";
        }
    },

};

var app = express();

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

app.listen(3000, "0.0.0.0", () => {
    console.log("Started on port 3000");
});

This does not work as GraphQL seems do disallow to support individual methods for type-members. So I get null while testing this. My question is:

Is there a possibility to nest methods in such a way or do I have to return both strings every time I make a query?

Edit:

Tested in NodeJS GraphiQL

Input Query

{
  beitraege {
    beitr_name
    erst_name
  }
}

Output

{
  "data": {
    "beitraege": null
  }
}

Got my mistake:

instead of Beitrag: () => it had to be beitraege: () => as I do not want to define type members but schema members. I guess.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM