New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need guide for writing function to resolve root data type with nested type #379

Closed
jeud opened this Issue Aug 17, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@jeud
Copy link

jeud commented Aug 17, 2017

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

@Jack-Kingdom

This comment has been minimized.

Copy link

Jack-Kingdom commented Aug 28, 2017

Same problem faced. Any methods solve this ?

@Jack-Kingdom

This comment has been minimized.

Copy link

Jack-Kingdom commented Aug 29, 2017

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

@surajBpandey

This comment has been minimized.

Copy link

surajBpandey commented Apr 4, 2018

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';
}
}
}
}

@IvanGoncharov

This comment has been minimized.

Copy link
Collaborator

IvanGoncharov commented Apr 20, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment