Sumchans
Sumchans

Reputation: 3774

GraphQL returning data using mongodb native driver

I am trying return some data in graphiql using the mongodb native driver not mongoose. For some reason I don't get the data returned on graphiql. but when I console.log it I am able to see it.

Here is my Query

 type Highlight {
        id: ID!
        content: String!
        title: String
        author: String
    }    
    type Query {
        highlights: [Highlight]!
        highlight(id: ID!): Highlight
    }

Here is my resolver -

Query: {
    highlights: getHighlights,
}

Here is the getHighlights function -

async function getHighlights() {
  try {
    await client.connect();
    console.log("Connected correctly to server");
    const db = client.db(dbName);

    db.collection("highlights").find({})(function (err, result) {
      if (err) throw err;     
      return result;
    });

  } catch (err) {
    console.log(err.stack);
  }
}

When I return this is what I see on graphiql enter image description here

EDIT - 28/04/2021 enter image description here

Upvotes: 0

Views: 92

Answers (2)

Metcalfe
Metcalfe

Reputation: 121

I had the same error. My issue was I'd misspelled one of the attribute names in the database. Quick one to check.

Upvotes: 0

mohammad nowresideh
mohammad nowresideh

Reputation: 158

you have to return your data that you got from database, you can see the error from grahql in message property:

async function getHighlights() {
  try {
    await client.connect();
    console.log("Connected correctly to server");
    const db = client.db(dbName);

    const data = db.collection("highlights").find({})(function (err, result) {
      if (err) throw err;     
      return result;
    });

    return data

  } catch (err) {
    console.log(err.stack);
  }
}

Upvotes: 1

Related Questions