Express-graphql: Need guide for writing function to resolve root data type with nested type

Created on 17 Aug 2017  路  4Comments  路  Source: graphql/express-graphql

Hi all

Please guide how to resolve data with nested type

Query

query {
  article {
    content
    author {
      name
    }
  }
}

Server

const express = require('express')
const fs = require('fs')
const graphQLHTTP = require('express-graphql')
const { buildSchema } = require('graphql')

const app = express()

const schema = buildSchema(`
    type Article {
        content: String
        author: Author
    }

    type Author {
        name: String
    }

    type Query {
        article: Article
    }
`)

const resolver = {
    article() {
        // data in database is kept with ID as primary key, so how to get the next function to be executed to get related data
        return { content: 'Hello World', authorId: 1 }
    },
    author() {
        // this function never get executed, please guide how
        return { name: 'Steve' }
    }
}

app.use(graphQLHTTP({
    schema,
    rootValue: resolver,
    graphiql: true
}))

app.listen(3000)

Thanks

Most helpful comment

Generate a class based return to solve this problem.
check here, or official doc

All 4 comments

Same problem faced. Any methods solve this ?

Generate a class based return to solve this problem.
check here, or official doc

See...This
var schema=buildSchema(`
type Query{
user:User
}
type User{
Name:String
Password:String
}

`);

var resolver={
user(){
return{
Name()
{
return 'What u want to return';
},
Password()
{
return 'What u want to return';
}
}
}
}

@jeud I think @Jack-Kingdom answered your question fully. Can I close this issue?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

langpavel picture langpavel  路  5Comments

m-diiorio picture m-diiorio  路  4Comments

dmytro-kerest picture dmytro-kerest  路  3Comments

jamesmoriarty picture jamesmoriarty  路  4Comments

AchuthSankar picture AchuthSankar  路  5Comments