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

查看:39
本文介绍了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

我不知道这里是否有人能告诉我我做错了什么,我尝试将其全部移至查询解析器,然后它可以将变异"替换为查询",但使用查询"在这里,我想稍后当我真正想使用 Mutation 来改变数据时,我无论如何都需要它来工作?

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 规范.文档通常由单个操作定义(querymutation)组成,其中包括该操作的所有请求字段.该文档还将包含片段(如果有).

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天全站免登陆