Skip to content

Commit bfd22bf

Browse files
committed
first pass at graphql support
1 parent 8152736 commit bfd22bf

File tree

8 files changed

+1527
-14
lines changed

8 files changed

+1527
-14
lines changed

api/graphql/function.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "anonymous",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "req",
8+
"methods": ["get", "post"],
9+
"route": "todo/graphql"
10+
},
11+
{
12+
"type": "http",
13+
"direction": "out",
14+
"name": "$return"
15+
}
16+
],
17+
"scriptFile": "../dist/graphql/index.js"
18+
}

api/graphql/index.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { AzureFunction, Context, HttpRequest } from '@azure/functions'
2+
import { PrismaClient, Todo } from '@prisma/client'
3+
import { ApolloServer, gql } from "apollo-server-azure-functions";
4+
import { readFileSync } from "fs";
5+
6+
const prisma = new PrismaClient()
7+
8+
// Construct a schema, using GraphQL schema language
9+
const typeDefs = gql`
10+
type Query {
11+
todo(
12+
id: ID!
13+
): Todo
14+
15+
todoList: [Todo!]!
16+
}
17+
18+
type Mutation {
19+
20+
addTodo(
21+
title: String!,
22+
completed: Boolean
23+
): Todo
24+
25+
updateTodo(
26+
id: ID!,
27+
title: String,
28+
completed: Boolean
29+
): Todo
30+
31+
deleteTodo(
32+
id: ID!
33+
): Todo
34+
35+
}
36+
37+
type Todo {
38+
id: ID!
39+
title: String!
40+
completed: Boolean
41+
}
42+
`
43+
44+
// Provide resolver functions for your schema fields
45+
const resolvers = {
46+
Query: {
47+
48+
todoList: () => {
49+
return prisma.todo.findMany().then(value => value.map(todo => toClientToDo(todo)))
50+
},
51+
52+
todo: (parent, args) => {
53+
return prisma.todo.findUnique({
54+
where: {
55+
id: parseInt(args.id, 10)
56+
},
57+
}).then(value => toClientToDo(value))
58+
}
59+
60+
},
61+
62+
Mutation: {
63+
64+
addTodo: (parent, args) => {
65+
return prisma.todo.create({
66+
data: {
67+
todo: args.title,
68+
completed: args.completed
69+
}
70+
}).then(value => toClientToDo(value))
71+
},
72+
73+
updateTodo: (parent, args) => {
74+
return prisma.todo.update({
75+
data: {
76+
todo: args.title,
77+
completed: args.completed ?? false
78+
},
79+
where: {
80+
id: parseInt(args.id, 10)
81+
}
82+
}).then(value => toClientToDo(value))
83+
},
84+
85+
deleteTodo: (parent, args) => {
86+
return prisma.todo.delete({
87+
where: {
88+
id: parseInt(args.id, 10)
89+
}
90+
}).then(value => toClientToDo(value))
91+
}
92+
93+
}
94+
};
95+
96+
const toClientToDo = (todo: Todo) => {
97+
return {
98+
id: todo.id,
99+
title: todo.todo ,
100+
completed: todo.completed
101+
}
102+
}
103+
104+
// @ts-ignore
105+
const server = new ApolloServer({ typeDefs, resolvers, debug: true, playground: true });
106+
107+
export default server.createHandler({
108+
cors: {
109+
origin: '*'
110+
},
111+
});

api/host.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
},
1111
"extensionBundle": {
1212
"id": "Microsoft.Azure.Functions.ExtensionBundle",
13-
"version": "[1.*, 2.0.0)"
13+
"version": "[2.*, 3.0.0)"
1414
}
1515
}

0 commit comments

Comments
 (0)