Ask coding questions

← Back to all posts
i need help in graphql
h
ch1ck3n (2053)

ok so i have a graphql mutation i got from here

        query user($username: String!, $after: String, $order: String, $count: Int) {
					user: userByUsername(username: $username) {
						posts(after: $after, order: $order, count: $count) {
							items {
								"""+POST+"""
							}
						}
					}
				}

and POST is

	id
	title
	body
	preview(removeMarkdown: true, length: 150)
	url
	commentCount
	isHidden
	isPinned
	isLocked
	isAnnouncement
	timeCreated
	isAnswered
	isAnswerable		
	voteCount
	canVote
	hasVoted
	user {{
					{USER}
	}}
	repl {{
					{REPL}
	}}
	board {{
					{BOARD}
	}}
	answer {{
					{COMMENT}
	}}

and this is POST
but when I request to https://staging.replit.com/graphql with headers

    'X-Requested-With': 'a potato',
    'Referrer': 'https://staging.replit.com/'

it returns an error:
{'errors': [{'message': 'Variable "$username" is never used in operation "user".', 'locations': [{'line': 2, 'column': 20}], 'extensions': {'code': 'GRAPHQL_VALIDATION_FAILED'}}]}

i know I am supposed to put the username in there but I don't know where.

the query is supposed to return the user's posts.

Answered by RoBlockHead (520) [earned 5 cycles]
View Answer
Comments
hotnewtop
RoBlockHead (520)

the username variable should be defined in a JSON object under the main query, so you'd send this sorta json stuff:

{
  "query": "YOUR QUERY HERE",
  "variables": {
    "username":"RoBlockHead"
  }
}
ch1ck3n (2053)

@RoBlockHead ok now it says "variables are invalid JSON"

RoBlockHead (520)

@ch1ck3n wacky. can you link your repl?

ch1ck3n (2053)

@RoBlockHead its down at line 184 of App/REPLAPI/main.py

RoBlockHead (520)

@ch1ck3n not sure what the issue is tbh. I'm not quite sure how python handles objects to json. Is it possible to send the object to a JSON.toString or equivalent function before sending it to the server?

ch1ck3n (2053)

@RoBlockHead hmm now it says "POST body missing. Did you forget use body-parser middleware?"

ch1ck3n (2053)

@RoBlockHead oh, i had to stringify the variables object! thank you for helping.

RoBlockHead (520)

@ch1ck3n np! If you need any help with the gql API, I'm on the discord.

ch1ck3n (2053)

@RoBlockHead ok sure which board or channel