GraphQL-tools: How do I write resolver function to search a list?

162 views Asked by At

I'm just learning GraphQL, and am struggling on the return statement in a resolver function. All I want is to search for an item by ID.

My search:

query {
  link(id: "link-0") {
    url
  }
}

I keep getting this error: "message": "Cannot return null for non-nullable field Link.url."

I think the issue is in my map function return statement, but I've tried with links.find() and other ways and just can't get it to work.

let links = [
    {
        id: "link-0",
        url: 'www.gmail.com',
        description: 'Mail inbox',
    },
    {
        id: 'link-1',
        url: 'www.google.com',
        description: 'Search engine',
    },
];

type Query {
    info: String!
    feed: [Link!]!
    link(id: ID!): Link!
}

type Link {
    id: ID!
    description: String!
    url: String!
}

const resolvers = {
    Query: {
        feed: () => links,
        link: (parent, args) => links.map(link => {
            if (link.id == args.id) {
                return link;
            }
        }),
    },
}
0

There are 0 answers