dgraph icon indicating copy to clipboard operation
dgraph copied to clipboard

[ENHANCEMENT]: Avoid repeating identical HTTP requests in @custom fields

Open damonfeldman opened this issue 1 year ago • 0 comments

Use case and current behavior

Currently an @custom field such as

type User { username: String! @id ... posts: [Post] @custom(http: { url: "https://my.api.com/person/$username/posts", method: GET }) }

may request the same "itemType" information many times in a single query. This can lead to varied results if the API info changes, and is inefficient.

Enhancement

Instead, the exact same URL should make one call per Dgraph transaction. For requests with a post body, GraphQL request or other information, the entire request may be needed to be used as a key, or hashed and used as a key to avoid duplicates.

This may not make sense under the current restriction per the docs that "each custom call must include either an ID or @id field" which may mean that the requests must already include a unique identifier, which prevents many requests of the same item in most cases.

Overall, any item should be return-able via @custom, and if there are duplicate requests within a transaction, they should be consolidated to one request.

Ideally, this will work both with regular Entities in Dgraph (fields and entity UIDs returned) and @remote types where the entire data object is returned from an external source.

Solution proposal

This could be accomplished by keeping a map of URLs called, and responses for the transaction duration.

Links to Discuss, RFC or previous Issues and PRs

No response

Links to examples and research

No response

Additional Information

See @custom docs here: https://dgraph.io/docs/graphql/custom/directive/

damonfeldman avatar Aug 11 '23 21:08 damonfeldman