Last active
October 20, 2022 16:43
-
-
Save danba340/ce04651f8fc14bcb974fb4b940d95bdd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thank you for this gist.
I had to make some changes so it can be executable on Deno 1.17.x: