最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Apollo Server, Graphql - Must provide query string - Stack Overflow

programmeradmin6浏览0评论

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: 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?

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./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?

Share Improve this question edited Jan 7, 2019 at 19:13 Nopzen asked Jan 7, 2019 at 18:56 NopzenNopzen 5762 gold badges6 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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 plete example:

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

评论列表(0)

  1. 暂无评论