0

I am trying to figure out how to write a query in Apollo GraphQL.

I have a schema and have run the application in development mode. I have authenticated through the front end.

I expect that I should be able to follow this documentation and query the user.

I can see from the studio, that the Me query should be capable of checking for my first name (which I can see is recorded in the database), but when I press run in Apollo Studio, I get a null response to the query.

enter image description here

Is there an assumed step to get this working that needs to be taken before queries can be run? It gets worse when I try to do a query on the users table generally. That returns a not authenticated error (I have authenticated in the local environment in the dev app).

enter image description here

I'm struggling to connect the dots between the documentation that shows how this is expected to run queries and the starting point. I suspect that these documents have been prepared with the expectation that users know something fundamental about how to engage with them. I'm looking for disclosure as to what those assumptions might be. I can see from this question that there is a need for an authorisation header, (although my error is to do with authentication rather than authorisation). However, in my studio, the headers tab is empty. How do I populate it and what do I use to populate it?

I can see from the Apollo dev tool that it is trying to use a logged in query. I don't understand what drives this query in the Apollo Studio. Inside the localhost web app (which is running), I am logged in. When I try and run that query in the dev tools, the isLoggedIn (name of the query) is underlined, with an error explanation appearing that says:

Cannot query field "isLoggedIn" on type "Query".

The response shows:

{
  "data": {}
}

I am lost for a starting point to find something to try and solve.

enter image description here

I think, based on a comment in this Odyssey tutorial, that the sandbox does not know how to connect to my psql data (not sure about this, but how could it know what queries I have, and not know which data has been stored in the attributes on the schema?). My env variables include my psql attributes and my prisma migrate is up to date. How can I let the sandbox know where the data is stored?

I am trying to learn using this boilerplate repo.

For my next attempt, I tried using the login mutation to generate a token, that I could try adding to the header. I don't know if it needs to be added under the name 'authorization' or 'token', so I made headers with both attribute names and added the same token to each of them.

I tried running the me and user query again, and get a mouthful of gibberish in the response.

enter image description here

The link in the response text goes to a page that has the following error message:

> <Error> <Code>NoSuchKey</Code> <Message>The specified key does not
> exist.</Message> </Error>

When I try going through the process of adding an APOLLO_KEY to my env variables and starting the server, I get an error that says "Unable to reach server". When I run the diagnose script on that error, I get:

Could not find any problems with the endpoint. Would you please to let us know about this at explorer-feedback@apollographql.com

I created a new api key and tried again and am able to connect. I am able to run a login mutation and can return my first name inside that mutation, but I cannot do it from the me or user query - those queries still return the unauthenticated error response.

I have tried adding the authorization token to the header field both with and without "", and I have tried labelling that attribute as each of authorization, Authorization, token and then each of those inside "". None of them seems to make any difference to whether I can run a query. How can I find the name of the header token that Apollo Studio Explorer will accept?

I also tried the syntax suggested in this post, which is key Authorization and value "Bearer token" (there are double quotation marks around that string and a space between the word Bearer (capitalised) and the token string). There are no curly braces. That doesn't work either.

I have also tried expressing it as shown in this page of the Apollo documentation, which I think means that the key of the header value should be Authorization and the value should be the word Bearer, immediately followed by the token string generated in the output of the Login migration, inside {{ }}. When I try this, I get the same response as each of the other attempts described above.

There is a difference in the responses though, I get an unauthenticated response on the user query, and a null response on the me query.

One final strange observation: the studio returns the above error and null responses, but if I use the apollo client dev tools in the browser console, I can run the same Me query and get the result. The user query still returns an unauthenticated error when I run it in the dev tools.

I'd also note that I can ask for the firstName attribute, inside the Login mutation, and receive them back in that response. However, I can't access them inside a Me query itself.

The next thing I investigated was how the resolver was managing the data. The boilerplate includes a resolver with:

import { AuthenticationError } from "apollo-server-express"
import { createMethodDecorator } from "type-graphql"

import { ResolverContext } from "../resolverContext"
export function UseAuth(roles?: string[]): any {
  return createMethodDecorator<ResolverContext>(async ({ context: { req } }, next) => {
    const argRoles = roles || []
    if (req?.currentUser) {
      if (argRoles.length === 0) return next()
      if (argRoles.includes(req.currentUser.role)) return next()
      throw new AuthenticationError("Not authorized")
    } else {
      throw new AuthenticationError("Not authenticated")
    }
  })
}

I wondered if maybe the role wasn't being considered. But I can see that it is inside the login mutation, but is not in a query.

enter image description here

enter image description here

Is there a 'for dummies' guide to getting started with apollo graphql?

Mel
  • 2,481
  • 26
  • 113
  • 273
  • You probably have to authenticate in some way? The boilterplate repo is massive to get started. Have you tried something like https://www.howtographql.com/? – Herku Jan 31 '22 at 01:09
  • Thanks Herku, I have done all of the tutorials on graphql, and the odyssey tutorial on apollo. The point is that the login mutation does authenticate, but it doesnt allow the query to run. That doesn't make any sense. – Mel Jan 31 '22 at 01:57
  • You don't have to name your query "query Me" you should be able to just do "me { FirstName}" to directly query your aggregate – Enrico Feb 04 '22 at 10:00
  • the problem arose because I couldnt find the correct syntax to add the auth token to the header – Mel Feb 04 '22 at 17:38

1 Answers1

1

I hope this spares someone some angst.

The format that works in Apollo Studio Explorer is

Key: Authorization Value: Bearer[space][token]

There are no curly braces and no quotation marks in any of this. See this post for more discussion about this.

Mel
  • 2,481
  • 26
  • 113
  • 273