Skip to content

Commit e20baf6

Browse files
committed
dql is now dql.query and dql.mutate
1 parent 2d486f8 commit e20baf6

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ self.addGraphQLResolvers({
1212

1313
async function reallyComplexDql({ parents, dql }) {
1414
const ids = parents.map(p => p.id);
15-
const someComplexResults = await dql(`really-complex-query-here with ${ids}`);
15+
const someComplexResults = await dql.query(`really-complex-query-here with ${ids}`);
1616
return parents.map(parent => someComplexResults[parent.id])
1717
}
1818

slash-graphql-lambda-types/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@slash-graphql/lambda-types",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Types for building out a Dgraph Lambda",
55
"main": "index.js",
66
"types": "types.d.ts",

slash-graphql-lambda-types/types.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ module "@slash-graphql/lambda-types" {
2121
type GraphQLEvent = GraphQLEventFields & {
2222
respondWith: (r: ResolverResponse) => void,
2323
graphql: (s: string, vars: Record<string, any> | undefined) => Promise<GraphQLResponse>,
24-
dql: (s: string, vars: Record<string, any> | undefined) => Promise<GraphQLResponse>,
24+
dql: {
25+
query: (s: string, vars: Record<string, any> | undefined) => Promise<GraphQLResponse>
26+
mutate: (s: string) => Promise<GraphQLResponse>
27+
},
2528
}
2629

2730
type GraphQLEventWithParent = GraphQLEvent & {

src/dgraph.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export async function graphql(query: string, variables: Record<string, any> = {}
1717
return response.json();
1818
}
1919

20-
export async function dql(query: string, variables: Record<string, any> = {}): Promise<GraphQLResponse> {
20+
async function dqlQuery(query: string, variables: Record<string, any> = {}): Promise<GraphQLResponse> {
2121
const response = await fetch(`${process.env.DGRAPH_URL}/query`, {
2222
method: "POST",
2323
headers: {
@@ -31,3 +31,23 @@ export async function dql(query: string, variables: Record<string, any> = {}): P
3131
}
3232
return response.json();
3333
}
34+
35+
async function dqlMutate(mutate: string): Promise<GraphQLResponse> {
36+
const response = await fetch(`${process.env.DGRAPH_URL}/mutate?commitNow=true`, {
37+
method: "POST",
38+
headers: {
39+
"Content-Type": "pplication/rdf",
40+
"X-Auth-Token": process.env.DGRAPH_TOKEN || ""
41+
},
42+
body: mutate
43+
})
44+
if (response.status !== 200) {
45+
throw new Error("Failed to execute DQL Mutate")
46+
}
47+
return response.json();
48+
}
49+
50+
export const dql = {
51+
query: dqlQuery,
52+
mutate: dqlMutate,
53+
}

src/evaluate-script.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe(evaluateScript, () => {
6060
it("works with dgraph dql", async () => {
6161
const runScript = evaluateScript(`
6262
async function todoTitles({dql}) {
63-
const results = await dql('{ queryTitles(func: type(Todo)){ Todo.title } }')
63+
const results = await dql.query('{ queryTitles(func: type(Todo)){ Todo.title } }')
6464
return results.data.queryTitles.map(t => t["Todo.title"])
6565
}
6666
addGraphQLResolvers({ "Query.todoTitles": todoTitles })`)

0 commit comments

Comments
 (0)