简体   繁体   English

graphql查询嵌入在req.body中

[英]graphql query is embedded inside req.body

So I'm trying to move from REST to GraphQL but there's a minor thing that makes it a little bit harder is that queries eg 所以我正在尝试从REST迁移到GraphQL,但有一件小事使查询更加困难

www.example.com?test=test the query: { test: "test" } is actually under body: { query: { test: "test" } } in GraphQL. www.example.com?test=test这个query: { test: "test" }实际上在GraphQL中body: { query: { test: "test" } } So I was wondering if there was some middleware or something that could move this back out. 因此,我想知道是否存在某种中间件或某种可以将其撤回的中间件。

I'm using body-parser via 我通过使用body-parser

var bodyParser =
    require("body-parser")
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
    extended: true
})) 

Unless there's a specific reason you want to validate and execute the GraphQL document yourself, you should probably use an existing library for that. 除非有特定原因要自己验证和执行GraphQL文档,否则可能应该使用现有的库。 Two of the more popular solutions for express: 两种较流行的快递解决方案:

//express-graphql
const graphqlHTTP = require('express-graphql');
const { makeExecutableSchema } = require('graphql-tools');

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

app.use('/graphql', graphqlHTTP({
  schema,
  graphiql: true,
}));

app.listen(4000);

// apollo-server-express
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

const server = new ApolloServer({ typeDefs, resolvers });

server.applyMiddleware({ app });

app.listen(4000);

You can also just use apollo-server , which runs apollo-server-express under the hood. 您也可以只使用apollo-server ,它在后台运行apollo-server-express。

const { ApolloServer, gql } = require('apollo-server');

// pass in a schema
const server = new ApolloServer({
  schema
});

// or let Apollo make it for you
const server = new ApolloServer({
  typeDefs,
  resolvers,
});

server.listen()

None of these solutions require you to include body-parser and provide a ton of extra features out-of-the-box. 这些解决方案都不要求您包括body-parser并提供大量现成的额外功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM