Hi all
Please guide how to resolve data with nested type
query {
article {
content
author {
name
}
}
}
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
__authorID__ on your object won't be resolved because you didn't define __authorID__ on __type Article__.
I have not found a working thing but I found that I can pass a function on author property which returns an object.
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',
author: () => ({
name: 'Steve'
})
}
}
}
This returns the object which resolves to __type Author__. The only problem here is that I can't find ___author___ from __type Article__. I've tried some things but to no avail.
Any help on this particular issue?
Also, the arguments of the resolver function is not documented. Resolver functions have three arguments: args(Object type), context(Object type) and fieldDef.
same issue.
@jeud @kazahara @mrdulin Sorry for delay. Here is how it you should implement such APIs:
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() {
return new Article({ content: 'Hello World', authorId: 1 })
},
}
class Article {
constructor(data) {
this.content = data.content;
this._authorId = data.authorId;
}
author() {
// const data = getAuthorDataFromDB(this._authorId);
const data = { name: 'Steve' };
return new Author(data);
}
}
class Author {
constructor(data) {
this.name = data.name;
}
}
app.use(graphQLHTTP({
schema,
rootValue: resolver,
graphiql: true
}))
app.listen(3000)
Here are the official docs with another example: http://graphql.org/graphql-js/object-types/
@IvanGoncharov thanks. Does this way lead N+1 problem?
What about these type definition?
type Person {
id: ID!
name: String
friends: [Person]!
}
type Query {
person(id: ID!): Person
}
type User {
id: ID!
name: String
post: [Post]!
}
type Post {
id: ID!
title: String
author: User!
}
type Query {
user(id: ID!): User
postsByUserId(id: ID!): [Post]!
postById(id: ID!): Post
}
How can I write rootValue for these two types? Is there common guide for this? I don't want to list every case and ask you. Thanks
Without graphql-tools and constructor type definition
@mrdulin This repo issues are reserved for reporting bugs and asking for features.
https://github.com/graphql/graphql-js/blob/master/.github/ISSUE_TEMPLATE.md#questions-regarding-how-to-use-graphql
Most helpful comment
@jeud @kazahara @mrdulin Sorry for delay. Here is how it you should implement such APIs:
Here are the official docs with another example: http://graphql.org/graphql-js/object-types/