Skip to content

Instantly share code, notes, and snippets.

@danba340
Last active October 20, 2022 16:43
Show Gist options
  • Save danba340/ce04651f8fc14bcb974fb4b940d95bdd to your computer and use it in GitHub Desktop.
Save danba340/ce04651f8fc14bcb974fb4b940d95bdd to your computer and use it in GitHub Desktop.
import { Application } from "https://deno.land/x/oak/mod.ts";
import { applyGraphQL, gql } from "https://deno.land/x/oak_graphql/mod.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:8080");
await app.listen({ port: 8080 });
@eduardorangell
Copy link

Hi, thank you for this gist.

I had to make some changes so it can be executable on Deno 1.17.x:

// deno-lint-ignore-file no-explicit-any
import { Application, Router } from 'https://deno.land/x/oak/mod.ts';
import { applyGraphQL, gql } from 'https://deno.land/x/oak_graphql/mod.ts';

const app = new Application();

app.use(async (ctx, next) => {
	await next();
	const rt = ctx.response.headers.get('X-Response-Time');
	console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
});

app.use(async (ctx, next) => {
	const start = Date.now();
	await next();
	const ms = Date.now() - start;
	ctx.response.headers.set('X-Response-Time', `${ms}ms`);
});

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: 'T-Rex',
		image: '🦖',
	},
];

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

const GraphQLService = await applyGraphQL<Router>({
	Router,
	typeDefs: types,
	resolvers: resolvers,
});

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

console.log('Server running on http://localhost:8000');
await app.listen({ port: 8000 });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment