0

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
      }
    ]
  }
}
jrock2004
  • 3,229
  • 5
  • 40
  • 73
  • ... and we should guess what query and what response? – xadm Nov 15 '20 at 16:52
  • Great point, let me update – jrock2004 Nov 15 '20 at 16:55
  • data contains 'resource' then no need to resolve it deeper (no resource resolver called) ... but string doesn't match required type then null returned – xadm Nov 15 '20 at 16:59
  • Not sure I am understanding your response @xadm – jrock2004 Nov 15 '20 at 17:03
  • events resolver gets all fields/props populated ... 'resource' field/prop has a value then this set of data is assumed as resolved and ready to return as response ... but on result validation value for 'resource' is not validates as not matching with requested response type and replaced with null – xadm Nov 15 '20 at 17:08
  • So I need to somehow tell it to do another query to get that data. Would I do that during the `getEvents` function? – jrock2004 Nov 15 '20 at 17:12
  • rename data field f.e. to `resource_data` and you need a separate 'resource' resolver, it will get a 1st arg/parent set to event then `parent.resource_data` will be your key to find within resources ... but sorry, search for syntax/some tutorial, how to define this 'nested' resolver 'manually' – xadm Nov 15 '20 at 17:17
  • 1
    Don't use `buildSchema`. Period. If you want to use SDL to create your schema, use `makeExecutableSchema` from `graphql-tools`. Otherwise, you need to build your `GraphQLSchema` object programmatically. See the linked dupe for a more detailed explanation. – Daniel Rearden Nov 15 '20 at 17:43
  • 1
    More details about why you shouldn't use `buildSchema` are provided [here](https://stackoverflow.com/questions/53984094/notable-differences-between-buildschema-and-graphqlschema/53987189#53987189). – Daniel Rearden Nov 15 '20 at 17:44

0 Answers0