Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Dockerized Deno GraphQL server #9

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
initial deno docker container
myty committed Oct 26, 2020
commit 7f6e28a64cf328c408f783ec51ab2f737741dc7c
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -23,5 +23,12 @@
"frontend/build/**": true,
"frontend/src/tailwind.output.css": true
},
"typescript.tsdk": "./frontend/node_modules/typescript/lib"
"typescript.tsdk": "./frontend/node_modules/typescript/lib",
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true,
"deno.import_intellisense_origins": {
"https://deno.land": true
},
"deno.import_intellisense_autodiscovery": true
}
40 changes: 40 additions & 0 deletions deno/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
FROM frolvlad/alpine-glibc:alpine-3.11_glibc-2.31

ENV DENO_VERSION=1.4.5

RUN apk add --virtual .download --no-cache curl \
&& curl -fsSL https://github.com/denoland/deno/releases/download/v${DENO_VERSION}/deno-x86_64-unknown-linux-gnu.zip \
--output deno.zip \
&& unzip deno.zip \
&& rm deno.zip \
&& chmod 777 deno \
&& mv deno /bin/deno \
&& apk del .download

RUN addgroup -g 1993 -S deno \
&& adduser -u 1993 -S deno -G deno \
&& mkdir /deno-dir/ \
&& chown deno:deno /deno-dir/

ENV DENO_DIR /deno-dir/
ENV DENO_INSTALL_ROOT /usr/local

# The port that your application listens to.
EXPOSE 1993

WORKDIR /app

# Prefer not to run as root.
USER deno

# Cache the dependencies as a layer (the following two steps are re-run only when deps.ts is modified).
# Ideally cache deps.ts will download and compile _all_ external files used in main.ts.
COPY deps.ts .
RUN deno cache deps.ts

# These steps will be re-run upon each file change in your working directory:
ADD . .
# Compile the main app so that it doesn't need to be compiled each startup/entry.
RUN deno cache main.ts

CMD ["deno", "run", "--allow-net", "main.ts"]
10 changes: 10 additions & 0 deletions deno/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export {
Application,
Router,
RouterContext,
} from "https://deno.land/x/[email protected]/mod.ts";
export {
applyGraphQL,
gql,
GQLError,
} from "https://deno.land/x/[email protected]/mod.ts";
68 changes: 68 additions & 0 deletions deno/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Application } from "./deps.ts";
import { applyGraphQL, gql } from "./deps.ts";

const app = new Application();

const types = gql`
type Dino {
name: String
image: String
}
input DinoInput {
name: String
image: String
}
type ResolveType {
done: Boolean
}
type Query {
getDino(name: String): Dino
getDinos: [Dino!]!
}
type Mutation {
addDino(input: DinoInput!): ResolveType!
}
`;

const dinos = [
{
name: "Tyrannosaurus Rex",
image: "🦖",
},
];

const resolvers = {
Query: {
getDino: (_, { name }) => {
const dino = dinos.find((dino) => dino.name.includes(name));
if (!dino) {
throw new Error(`No dino name includes ${name}`);
}
return dino;
},
getDinos: () => {
return dinos;
},
},
Mutation: {
addDino: (_, { input: { name, image } }) => {
dinos.push({
name,
image,
});
return {
done: true,
};
},
},
};

const GraphQLService = applyGraphQL({
typeDefs: types,
resolvers: resolvers,
});

app.use(GraphQLService.routes(), GraphQLService.allowedMethods());

console.log("Server start at http://localhost:1993");
await app.listen({ port: 1993 });
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3.8"
services:
deno-graphql:
build:
context: ./deno
dockerfile: Dockerfile
ports:
- "1993:1993"