9

它只适用于以下代码

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');
4

6 回答 6

6
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})
于 2020-05-23T02:31:16.767 回答
5

您现在可以使用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:我是包的作者,所以你可以问我任何问题。

希望这可以帮助!

于 2020-05-26T09:58:40.680 回答
1

这是一个使用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 });
于 2020-05-21T20:16:18.743 回答
0

这是一个使用橡木和中间件的代码示例。您还可以像阿波罗一样享受游乐场 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 });
于 2020-05-25T14:37:23.047 回答
0

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

于 2020-05-27T09:58:44.597 回答
0

我创建了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
      })
}
于 2021-05-03T10:48:04.680 回答