How to run graphql using fetch

Question:
How do I run GraphQL on Replit’s API using fetch?


Repl link:

code snippet

Is not that different from anywhere. Just be sure about the endpoint (https://replit.com/@masfrost/replit-gql-schema#schema.graphql) and your username.

const url = "https://api.replit.com/graphql";
const graphqlQuery = `
{
  userByUsername(username: "YOUR_USERNAME") {
    id
    username
    karma
    firstName
    lastName
  }
}`;

fetch(url, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
  },
  body: JSON.stringify({
    query: graphqlQuery
  })
})
.then(r => r.json())
.then(data => console.log('data returned:', data));

2 Likes

That schema link leaves out a bunch of stuff. But thanks! And karma doesn’t work (tested it before).

Also, it’s replit.com/graphql, not api.replit.com/graphql

3 Likes

It’s not working for me. Something’s wrong

Could you be more specific about what is wrong?

(Also thanks for the tips!)

Might want to try all the headers that are normally used for replit graphql requests, not just these.

I am also 90% sure this needs the variables key, even if empty.

And change that to use the variable too.

2 Likes

um don’t recently they been cracking down hard on it

2 Likes

Wb?

From the recent announcements, (and I got this verified by staff) these rules only really apply to GQL mutations, and using queries is still fine. Afaik people have only gotten in trouble from doing mutations or scraping the GQL schema

5 Likes

lol I mean I literally just got banned for talking about gql so Id just recommend not starting on that path. That’s all lol bye srry for interuptting

3 Likes

Bigminiboss is right; using GQL is dangerous. AFAIK queries are allowed, but mutations are not. There are still templates for using GQL on Replit for queries. However, many people have been banned recently for GQL-related stuff, so be very careful when using GQL. Don’t make a bot (it’s risky and may be okay and may get you banned) and don’t acquire the schema by any means.

For simple queries you are probably fine but to be truly safe, avoid anything and everything related to GQL.

3 Likes

Yet there are several bots in the community. Why?

2 Likes

There used to be fewer restrictions and so bots could be made more easily. Even two months ago you could make Replit bots using a template made by bigminiboss. But the restrictions are ever increasing and now making a bot is a really bad idea. Also Gdaybot has mod approval IIRC. PikaBot2005 didn’t last I checked but PikachuB2005 is special and the mods are aware of his bot.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.