带有Deno的GraphQL服务器

赫曼特·梅塔利亚

以下代码仅工作一次

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  buildSchema,
} from "https://cdn.pika.dev/graphql/^15.0.0";
import { serve } from "https://deno.land/[email protected]/http/server.ts";

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return "world";
        },
      },
    },
  }),
});

var query = "{ hello }";

graphql(schema, query).then((result) => {
  console.log(result);
});

如何保持聆听,express就像这样

var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

// The root provides a resolver function for each API endpoint
var root = {
  hello: () => {
    return 'Hello world!';
  },
};

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
拉克什·贾恩(Rakesh Jain)
import {
    graphql,
    buildSchema,
} from "https://cdn.pika.dev/graphql/^15.0.0";
import {Application, Router} from "https://deno.land/x/oak/mod.ts";

var schema = buildSchema(`
  type Query {
    hello: String
  }
`);

var resolver = {hello: () => 'Hello world!'}

const executeSchema = async (query:any) => {
    const result = await graphql(schema, query, resolver);   
    return result;
}

var router = new Router();

router.post("/graph", async ({request, response}) => {
    if(request.hasBody) {
        const body = await request.body();
        const result = await executeSchema(body.value);
        response.body = result;
    } else {
        response.body = "Query Unknown";
    }
})


let app = new Application();
app.use(router.routes());
app.use(router.allowedMethods());
console.log("Server running");
app.listen({port: 5000})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有XAMPP的SMTP服务器?

来自分类Dev

Spring OAuth:带有授权服务器后端的资源服务器

来自分类Dev

带有代理到 Hugo 服务器的 Caddy 服务器无法加载 CSS

来自分类Dev

带有Keycloak服务器的Zuul反向代理

来自分类Dev

带有Django的简单远程服务器

来自分类Dev

带有服务器发送事件的AngularJS

来自分类Dev

内部服务器错误500(带有IIS日志)

来自分类Dev

带有corbaloc访问权限的MICO CORBA服务器

来自分类Dev

带有阻止请求的Gevent异步服务器

来自分类Dev

带有Spray Akka超线程的Scala服务器

来自分类Dev

带有MySQL服务器的AngularJs实时应用

来自分类Dev

带有AWS SES错误的Acumatica SMTP服务器

来自分类Dev

带有php内置服务器的Docker

来自分类Dev

带有XML的POST烧瓶服务器(来自python)

来自分类Dev

带有easyrtc的TURN服务器不起作用

来自分类Dev

Playframework:无法编译带有服务器示例的测试

来自分类Dev

带有节点MySQL的无服务器框架

来自分类Dev

带有Crystal的类似WEBrick的服务器

来自分类Dev

如何设置带有静态IP的VirtualBox服务器?

来自分类Dev

从服务器运行带有显示的Jar

来自分类Dev

来自带有Rails的SMTP服务器的响应

来自分类Dev

带有服务器时间PHP的Javascript时钟

来自分类Dev

带有外部nuget的Teamcity nuget服务器

来自分类Dev

带有“连接到服务器”选项的问题

来自分类Dev

带有两个远程服务器的Githooks

来自分类Dev

带有SSH -X的远程X服务器

来自分类Dev

带有远程GUI管理的SVN服务器

来自分类Dev

带有tomcat的Apache Web服务器

来自分类Dev

带有Aurelia-CLI服务器的CORS