0

I have a list of queries/mutations/subscriptions that I wrote little bit ago and now I can't remember how GraphQL works.

All I'm trying to do is return a String that says "Hello".

typedef

type Hello {
  message: String
}

type Query {
  hello: Hello
}

resolver

const resolvers = {
  Query: {
    hello: () => 'Hello, world!'
  }
}

When I go to make a query in the GraphiQL visualizer with

{
  hello {
    message
  }
}

I get back

{
  "data": {
    "hello": {
      "message": null
    }
  }
}

Just in case it makes a difference I am using apollo-server-express.

Brandon Benefield
  • 1,504
  • 1
  • 21
  • 36

1 Answers1

2

You're asking for an object named hello with a nested key named message.

What the query is actually returning is 'Hello, world!'.message which is why it isn't erroring and returning null.

You either want to hello: () => ({ message: 'Hello, world!' }) or just make the field type a string.

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
  • So im guessing if i wanted to also return other data, say a jwt for example, i would add it to the Hello type and in the resolver i would return `{message: 'hello', jwt: 'supersecretjwt'}`? Would this also work if i was trying to return a User as well as a jwt? So `{id: user.id, username: user.username, jwt: 'jwt'}`? – Brandon Benefield Oct 14 '18 at 03:03