3
votes

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?

1

1 Answers

6
votes

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"
  },
}