Dgraph returns error when pass id to lambda query

i cant pass id parameter to lambda query.
Anyone have an idea for this ?

my schema:

type Content {
    id:ID!
	status: Int
    reviewer_response: String
    title: String
    video: String
    video_time: Int 
    Thumbnail: String
    Form_values: String
    Search_string: String
    publish: Boolean!
    created_at: DateTime
    updated_at: DateTime
}

type Query {
    contentDetailsById(id: ID!): [Content] @lambda
}

my lambda script

async function contentDetailsById({args, dql}) {
    const results = await dql.query(`query getContent($id:ID) {
        getContent(func: type(Content)) @filter(eq(Content.id, $id) AND eq(Content.publish,true)) {
            title: Content.title
        }
    }`, {"$id": args.id})
    return results.data.getContent
}

self.addGraphQLResolvers({
    "Query.contentDetailsById": contentDetailsById,
})

my query

query {
	contentDetailsById(id: "0x7aeb54747") {
		title
	}
}

What is your error?

my error:

Evaluation of custom field failed because external request returned an error: unexpected error with: 500 for field: contentDetailsById within type: Query.

Is there an error in your lambda logs?

yes my lambda log has this error

TypeError: Cannot read property 'getContent' of null

So your lambda query did not return what you expected. Try this:

async function contentDetailsById({args, dql}) {
    const results = await dql.query(`query getContent($id:string) {
        getContent(func: type(Content)) @filter(eq(Content.id, $id) AND eq(Content.publish,true)) {
            title: Content.title
        }
    }`, {"$id": args.id})
    if (results && results.data && results.data.getContent) return results.data.getContent
    console.log('Invalid getContent: ', JSON.stringify(results))
    return []
}

self.addGraphQLResolvers({
    "Query.contentDetailsById": contentDetailsById,
})

Explanation:

  1. Replace input type of ID (GraphQL) to string (DQL)
  2. Add condition to check response
  3. If not return response then log response to lambda console
  4. Return empty list to prevent GraphQL error.

This query has no error but it does not return an answer and only returns an empty array !!
lamda also has no error log .

this is wrong because query should return title of node 0x7aeb54747.

finally i solve the problem. my lambda code is wrong for id filtering.
this is correct code:

async function contentDetailsById({args, dql}) {
    const results = await dql.query(`query getContent($id:string) {
        getContent(func: type(Content)) @filter(uid($id) AND eq(Content.publish,true)) {
            title: Content.title
        }
    }`, {"$id": args.id})
    if (results && results.data && results.data.getContent) return results.data.getContent
    console.log('Invalid getContent: ', JSON.stringify(results))
    return []
}
1 Like

Sorry, I dodn’t catch the wrong uid filter.

It would also be more performant if you put the uid function as the root and the type function in the filter, FYI.