ariadne: "ImportError: cannot import name 'PLAYGROUND_HTML' from 'ariadne.constants'" building a mock GraphQL server from an old tutorial
Symptom: a quick mock of the Matterport Model API (GraphQL SDL rebuilt from the public docs, served with Flask + ariadne so the quickstart queries could run without a key) crashes at import: from ariadne.constants import PLAYGROUND_HTML raises ImportError: cannot import name 'PLAYGROUND_HTML' from 'ariadne.constants'. The snippet is the classic Flask example found in many blog posts and older ariadne docs (@app.route('/graphql', methods=['GET']) returning PLAYGROUND_HTML).
Cause: current ariadne (checked with 1.1.1 from pip install ariadne) no longer ships the HTML constant. ariadne.constants now only has CONTENT_TYPE_JSON, CONTENT_TYPE_TEXT_HTML, CONTENT_TYPE_TEXT_PLAIN, DATA_TYPE_JSON and DATA_TYPE_MULTIPART. The in-browser IDEs moved to ariadne.explorer: ExplorerGraphiQL, ExplorerApollo, ExplorerPlayground and ExplorerHttp405.
Fix for the Flask pattern:
from ariadne import make_executable_schema, graphql_sync
from ariadne.explorer import ExplorerGraphiQL
from flask import Flask, request, jsonify
schema = make_executable_schema(open('schema.graphql').read(), query)
explorer_html = ExplorerGraphiQL().html(None)
app = Flask(__name__)
@app.get('/graphql')
def explorer():
return explorer_html, 200
@app.post('/graphql')
def run():
ok, result = graphql_sync(schema, request.get_json(), context_value={'request': request})
return jsonify(result), 200 if ok else 400(ExplorerPlayground still exists if you specifically want the old Playground UI.) If you only need to check that queries are valid against the SDL, skip the server: graphql.build_schema(sdl) plus graphql.validate(schema, graphql.parse(query)) from graphql-core needs no web framework at all.
Related pitfall from the same session: import strawberry failed with ModuleNotFoundError because only ariadne was in requirements. Pick one library and pin it (ariadne==1.1.1) so tutorials and installed versions match.