How to JSON.parse() all responses using REST APIs #228
-
| 
         Everything is working fine but I would like to automatically JSON.parse() all the responses I get instead of doing a JSON.parse() of my responses in all my  Here's my  export interface MyContext {
  dataSources: {
    example: ExampleAPI
  }
}
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const resolverFiles = loadFilesSync(join(__dirname, "./**/*.resolver.*"))
const typesArray = loadFilesSync(join(__dirname, "."), { extensions: ["graphql"], recursive: true })
const resolvers = mergeResolvers(resolverFiles)
const typeDefs = mergeTypeDefs(typesArray)
const app = express()
const httpServer = http.createServer(app)
const server = new ApolloServer<MyContext>({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
})
await server.start()
app.use(
  "/",
  cors<cors.CorsRequest>({ origin: [process.env.WEBSITE_URL] }),
  bodyParser.json({ limit: "50mb" }),
  expressMiddleware(server, {
    context: async () => {
      const { cache } = server
      return {
        dataSources: {
          exampleAPI: new ExampleAPI({ cache }),
        },
      }
    },
  })
)
await new Promise<void>((resolve) => httpServer.listen({ port: 4001 }, resolve))Here's my  export class ExampleAPI extends RESTDataSource {
  override baseURL = process.env.URL
  async getExample({ code }: ParamsType): ResponseType {
    try {
      const response = await this.get<string>(`/api/${code}/example`)
      return JSON.parse(response)
    } catch (error) {
      restToGraphlError(error)
    }
  }
} | 
  
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
| 
         The helper methods already return the parsed body, so I'm not sure what you mean. Are you using the latest version of  For example: datasource-rest/src/RESTDataSource.ts Lines 383 to 393 in 5ac9b52  | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         Hmm that's strange .. Yeah I have the last version of  {
  "name": "my-project",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "compile": "tsc",
    "start": "npm run compile && node ./dist/index.js",
    "dev": "tsx watch src/index.ts",
    "codegen": "graphql-codegen --watch"
  },
  "dependencies": {
    "@apollo/datasource-rest": "^6.0.1",
    "@apollo/server": "^4.7.5",
    "@graphql-codegen/cli": "^4.0.1",
    "@graphql-codegen/typescript": "^4.0.1",
    "@graphql-codegen/typescript-resolvers": "^4.0.1",
    "@graphql-tools/load-files": "^7.0.0",
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "eslint": "^8.43.0",
    "express": "^4.18.2",
    "graphql": "^16.7.1",
    "graphql-tools": "^9.0.0"
  },
  "devDependencies": {
    "@types/cors": "^2.8.13",
    "@types/node": "^20.3.2",
    "@typescript-eslint/eslint-plugin": "^5.60.1",
    "@typescript-eslint/parser": "^5.60.1",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "prettier": "^2.8.8",
    "tsx": "^3.12.7",
    "typescript": "^5.1.3"
  }
} | 
  
Beta Was this translation helpful? Give feedback.
-
| 
         I went ahead and updated our old sandbox to use latest versions. If you can fork it and reproduce the issue here that would be helpful. I can't reproduce with the code you've pasted since I don't have your schema or resolvers.  | 
  
Beta Was this translation helpful? Give feedback.
Ok I figured it out, this is because of my backend.
The
content-typeof my data isapplication/ld+json; charset=utf-8but theparseBody()function from@apollo/datasource-restdoes aresponse.json()only if:Some i'm not in this case.
What can I do then ?