Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions define-hosts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ If not provided, the default type is `http`. The following table lists the avail
| :----------- | :------------------------------------- | :-------------------------- |
| `http` | Connect to an HTTP or HTTPS web server | `http`, `graphql`, `models` |
| `postgresql` | Connect to a PostgreSQL database | `postgresql` |
| `dgraph` | Connect to a Dgraph database | `dgraph` |

We'll update this table as we add more host types.

Expand Down Expand Up @@ -187,3 +188,34 @@ For example, if using Neon, refer to the [Neon documentation](https://neon.tech/
</Tip>

</ResponseField>

## Dgraph Host

This host type supports connecting to Dgraph databases.
You can use the [Dgraph APIs](/sdk/dgraph) in the Functions SDK to interact with the database.

**Example:**

```json hypermode.json
{
"hosts": {
"my-dgraph": {
"type": "dgraph",
"grpcTarget": "frozen-mango.grpc.eu-central-1.aws.cloud.dgraph.io:443",
"key": "{{DGRAPH_API_KEY}}"
}
}
}
```

<ResponseField name="type" type="string" required>
Always set to `"dgraph"` for this host type.
</ResponseField>

<ResponseField name="grpcTarget" type="string" required>
The gRPC target for the Dgraph database.
</ResponseField>

<ResponseField name="key" type="string" required>
The API key for the Dgraph database.
</ResponseField>
3 changes: 2 additions & 1 deletion mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
"sdk/graphql",
"sdk/http",
"sdk/models",
"sdk/postgresql"
"sdk/postgresql",
"sdk/dgraph"
]
}
]
Expand Down
250 changes: 250 additions & 0 deletions sdk/dgraph.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
---
title: Dgraph
description: "Execute queries and mutations against a Dgraph database."
---

{/* <!-- vale Google.Headings = NO --> */}

Hypermode's Dgraph APIs allow you to run queries and mutations against a Dgraph database, as well as alter
the schema if necessary.
After [defining a host](../define-hosts) for your Dgraph gRPC endpoint in your project's manifest,
you can use the following APIs to interact with the database.

## Example project

For your reference, a complete example using the Dgraph APIs is available on GitHub in the
`hypermodeAI/functions-as` repository, at [/examples/dgraph](https://github.com/hypermodeAI/functions-as/tree/main/examples/dgraph).

## Import from the SDK

To begin, import the `dgraph` namespace from the SDK:

<CodeGroup>

```ts AssemblyScript
import { dgraph } from "@hypermode/functions-as";
```

</CodeGroup>

## Dgraph APIs

The APIs in the `dgraph` namespace are below.

<Tip>
We're constantly introducing new APIs through ongoing development with build
partners. [Let's chat](mailto:[email protected]) about what would make the
Functions SDK even more powerful for your next use case!
</Tip>

### Functions

#### execute

Execute a Dgraph query or mutation using a Dgraph Request object.

<CodeGroup>

```ts AssemblyScript
function execute(hostName: string, request: Request): Response;
```

</CodeGroup>

<ResponseField name="hostName" type="string" required>
Name of the host, as [defined in the manifest](../define-hosts).
</ResponseField>

<ResponseField name="request" type="Request" required>
A Dgraph [`Request`](#request) object, describing the query or mutation to
execute.
</ResponseField>

#### alterSchema

Alter the schema of a Dgraph database.

<CodeGroup>

```ts AssemblyScript
function alterSchema(hostName: string, schema: string): string;
```

</CodeGroup>

<ResponseField name="hostName" type="string" required>
Name of the host, as [defined in the manifest](../define-hosts).
</ResponseField>

<ResponseField name="schema" type="string" required>
The schema to apply to the Dgraph database.
</ResponseField>

#### dropAttr

Drop an attribute from a Dgraph schema.

<CodeGroup>

```ts AssemblyScript
function dropAttr(hostName: string, attr: string): string;
```

</CodeGroup>

<ResponseField name="hostName" type="string" required>
Name of the host, as [defined in the manifest](../define-hosts).
</ResponseField>

<ResponseField name="attr" type="string" required>
The attribute to drop from the Dgraph schema.
</ResponseField>

#### dropAll

Drop all data from a Dgraph database.

<CodeGroup>

```ts AssemblyScript
function dropAll(hostName: string): string;
```

</CodeGroup>

<ResponseField name="hostName" type="string" required>
Name of the host, as [defined in the manifest](../define-hosts).
</ResponseField>

### Objects

#### Request

A Dgraph request object, used to execute queries and mutations.

<CodeGroup>

```ts AssemblyScript
class Request {
constructor(Query: Query | null = null, Mutations: Mutation[] | null = null);
query: Query = new Query();
mutations: Mutation[] = [];
}
```

</CodeGroup>

<ResponseField name="new dgraph.Request(query, mutations)">

Creates a new `Request` object with the given `query` and `mutations`.

The [`query`](#query) and [`mutations`](#mutation) fields are optional and default to `null`.

</ResponseField>

<ResponseField name="query" type="Query">
A Dgraph [`query`](#query) object.
</ResponseField>

<ResponseField name="mutations" type="Mutation[]">
An array of Dgraph [`mutation`](#mutation) objects.
</ResponseField>

#### Query

A Dgraph query object, used to execute queries.

<CodeGroup>

```ts AssemblyScript
class Query {
constructor(query: string = "", variables: Variables = new Variables());
query: string = "";
variables: Map<string, string> = new Map<string, string>();
}
```

</CodeGroup>

<ResponseField name="new dgraph.Query(query, variables)">
Creates a new `Query` object with the given `query` and `variables`. `query`
is a Dgraph Query Language (DQL) query string, and `variables` is a
[`Variables`](#variables) object.
</ResponseField>

<ResponseField name="query" type="string">
The DQL query to execute.
</ResponseField>

<ResponseField name="variables" type="Map<string, string>">
A map of query variables.
</ResponseField>

#### Variables

A Variables object used to set query variables in a Dgraph query.

<CodeGroup>

```ts AssemblyScript
class Variables {
public set<T>(name: string, value: T): void;
public toMap(): Map<string, string>;
}
```

</CodeGroup>

<ResponseField name="Variables.set(name, value)">
Sets a query variable with the given `name` and `value`. `name` is of type
`string`, and `value` can be of any type.
</ResponseField>

<ResponseField name="Variables.toMap()">
Returns a map of all query variables set in the `Variables` object.
</ResponseField>

#### Mutation

A Dgraph mutation object, used to execute mutations.

<CodeGroup>

```ts AssemblyScript
class Mutation {
constructor(
public setJson: string = "",
public delJson: string = "",
public setNquads: string = "",
public delNquads: string = "",
public condition: string = "",
)
}
```

</CodeGroup>

<ResponseField name="new dgraph.Mutation(setJson, delJson, setNquads, delNquads, condition)">
Creates a new `Mutation` object with the given `setJson`, `delJson`,
`setNquads`, `delNquads`, and `condition` fields.
</ResponseField>

<ResponseField name="setJson" type="string">
A JSON string representing the data to set in the mutation.
</ResponseField>

<ResponseField name="delJson" type="string">
A JSON string representing the data to delete in the mutation.
</ResponseField>

<ResponseField name="setNquads" type="string">
A string representing the data to set in the mutation in NQuads format.
</ResponseField>

<ResponseField name="delNquads" type="string">
A string representing the data to delete in the mutation in NQuads format.
</ResponseField>

<ResponseField name="condition" type="string">
A string representing the condition query for the mutation.
</ResponseField>
3 changes: 3 additions & 0 deletions sdk/functions-sdk.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ The following APIs are available in the Functions SDK:
<Card title="PostgreSQL" icon="database" href="/sdk/postgresql">
Execute queries against a PostgreSQL database.
</Card>
<Card title="Dgraph" icon="database" href="/sdk/dgraph">
Execute queries and mutations against a Dgraph database.
</Card>
</CardGroup>

## Installation
Expand Down
6 changes: 6 additions & 0 deletions styles/config/vocabularies/general/accept.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ UUID
nnClassify
serverless
getVector
gRPC
[Dd]graph
alterSchema
dropAll
NQuads
dropAttr