You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何获取LeetCode GraphQL Schema?使用Apollo GraphQL Sandbox访问LeetCode GraphQL端点遇连接失败问题求助

Fixing "Unable to Reach Server" for LeetCode GraphQL in Apollo Sandbox

LeetCode's GraphQL endpoint enforces CORS restrictions and requires specific request headers, which is why Apollo Sandbox (running in your browser) can't connect directly. Here's how to resolve this and fetch the schema successfully:

1. Bypass CORS with a Browser Extension (Quickest Fix)

  • Install a CORS-unblocking extension like Allow CORS: Access-Control-Allow-Origin (available for major browsers).
  • Enable the extension, then refresh Apollo Sandbox and re-enter https://leetcode.com/graphql as the endpoint.
  • Important: Disable the extension after use to avoid exposing your browser to unnecessary security risks.

2. Use a Local Proxy (More Reliable & Secure)

LeetCode blocks requests from non-browser User-Agents, so a proxy lets you add required headers and bypass CORS. Here's a simple Node.js proxy setup:

  1. Create a new folder, initialize npm, and install dependencies:
    npm init -y
    npm install express http-proxy-middleware
    
  2. Create a proxy.js file with this code:
    const express = require('express');
    const { createProxyMiddleware } = require('http-proxy-middleware');
    const app = express();
    
    app.use('/graphql', createProxyMiddleware({
      target: 'https://leetcode.com',
      changeOrigin: true,
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
      }
    }));
    
    app.listen(4000, () => console.log('Proxy running at http://localhost:4000/graphql'));
    
  3. Run the proxy:
    node proxy.js
    
  4. In Apollo Sandbox, enter http://localhost:4000/graphql as the endpoint. You should now connect successfully.

3. Fetch the LeetCode GraphQL Schema

Once connected to the proxy (or CORS-unblocked endpoint):

  • Apollo Sandbox will automatically introspect the schema and display it in the "Schema" tab.
  • If you want to export the schema to a file, use the get-graphql-schema tool:
    npx get-graphql-schema http://localhost:4000/graphql > leetcode-schema.graphql
    

4. Test Your Query

With the connection working, your existing query should run without issues. Just enter it in the Sandbox, add the username variable, and execute:

query getUserProfile($username: String!) { 
  matchedUser(username: $username) { 
    username 
    submitStats: submitStatsGlobal { 
      acSubmissionNum { 
        difficulty 
        count 
        submissions 
      } 
    } 
  } 
}

Notes

  • Always use a valid browser User-Agent when interacting with LeetCode's API to avoid being blocked.
  • LeetCode may adjust their API restrictions over time, so if a method stops working, check for updated header requirements or proxy configurations.

内容的提问来源于stack exchange,提问作者Ysp

火山引擎 最新活动