import { mergeResolvers, mergeTypeDefs } from '@graphql-tools/merge'; import { makeExecutableSchema } from '@graphql-tools/schema'; import { GraphQLSchema } from 'graphql'; import { IResolvers } from '@graphql-tools/utils'; import express from 'express'; import { getGraphQLParameters, processRequest, renderGraphiQL, shouldRenderGraphiQL, sendResult } from "graphql-helix"; const movies = [ { id: 1, title: 'The Shawshank Redemption', year: 1994, }, { id: 2, title: 'The Godfather', year: 1972, }, { id: 3, title: 'The Godfather: Part II', year: 1974, }, { id: 4, title: 'The Dark Knight', year: 2008, } ]; export const remoteApplication1 = async () => { const movieTypes = ` type Movie { id: Int title: String year: Int tjena2: Int } type Movies { movies: [Movie] count: Int x: Int y: Int } type Query { getMovie(id: Int): Movie getMovies: Movies getMoviesWithErrors: Movies } type Mutation { createMovie(title: String!, year: Int! ): Movie } `; const movieTypes2 = ` type Movie { id: Int title: String year: Int country: String tjena2: Int } type Movies { movies: [Movie] count: Int x: Int y: Int } type Query { getMovie(id: Int): Movie getMovies: Movies getMoviesWithErrors: Movies } type Mutation { createMovie(title: String!, year: Int! ): Movie } `; function getMovieResolvers(): IResolvers { const resolvers = { Query: { getMovie: async (_: any, args: { id: number }) => { return movies[args.id]; }, getMovies: (_: any) => { return { movies, count: movies.length, }; }, getMoviesWithErrors: (_: any) => { throw new Error('getMoviesErrors'); }, }, Mutation: { createMovie: async (_: any, args: any) => { return movies.push({ title: args.title, year: args.year, id: movies.length }); }, }, }; return resolvers; } function getSchema(types: string): GraphQLSchema { const movieResolvers = getMovieResolvers(); const resolvers = mergeResolvers([movieResolvers]) const typeDefs = mergeTypeDefs([types]); return makeExecutableSchema({ resolvers, typeDefs, }); } let version = { version: '1' }; let schema = getSchema(movieTypes); const app = express(); app.use(express.json()); app.use("/version", async (req, res) => { res.send(version); }); let latestHeaders: any = {}; let errorsActive = false; app.use("/graphql", async (req, res) => { if (errorsActive) { res.status(500); res.send({ errors: [{ message: 'ERROR' }] }); return; } latestHeaders = req.headers; const request = { body: req.body, headers: req.headers, method: req.method, query: req.query, }; if (shouldRenderGraphiQL(request)) { res.send(renderGraphiQL()); } else { const { operationName, query, variables } = getGraphQLParameters(request); const result = await processRequest({ operationName, query, variables, request, schema, }); sendResult(result, res); } }); const server = app.listen(8008); await new Promise((resolve) => setTimeout(resolve, 500)); return { server, toggleSchema: () => { schema = getSchema(movieTypes2); }, setVersion: (value: string) => { version = { version: value }; }, getHeaders: () => { return latestHeaders; }, setErrors: (value: boolean) => { errorsActive = value; }, }; }; if (process.env.RUN === 'true') { remoteApplication1(); }