We have a GraphQL API in WSO2 and we have a frontend web project that uses this API. Before run the frontend project, we enter yarn graphql:codegen
command on terminal so we can generate our graphql schema by using our service endpoint which is in our environment variables. It can generate the schema when we use our service endpoint directly.
But, if we use the gateway endpoint (which is generated in WSO2) and enter same command, we can not load the schema from this URL. We see this error.
Failed to load schema from https://gateway.staging.XXXXXXX.XXXXXXX/XXXXXX/1.0.0/graphql:
Could not obtain introspection result, received: {"Fault":{"faultcode":"soapenv:Server","faultstring":"class graphql.language.FragmentSpread cannot be cast to class graphql.language.Field (graphql.language.FragmentSpread and gr
aphql.language.Field are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @78c1a8e6)","detail":null}}
Error: Could not obtain introspection result, received: {"Fault":{"faultcode":"soapenv:Server","faultstring":"class graphql.language.FragmentSpread cannot be cast to class graphql.language.Field (graphql.language.FragmentSpread
and graphql.language.Field are in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @78c1a8e6)","detail":null}}
at getSchemaFromIntrospection (C:\Users\wic\web\node_modules\@graphql-tools\wrap\index.cjs.js:1766:15)
at C:\Users\wic\web\node_modules\@graphql-tools\wrap\index.cjs.js:1776:58
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async UrlLoader.getSubschemaConfigAsync (C:\Users\wic\web\node_modules\@graphql-tools\url-loader\index.cjs.js:333:21)
at async UrlLoader.load (C:\Users\wic\web\node_modules\@graphql-tools\url-loader\index.cjs.js:386:33)
at async loadFile (C:\Users\wic\web\node_modules\@graphql-tools\load\index.cjs.js:48:37)
at async C:\Users\wic\web\node_modules\@graphql-tools\load\index.cjs.js:426:24
GraphQL Code Generator supports:
- ES Modules and CommonJS exports (export as default or named export "schema")
- Introspection JSON File
- URL of GraphQL endpoint
- Multiple files with type definitions (glob expression)
- String in config file
Try to use one of above options and run codegen again.
Error: Failed to load schema
at loadSchema (C:\Users\wic\web\node_modules\@graphql-codegen\cli\bin.js:461:15)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async C:\Users\wic\web\node_modules\@graphql-codegen\cli\bin.js:874:55
at async Task.task (C:\Users\wic\web\node_modules\@graphql-codegen\cli\bin.js:727:17)
Error: Failed to load schema
at loadSchema (C:\Users\wic\web\node_modules\@graphql-codegen\cli\bin.js:461:15)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async C:\Users\wic\web\node_modules\@graphql-codegen\cli\bin.js:874:55
at async Task.task (C:\Users\wic\web\node_modules\@graphql-codegen\cli\bin.js:727:17)
We tried to do send query in Postman by using gateway endpoint and also we are using cognito authorization. It works well.
I suspect that this problem is about Introspection. But I didn't find any solution for WSO2. I tried to get direct URL of "Download Schema" button in WSO2 Devportal by inspecting the page but it doesn't give any URL.
How can I solve the problem?
You can get the GraphQL schema of an API using the REST API in APIM 3.2.0.
Here, you have to provide the apiId
as a path parameter.
eg:
curl -k -H "Authorization: Bearer <access_token>" "https://<hostname>/api/am/publisher/v1/apis/<apiId>/graphql-schema"
Refer this for more information on getting started with APIM Publisher REST APIs.