So I am trying to play around with setting up a graphQL server. I have the following data
const events = [
{
id: 'event-1',
title: 'A cool title',
resource: 'resource-1',
}
];
const resources = [
{
id: 'resource-1',
name: 'John',
}
];
So I am using buildSchema
and not understanding how I tell it to resolve the data for the resource. When I start the server I can auto complete the properties, but when I hit play, resource is returning as null.
const schema = buildSchema(`
type Query {
events(id: String): [Event]
resources(name: String): [Resource]
}
type Resource {
id: Int
name: String
}
type Event {
id: String
title: String
resource: Resource
}
`);
const getResources = (args) => {
if (args.name) {
const name = args.name;
return resources.filter((resource) => resource.name === name);
} else {
return resources;
}
};
const getEvents = (args) => {
if (args.title) {
const name = args.title;
return events.filter((event) => event.title === name);
} else {
return events;
}
};
const root = {
events: getEvents,
resources: getResources,
}
app.use(
"/",
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
})
);
Not sure if its the way I built the data or am I missing something else?
Here is the query
{
events {
id
title
resource {
name
}
}
}
Response I am getting
{
"data": {
"events": [
{
"id": "event-1",
"title": "Trim",
"resource": null
}
]
}
}