在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):aaronwlee/oak-graphql开源软件地址(OpenSource Url):https://github.com/aaronwlee/oak-graphql开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):Read MeHello, it's Aaron.
Firstly, I'd like to say thanks for using and loving Unfortunately, it is nearly impossible to maintain Thus, I need some help! If someone wants to maintain it, please don't hesitate to give me an email.
I want someone can have permission who update and push it to I hope y'all are doing well in this bad situation. Regards Oak-GraphQLA simple graphql middleware for oak deno framework. ! Make sure your playground endpoint indicates same as your URL http://localhost:8080/graphql Simple rundeno run --allow-net --unstable index.ts Simple example index.tsimport { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
import { applyGraphQL, gql, GQLError } 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 User {
firstName: String
lastName: String
}
input UserInput {
firstName: String
lastName: String
}
type ResolveType {
done: Boolean
}
type Query {
getUser(id: String): User
}
type Mutation {
setUser(input: UserInput!): ResolveType!
}
`;
const resolvers = {
Query: {
getUser: (parent: any, { id }: any, context: any, info: any) => {
console.log("id", id, context);
if(context.user === "Aaron") {
throw new GQLError({ type: "auth error in context" })
}
return {
firstName: "wooseok",
lastName: "lee",
};
},
},
Mutation: {
setUser: (parent: any, { input: { firstName, lastName } }: any, context: any, info: any) => {
console.log("input:", firstName, lastName);
return {
done: true,
};
},
},
};
const GraphQLService = await applyGraphQL<Router>({
Router,
typeDefs: types,
resolvers: resolvers,
context: (ctx) => {
// this line is for passing a user context for the auth
return { user: "Aaron" };
}
})
app.use(GraphQLService.routes(), GraphQLService.allowedMethods());
console.log("Server start at http://localhost:8080");
await app.listen({ port: 8080 }); TODO
Methodgql
GQLErrorAn error handler
throw new GQLError("string");
or
throw new GQLError({type: "General error", detail: "somthing critical!"}); applyGraphQLA Generator which based Attain Router class creates some middlewares for supporting the GraphQL.
const resolvers = {
Query: {
getUser: (parent: any, {id}: any, context: any, info: any) => {
// ...query handling function here
},
},
Mutation: {
addUser: (parent: any, {firstName, lastName}: any, context: any, info: any) => {
// ...add user codes here
},
}
}
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论