Hello,
Could you please help me to find where can I find an answer or documentation describing this situation?
I know I can easily do this if I use javascript for building the scheme, but the syntax needs much more words :D
Here is the code of schema and resolver:
const { buildSchema } = require('graphql');
const schema = buildSchema(/* GraphQL */ `
type Position {
x: String
y: String
}
type Location {
name: String!
position: Position
}
type Query {
locations: [Location!]!
}
`);
const root = {
Location: {
position() {
// this line is not called <-------------------
return new Promise(resolve => {
setTimeout(() => {
resolve({ x: 111, y: 222 });
}, 300);
});
},
},
locations() {
return [{ name: 'test' }];
},
};
module.exports = {
schema,
root,
};
Here is the query
{
locations {
name
position {
x
y
}
}
}
here is the result:
{
"data": {
"locations": [
{
"name": "test",
"position": null
}
]
}
}
Versions:
"express": "^4.14.1",
"express-graphql": "^0.6.6",
"graphql": "^0.10.1",
Thanks
Okay, I got it. Found answer here http://graphql.org/graphql-js/object-types/
Thanks for reporting back @asci. Glad to hear that you got things sorted out.
@asci can you please explain what you did to resolve this.
@vivex In @asci example you should change Location => 'locations` here:
const root = {
Location: {
position() {
Details here: http://graphql.org/graphql-js/object-types/
@IvanGoncharov @asci Can you post the root variable here. I still couldn't understand
For anyone else that could potentially be stuck on this, the key point on the Object Types page is this:
Instead of a root-level resolver for the RandomDie type, we can instead use an ES6 class, where the resolvers are instance methods.
The example from the OP can be made to work with the following changes:
Add a Location class:
class Location {
constructor(public name: string) { }
async position() {
// this line is not called <-------------------
return new Promise(resolve => {
setTimeout(() => {
resolve({x: 111, y: 222});
}, 300);
});
}
}
Return an array of Location objects from the locations resolver:
export function createRootResolver() {
return {
locations: () => {
return [new Location('test')];
},
};
}
Most helpful comment
@IvanGoncharov @asci Can you post the root variable here. I still couldn't understand