【问题标题】:GraphQL server with Deno带有 Deno 的 GraphQL 服务器
【发布时间】:2020-09-08 14:02:59
【问题描述】:

下面的代码只工作一次

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  buildSchema,
} from "https://cdn.pika.dev/graphql/^15.0.0";
import { serve } from "https://deno.land/std@0.50.0/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');

【问题讨论】:

    标签: javascript node.js express graphql deno


    【解决方案1】:
    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})
    

    【讨论】:

      【解决方案2】:

      您现在可以使用https://deno.land/x/deno_graphql 来实现此目标。

      它提供开箱即用所需的一切,并与多个 Deno 框架(oak、abc、actain 等)一起使用。

      这就是你的代码的样子(例如橡木):

      import { Application, Context, Router } from "https://deno.land/x/oak/mod.ts";
      import {
        gql,
        graphqlHttp,
        makeExecutableSchema,
      } from "https://deno.land/x/deno_graphql/oak.ts";
      
      const typeDefs = gql`
        type Query {
          hello: String
        }
      `;
      
      const resolvers = {
        Query: {
          hello: () => "Hello world!",
        },
      };
      
      const context = (context: Context) => ({
        request: context.request,
      });
      
      const schema = makeExecutableSchema({ typeDefs, resolvers });
      
      const app = new Application();
      const router = new Router();
      
      router.post("/graphql", graphqlHttp({ schema, context }));
      
      app.use(router.routes());
      
      await app.listen({ port: 4000 });
      

      PS : 我是这个包的作者,所以你可以问我任何问题。

      希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        这是一个使用 oak 处理您的 GraphQL 代码的示例。

        首先假设您有一个存储库graphRepository.ts,其中包含您的图形架构:

        import {
            graphql,
            GraphQLSchema,
            GraphQLObjectType,
            GraphQLString
        } from "https://cdn.pika.dev/graphql/^15.0.0";
        
        var schema = new GraphQLSchema({
            query: new GraphQLObjectType({
                name: "RootQueryType",
                fields: {
                    hello: {
                        type: GraphQLString,
                        resolve() {
                            return "world";
                        },
                    },
                },
            }),
        });
        
        export async function querySchema(query: any) {
            return await graphql(schema, query)
                .then(async (result) => {
                    return result;
                });
        }
        

        现在使用路由启动您的 app.ts 侦听器,并使用以下 URL 调用端点:

        http://localhost:8000/graph/query/hello

        import { Application, Router } from "https://deno.land/x/oak/mod.ts";
        import { querySchema } from "./graphRepository.ts";
        
        const router = new Router();
        
        router
            .get("/graph/query/:value", async (context) => {
                const queryValue: any = context.params.value;
                const query = `{ ${queryValue}}`
                const result = await querySchema(query);
                console.log(result)
                context.response.body = result;
            })
        
        const app = new Application();
        app.use(router.routes());
        app.use(router.allowedMethods());
        
        await app.listen({ port: 8000 });
        

        【讨论】:

        • @HemantMetallia 我重写了我的答案,看看是否能解决你的问题
        【解决方案4】:

        这是一个使用 Oak 和中间件的代码示例。 您还可以像阿波罗一样享受游乐场的 GUI。

        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();
        
        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);
              return {
                firstName: "wooseok",
                lastName: "lee",
              };
            },
          },
          Mutation: {
            setUser: (parent: any, {firstName, lastName}: any, context: any, info: any) => {
              console.log("input:", firstName, lastName);
              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 });
        

        【讨论】:

          【解决方案5】:

          在操场 GUI 上给我“错误”:“JSON 输入意外结束”,任何解决此问题的建议

          【讨论】:

            【解决方案6】:

            我创建了gql 用于制作不绑定到 Web 框架的 GraphQL 服务器。上面的所有响应都显示了 Oak 集成,但您实际上不必使用它来拥有 GraphQL 服务器。你可以改用std/http

            import { serve } from 'https://deno.land/std@0.90.0/http/server.ts'
            import { GraphQLHTTP } from 'https://deno.land/x/gql/mod.ts'
            import { makeExecutableSchema } from 'https://deno.land/x/graphql_tools/mod.ts'
            import { gql } from 'https://deno.land/x/graphql_tag/mod.ts'
            
            const typeDefs = gql`
              type Query {
                hello: String
              }
            `
            
            const resolvers = {
              Query: {
                hello: () => `Hello World!`
              }
            }
            
            const schema = makeExecutableSchema({ resolvers, typeDefs })
            
            const s = serve({ port: 3000 })
            
            for await (const req of s) {
              req.url.startsWith('/graphql')
                ? await GraphQLHTTP({
                    schema,
                    graphiql: true
                  })(req)
                : req.respond({
                    status: 404
                  })
            }
            

            【讨论】:

              猜你喜欢
              • 2020-12-25
              • 2019-09-18
              • 2019-01-27
              • 2019-01-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-05-30
              • 2019-12-01
              相关资源
              最近更新 更多