Unable to parse string identifier

I’m currently trying to make a POST http request to the GitHub GraphQL API, by retrieving the value from the input field and pass it into the GraphQL query that I’ve written but I keep getting this particular error below:

message: "Parse error on "user" (IDENTIFIER) at [2, 21]

Here’s the query that I wrote and the fetch API call below

const getUserRepo = (queryString) => `
    query userRepos(${queryString}: String!) {
        user(login: "${queryString}") {
            avatarUrl
            name
            bio
            login
            repositories(first: 20) {
              nodes {
                name
                url
                createdAt
                stargazerCount
                forkCount
                description
                primaryLanguage {
                  name
                  color
                }
              }
            }
        }
    }
`;
const fetchResults = (e) => {
  e.preventDefault();

  const searchTerm = form.elements["search"].value;

  const queryMethod = {
    method: "post",
    headers: {
      "Content-Type": "application/json",
      Authorization: "bearer " + api_token,
      "User-Agent": "repo-search-client",
    },
    body: JSON.stringify({
      query: getUserRepo(searchTerm),
    }),
  };

  fetch("https://api.github.com/graphql", queryMethod)
    .then((res) => res.json())
    .then((data) => console.log(data))
    .catch((err) => console.log(JSON.stringify(err)));

  form.reset();
};

I’d be glad if anyone could render any assitance. Thanks

I fixed it already.

The error came from me using query userRepos(${queryString}: String!) { } instead of

const getUserRepo = (queryString) => `
    query {
        user(login: "${queryString}") {
            avatarUrl
            name
            bio
            login
            repositories(first: 20) {
              nodes {
                name
                url
                createdAt
                stargazerCount
                forkCount
                description
                primaryLanguage {
                  name
                  color
                }
              }
            }
        }
    }
`;
1 Like