Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables missing in requests in GraphQL explorer #527

Closed
cassc opened this issue Apr 14, 2021 · 4 comments
Closed

Variables missing in requests in GraphQL explorer #527

cassc opened this issue Apr 14, 2021 · 4 comments

Comments

@cassc
Copy link

cassc commented Apr 14, 2021

The sample project is created with the command lein new luminus luminus-sample +aleph +postgres +graphql +swagger +auth +auth-jwe.

In the GraphQL explorer window (http://localhost:3000/graphiql), with the following query

query ($name: String!) {
  droid(name: $name) {
    name
  }
}

and the variable,

{
  "name": "2001"
}

The request body sent to the server only has the query, missing the variables,

query ($name: String!) {
  droid(name: $name) {
    name
  }
}
@cassc
Copy link
Author

cassc commented Apr 14, 2021

Finally get it working by editing two files.

  • Change the definition of graphQLFetcher in resources/html/graphiql.html:
// Defines a GraphQL fetcher using the fetch API.
function graphQLFetcher(graphQLParams) {
  console.log(graphQLParams);
  return fetch(window.location.origin + '/api/graphql', { <-------------
    method: 'post',
    headers: {
      'Content-Type': 'application/graphql',
      'apikey': 'graphiql'
    },
    body: JSON.stringify(graphQLParams) // <-------------
  }).then(function (response) {
    return response.text();
  }).then(function (responseBody) {
console.log(responseBody);
    try {
      return JSON.parse(responseBody);
    } catch (error) {
      return responseBody;
    }
  });
}
  • Edit execute-request in the generated source code ./routes/services/graphql.clj, so it can parse variables:
(defn execute-request [query]
  (let [{:keys [query variables operationName]} (json/read-str query :key-fn keyword)]
    (-> (lacinia/execute compiled-schema query variables nil)
        (json/write-str))))

@yogthos
Copy link
Member

yogthos commented Apr 14, 2021

Ah, thanks for digging into this. I'll update the template with the fixes.

@yogthos
Copy link
Member

yogthos commented Apr 14, 2021

I've updated the template here, if that works I think we can close the issue.

@cassc
Copy link
Author

cassc commented Apr 16, 2021

Thanks!

@cassc cassc closed this as completed Apr 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants