问题描述
我不确定我在这里做错了什么?我现在已经停留了Soem的时间,可以在我的无服务器设置中使用我的Apollo-Server-Lambda运行我的突变,当我尝试运行这样的查询时,我的查询工作正常:
{ "mutation": "{ signIn(username: \"SomeUser\", password: \"SomePassword\" ) { token } }" }
我只收到消息:"必须提供查询字符串."状态400.
我已经设置了我的解析器:
const resolvers = { Query: { users: async (_, args, ctx) => User.load(args, ctx) }, Mutation: { signIn: async (_, { username, password }, ctx) => Auth.signIn({ username, password }, ctx) } };
对于其他Infor,这是我的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! } `;
我正在使用Postman测试我的GraphQl端点,而我的内容类型是应用程序/JSON
我不知道这里有人能告诉我我在做什么错,我试图将其全部移动到查询解析器,并且它可以用"查询"替换"突变",但是它可以使用"使用"来使我对我" sens".查询"在这里,我猜想稍后我实际上想使用突变来突变数据,我还是需要它来工作?
有人可以告诉我这里我在哪里错了吗?
编辑
我安装了:graphql-playground-middleware-lambda并设置了以下设置的无服务器设置: ://github.com/prisma/graphql-playground#as-serverless handler ,如果我使用graphiql,它的工作原理,但是如果有人知道我通过Postman发送的JSON有什么问题,我仍然有兴趣吗?
推荐答案
发送请求时,您的请求主体应为适当的JSON对象,带有query属性(以及可选的,如果包括变量,则variables属性):
{ "query": "<GraphQL Document>", "variables {}, }
是这种情况,无论操作本身是query还是mutation.
上面的query属性的实际值必须是句法上正确的文档,如GraphQl 规范.文档通常由单个操作定义(query或a mutation)组成,该定义包括该操作的所有请求字段.如果使用任何内容,则该文档还将包括片段.
操作定义看起来像这样:
OperationType [Name] [VariableDefinitions] [Directives] SelectionSet
因此,您可以拥有这样的文档:
mutation SomeMutation { signIn(username: "SomeUser", password: "SomePassword") { token } }
在这里,操作的 type 是mutation,名称是SomeMutation,最外层的卷曲支架之间的所有内容都是选择设置.如果您有任何变量,则它们的类型将在选择集之前在括号中声明.
操作名称是可选的,但是在后端将其包含在调试目的上是有帮助的.从技术上讲,也可以省略操作类型,在这种情况下,GraphQl只是假设该类型是查询.例如,这仍然是一个有效的文档:
{ users { id } }
,相当于
query SomeName { users { id } }
前者称为查询速记.显然,这不能用于突变,因此突变必须始终明确说明其操作类型.一个完整的示例:
{ "query": "mutation SomeName ($username: String!, $password: String!) { signIn(username: $username, password: $password) { token } }", "variables { "username": "SomeUser", "password": "SomePassword" }, }
问题描述
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 } }" }
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) } };
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! } `;
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?
EDIT
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?
推荐答案
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 {}, }
This is the case whether the operation itself is a query or a mutation.
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.
An operation definition looks like this:
OperationType [Name] [VariableDefinitions] [Directives] SelectionSet
So you could have a document like this:
mutation SomeMutation { signIn(username: "SomeUser", password: "SomePassword") { token } }
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.
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 } }
and is equivalent to
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" }, }