Apollo Server,Graphql-必须提供查询字符串 [英] Apollo Server, Graphql - Must provide query string

查看:221
本文介绍了Apollo Server,Graphql-必须提供查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定在这里我在做什么错吗?现在,我一直处于停滞状态,无法在无服务器设置中使用我的apollo-server-lambda运行变异,当我尝试运行这样的查询时,我的查询工作正常:

Im not sure what im doing wrong here? I've been stuck now for soem time on getting my mutations to run with my apollo-server-lambda in my serverless setup, my queries works fine bu when i try to run a query like this:

{ "mutation": "{ signIn(username: \"SomeUser\", password: \"SomePassword\" ) { token } }" }

我只收到消息:必须提供查询字符串."状态400.

I just get the message: " Must provide query string." status 400.

我已经按照如下方式设置了我的解析器:

I've set up my resolver like so:

const resolvers = {
  Query: {
    users: async (_, args, ctx) => User.load(args, ctx)
  },

  Mutation: {
    signIn: async (_, { username, password }, ctx) => Auth.signIn({ username, password }, ctx)
  }
};

有关其他信息,这里是我的typeDefs:

For additional infor here is my typeDefs:

const typeDefs = gql`
  type User {
    id: ID!,
    firstname: String,
    lastname: String,
    username: String,
    createdAt: String,
    role: String
  }

  type AuthToken {
    token: String
  }

  type Query {
    hello: String,
    users(id: Int): [User]
  }

  type Mutation {
    signIn(username: String!, password: String!): AuthToken!
  }
`;

我正在使用邮递员来测试我的graphql端点,而我的内容类型是application/json

I'm using postman to test my graphql endpoint and my content type is application/json

我不知道这里是否有人可以告诉我我做错了什么,我试图将其全部移至查询解析器,并且可以用查询"代替变异",但是使用查询",我猜以后我何时真正想要使用变异"来变异数据,我还是需要它来工作吗?

I dont know if any one here can tell me what im doing wrong, i tryed to move it all to Query resolver, and it works replace "mutation" with "query" then but it dosent make sens to me using the "query" here and i guess later on when i actually want to use the Mutation to mutate data i would need this to work anyway?

有人可以告诉我我在哪里错了吗?

Can any one tell me where im wrong here?

编辑

我安装了:graphql-playground-middleware-lambda,并使用以下命令设置了无服务器设置: https ://github.com/prisma/graphql-playground#as-serverless-handler ,如果我使用Graphiql,它可以按预期工作,但我仍然对有人知道邮递员发送的json有什么问题还是很感兴趣?

I installed: graphql-playground-middleware-lambda and set up the serverless setup with: https://github.com/prisma/graphql-playground#as-serverless-handler and if i use Graphiql it works as intented, but im still interested if any one knows whats wrong with the json i send via postman?

推荐答案

发送请求时,您的请求正文应为格式正确的JSON对象,并带有query属性(如果需要,还可以包含variables属性)包括变量):

When sending the request, your request body should be a properly-formatted JSON object, with a query property (and optionally, a variables property if including variables):

{
  "query": "<GraphQL Document>",
  "variables {},
}

无论操作本身是query还是mutation,都是这种情况.

This is the case whether the operation itself is a query or a mutation.

上面的query属性的实际值必须是语法正确的文档,如GraphQL

The actual value of the query property above must be a syntactically correct document, as outlined in the GraphQL specification. A document will typically consist of a single operation definition (either a query or a mutation) that includes all the requested fields for that operation. The document will also include fragments, if using any.

操作定义如下:

OperationType [Name] [VariableDefinitions] [Directives] SelectionSet

因此您可以拥有一个像这样的文档:

So you could have a document like this:

mutation SomeMutation {
  signIn(username: "SomeUser", password: "SomePassword") {
    token
  }
}

在此,操作的类型mutation名称SomeMutation,最外面的花括号集之间的所有内容是选择设置.如果您有任何变量,则它们的类型将在选择集之前的括号中声明.

Here, the type of the operation is mutation, the name is SomeMutation and everything between the outermost set of curly brackets is the selection set. If you had any variables, their types would be declared in parentheses before the selection set.

操作名称是可选的,但将其包括在后端以进行调试会很有帮助.从技术上讲,操作类型也可以省略,在这种情况下GraphQL只是假设该类型是查询.例如,这仍然是有效的文档:

The operation name is optional, but it's helpful to include it for debugging purposes on the backend. Technically, the operation type can be omitted as well, in which case GraphQL simply assumes the type is a query. For example, this is still a valid document:

{
  users {
    id
  }
}

等效于

query SomeName {
  users {
    id
  }
}

前者称为查询速记.显然,这不能用于突变,因此,突变必须始终明确声明其操作类型.一个完整的例子:

The former is referred to as query shorthand. Obviously, this cannot be used for mutations, so mutations must always explicitly state their operation type. A complete example:

{
  "query": "mutation SomeName ($username: String!, $password: String!) { signIn(username: $username, password: $password) { token } }",
  "variables {
    "username": "SomeUser",
    "password": "SomePassword"
  },
}

这篇关于Apollo Server,Graphql-必须提供查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆